This source file includes following definitions.
- GetPreviewDataForIndex
- SetPreviewDataForIndex
- GetAvailableDraftPageCount
- IsInvalidIndex
- GetInstance
- GetDataEntry
- SetDataEntry
- RemoveEntry
- GetAvailableDraftPageCount
#include "chrome/browser/printing/print_preview_data_service.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/singleton.h"
#include "base/stl_util.h"
#include "printing/print_job_constants.h"
class PrintPreviewDataStore : public base::RefCounted<PrintPreviewDataStore> {
public:
PrintPreviewDataStore() {}
void GetPreviewDataForIndex(int index,
scoped_refptr<base::RefCountedBytes>* data) {
if (IsInvalidIndex(index))
return;
PreviewPageDataMap::iterator it = page_data_map_.find(index);
if (it != page_data_map_.end())
*data = it->second.get();
}
void SetPreviewDataForIndex(int index, const base::RefCountedBytes* data) {
if (IsInvalidIndex(index))
return;
page_data_map_[index] = const_cast<base::RefCountedBytes*>(data);
}
int GetAvailableDraftPageCount() {
int page_data_map_size = page_data_map_.size();
if (ContainsKey(page_data_map_, printing::COMPLETE_PREVIEW_DOCUMENT_INDEX))
page_data_map_size--;
return page_data_map_size;
}
private:
friend class base::RefCounted<PrintPreviewDataStore>;
typedef std::map<int, scoped_refptr<base::RefCountedBytes> >
PreviewPageDataMap;
~PrintPreviewDataStore() {}
static bool IsInvalidIndex(int index) {
return (index != printing::COMPLETE_PREVIEW_DOCUMENT_INDEX &&
index < printing::FIRST_PAGE_INDEX);
}
PreviewPageDataMap page_data_map_;
DISALLOW_COPY_AND_ASSIGN(PrintPreviewDataStore);
};
PrintPreviewDataService* PrintPreviewDataService::GetInstance() {
return Singleton<PrintPreviewDataService>::get();
}
PrintPreviewDataService::PrintPreviewDataService() {
}
PrintPreviewDataService::~PrintPreviewDataService() {
}
void PrintPreviewDataService::GetDataEntry(
int32 preview_ui_id,
int index,
scoped_refptr<base::RefCountedBytes>* data_bytes) {
*data_bytes = NULL;
PreviewDataStoreMap::const_iterator it = data_store_map_.find(preview_ui_id);
if (it != data_store_map_.end())
it->second->GetPreviewDataForIndex(index, data_bytes);
}
void PrintPreviewDataService::SetDataEntry(
int32 preview_ui_id,
int index,
const base::RefCountedBytes* data_bytes) {
if (!ContainsKey(data_store_map_, preview_ui_id))
data_store_map_[preview_ui_id] = new PrintPreviewDataStore();
data_store_map_[preview_ui_id]->SetPreviewDataForIndex(index, data_bytes);
}
void PrintPreviewDataService::RemoveEntry(int32 preview_ui_id) {
data_store_map_.erase(preview_ui_id);
}
int PrintPreviewDataService::GetAvailableDraftPageCount(int32 preview_ui_id) {
PreviewDataStoreMap::const_iterator it = data_store_map_.find(preview_ui_id);
return (it == data_store_map_.end()) ?
0 : it->second->GetAvailableDraftPageCount();
}