OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/printing/print_preview_data_service.h" | 5 #include "chrome/browser/printing/print_preview_data_service.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 // PrintPreviewDataStore owns the data and is responsible for freeing it when | 23 // PrintPreviewDataStore owns the data and is responsible for freeing it when |
24 // either: | 24 // either: |
25 // a) There is a new data. | 25 // a) There is a new data. |
26 // b) When PrintPreviewDataStore is destroyed. | 26 // b) When PrintPreviewDataStore is destroyed. |
27 // | 27 // |
28 class PrintPreviewDataStore : public base::RefCounted<PrintPreviewDataStore> { | 28 class PrintPreviewDataStore : public base::RefCounted<PrintPreviewDataStore> { |
29 public: | 29 public: |
30 PrintPreviewDataStore() {} | 30 PrintPreviewDataStore() {} |
31 | 31 |
32 // Get the preview page for the specified |index|. | 32 // Get the preview page for the specified |index|. |
33 void GetPreviewDataForIndex(int index, | 33 void GetPreviewDataForIndex( |
34 scoped_refptr<base::RefCountedBytes>* data) { | 34 int index, |
| 35 scoped_refptr<base::RefCountedBytes>* data) const { |
35 if (IsInvalidIndex(index)) | 36 if (IsInvalidIndex(index)) |
36 return; | 37 return; |
37 | 38 |
38 PreviewPageDataMap::iterator it = page_data_map_.find(index); | 39 PreviewPageDataMap::const_iterator it = page_data_map_.find(index); |
39 if (it != page_data_map_.end()) | 40 if (it != page_data_map_.end()) |
40 *data = it->second.get(); | 41 *data = it->second.get(); |
41 } | 42 } |
42 | 43 |
43 // Set/Update the preview data entry for the specified |index|. | 44 // Set/Update the preview data entry for the specified |index|. |
44 void SetPreviewDataForIndex(int index, | 45 void SetPreviewDataForIndex(int index, |
45 scoped_refptr<base::RefCountedBytes> data) { | 46 scoped_refptr<base::RefCountedBytes> data) { |
46 if (IsInvalidIndex(index)) | 47 if (IsInvalidIndex(index)) |
47 return; | 48 return; |
48 | 49 |
49 page_data_map_[index] = std::move(data); | 50 page_data_map_[index] = std::move(data); |
50 } | 51 } |
51 | 52 |
52 // Returns the available draft page count. | 53 // Returns the available draft page count. |
53 int GetAvailableDraftPageCount() { | 54 int GetAvailableDraftPageCount() const { |
54 int page_data_map_size = page_data_map_.size(); | 55 int page_data_map_size = page_data_map_.size(); |
55 if (base::ContainsKey(page_data_map_, | 56 if (base::ContainsKey(page_data_map_, |
56 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX)) | 57 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX)) |
57 page_data_map_size--; | 58 page_data_map_size--; |
58 return page_data_map_size; | 59 return page_data_map_size; |
59 } | 60 } |
60 | 61 |
61 private: | 62 private: |
62 friend class base::RefCounted<PrintPreviewDataStore>; | 63 friend class base::RefCounted<PrintPreviewDataStore>; |
63 | 64 |
(...skipping 24 matching lines...) Expand all Loading... |
88 | 89 |
89 PrintPreviewDataService::PrintPreviewDataService() { | 90 PrintPreviewDataService::PrintPreviewDataService() { |
90 } | 91 } |
91 | 92 |
92 PrintPreviewDataService::~PrintPreviewDataService() { | 93 PrintPreviewDataService::~PrintPreviewDataService() { |
93 } | 94 } |
94 | 95 |
95 void PrintPreviewDataService::GetDataEntry( | 96 void PrintPreviewDataService::GetDataEntry( |
96 int32_t preview_ui_id, | 97 int32_t preview_ui_id, |
97 int index, | 98 int index, |
98 scoped_refptr<base::RefCountedBytes>* data_bytes) { | 99 scoped_refptr<base::RefCountedBytes>* data_bytes) const { |
99 *data_bytes = nullptr; | 100 *data_bytes = nullptr; |
100 PreviewDataStoreMap::const_iterator it = data_store_map_.find(preview_ui_id); | 101 PreviewDataStoreMap::const_iterator it = data_store_map_.find(preview_ui_id); |
101 if (it != data_store_map_.end()) | 102 if (it != data_store_map_.end()) |
102 it->second->GetPreviewDataForIndex(index, data_bytes); | 103 it->second->GetPreviewDataForIndex(index, data_bytes); |
103 } | 104 } |
104 | 105 |
105 void PrintPreviewDataService::SetDataEntry( | 106 void PrintPreviewDataService::SetDataEntry( |
106 int32_t preview_ui_id, | 107 int32_t preview_ui_id, |
107 int index, | 108 int index, |
108 scoped_refptr<base::RefCountedBytes> data_bytes) { | 109 scoped_refptr<base::RefCountedBytes> data_bytes) { |
109 if (!base::ContainsKey(data_store_map_, preview_ui_id)) { | 110 if (!base::ContainsKey(data_store_map_, preview_ui_id)) { |
110 data_store_map_[preview_ui_id] = | 111 data_store_map_[preview_ui_id] = |
111 make_scoped_refptr(new PrintPreviewDataStore()); | 112 make_scoped_refptr(new PrintPreviewDataStore()); |
112 } | 113 } |
113 | 114 |
114 data_store_map_[preview_ui_id]->SetPreviewDataForIndex(index, | 115 data_store_map_[preview_ui_id]->SetPreviewDataForIndex(index, |
115 std::move(data_bytes)); | 116 std::move(data_bytes)); |
116 } | 117 } |
117 | 118 |
118 void PrintPreviewDataService::RemoveEntry(int32_t preview_ui_id) { | 119 void PrintPreviewDataService::RemoveEntry(int32_t preview_ui_id) { |
119 data_store_map_.erase(preview_ui_id); | 120 data_store_map_.erase(preview_ui_id); |
120 } | 121 } |
121 | 122 |
122 int PrintPreviewDataService::GetAvailableDraftPageCount(int32_t preview_ui_id) { | 123 int PrintPreviewDataService::GetAvailableDraftPageCount( |
| 124 int32_t preview_ui_id) const { |
123 PreviewDataStoreMap::const_iterator it = data_store_map_.find(preview_ui_id); | 125 PreviewDataStoreMap::const_iterator it = data_store_map_.find(preview_ui_id); |
124 return (it == data_store_map_.end()) ? | 126 return (it == data_store_map_.end()) ? |
125 0 : it->second->GetAvailableDraftPageCount(); | 127 0 : it->second->GetAvailableDraftPageCount(); |
126 } | 128 } |
OLD | NEW |