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/ptr_util.h" |
8 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
9 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
10 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
11 #include "printing/print_job_constants.h" | 12 #include "printing/print_job_constants.h" |
12 | 13 |
13 // PrintPreviewDataStore stores data for preview workflow and preview printing | 14 // PrintPreviewDataStore stores data for preview workflow and preview printing |
14 // workflow. | 15 // workflow. |
15 // | 16 // |
16 // NOTE: | 17 // NOTE: |
17 // This class stores a list of PDFs. The list |index| is zero-based and can | 18 // This class stores a list of PDFs. The list |index| is zero-based and can |
18 // be |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to represent complete preview | 19 // be |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to represent complete preview |
19 // document. The PDF stored at |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| is | 20 // document. The PDF stored at |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| is |
20 // optimized with font subsetting, compression, etc. PDF's stored at all other | 21 // optimized with font subsetting, compression, etc. PDF's stored at all other |
21 // indices are unoptimized. | 22 // indices are unoptimized. |
22 // | 23 // |
23 // PrintPreviewDataStore owns the data and is responsible for freeing it when | 24 // PrintPreviewDataStore owns the data and is responsible for freeing it when |
24 // either: | 25 // either: |
25 // a) There is a new data. | 26 // a) There is a new data. |
26 // b) When PrintPreviewDataStore is destroyed. | 27 // b) When PrintPreviewDataStore is destroyed. |
27 // | 28 // |
28 class PrintPreviewDataStore : public base::RefCounted<PrintPreviewDataStore> { | 29 class PrintPreviewDataStore { |
29 public: | 30 public: |
30 PrintPreviewDataStore() {} | 31 PrintPreviewDataStore() {} |
| 32 ~PrintPreviewDataStore() {} |
31 | 33 |
32 // Get the preview page for the specified |index|. | 34 // Get the preview page for the specified |index|. |
33 void GetPreviewDataForIndex( | 35 void GetPreviewDataForIndex( |
34 int index, | 36 int index, |
35 scoped_refptr<base::RefCountedBytes>* data) const { | 37 scoped_refptr<base::RefCountedBytes>* data) const { |
36 if (IsInvalidIndex(index)) | 38 if (IsInvalidIndex(index)) |
37 return; | 39 return; |
38 | 40 |
39 PreviewPageDataMap::const_iterator it = page_data_map_.find(index); | 41 PreviewPageDataMap::const_iterator it = page_data_map_.find(index); |
40 if (it != page_data_map_.end()) | 42 if (it != page_data_map_.end()) |
(...skipping 12 matching lines...) Expand all Loading... |
53 // Returns the available draft page count. | 55 // Returns the available draft page count. |
54 int GetAvailableDraftPageCount() const { | 56 int GetAvailableDraftPageCount() const { |
55 int page_data_map_size = page_data_map_.size(); | 57 int page_data_map_size = page_data_map_.size(); |
56 if (base::ContainsKey(page_data_map_, | 58 if (base::ContainsKey(page_data_map_, |
57 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX)) | 59 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX)) |
58 page_data_map_size--; | 60 page_data_map_size--; |
59 return page_data_map_size; | 61 return page_data_map_size; |
60 } | 62 } |
61 | 63 |
62 private: | 64 private: |
63 friend class base::RefCounted<PrintPreviewDataStore>; | |
64 | |
65 // 1:1 relationship between page index and its associated preview data. | 65 // 1:1 relationship between page index and its associated preview data. |
66 // Key: Page index is zero-based and can be | 66 // Key: Page index is zero-based and can be |
67 // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to represent complete preview | 67 // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to represent complete preview |
68 // document. | 68 // document. |
69 // Value: Preview data. | 69 // Value: Preview data. |
70 using PreviewPageDataMap = | 70 using PreviewPageDataMap = |
71 std::map<int, scoped_refptr<base::RefCountedBytes>>; | 71 std::map<int, scoped_refptr<base::RefCountedBytes>>; |
72 | 72 |
73 ~PrintPreviewDataStore() {} | |
74 | |
75 static bool IsInvalidIndex(int index) { | 73 static bool IsInvalidIndex(int index) { |
76 return (index != printing::COMPLETE_PREVIEW_DOCUMENT_INDEX && | 74 return (index != printing::COMPLETE_PREVIEW_DOCUMENT_INDEX && |
77 index < printing::FIRST_PAGE_INDEX); | 75 index < printing::FIRST_PAGE_INDEX); |
78 } | 76 } |
79 | 77 |
80 PreviewPageDataMap page_data_map_; | 78 PreviewPageDataMap page_data_map_; |
81 | 79 |
82 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDataStore); | 80 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDataStore); |
83 }; | 81 }; |
84 | 82 |
(...skipping 15 matching lines...) Expand all Loading... |
100 *data_bytes = nullptr; | 98 *data_bytes = nullptr; |
101 PreviewDataStoreMap::const_iterator it = data_store_map_.find(preview_ui_id); | 99 PreviewDataStoreMap::const_iterator it = data_store_map_.find(preview_ui_id); |
102 if (it != data_store_map_.end()) | 100 if (it != data_store_map_.end()) |
103 it->second->GetPreviewDataForIndex(index, data_bytes); | 101 it->second->GetPreviewDataForIndex(index, data_bytes); |
104 } | 102 } |
105 | 103 |
106 void PrintPreviewDataService::SetDataEntry( | 104 void PrintPreviewDataService::SetDataEntry( |
107 int32_t preview_ui_id, | 105 int32_t preview_ui_id, |
108 int index, | 106 int index, |
109 scoped_refptr<base::RefCountedBytes> data_bytes) { | 107 scoped_refptr<base::RefCountedBytes> data_bytes) { |
110 if (!base::ContainsKey(data_store_map_, preview_ui_id)) { | 108 if (!base::ContainsKey(data_store_map_, preview_ui_id)) |
111 data_store_map_[preview_ui_id] = | 109 data_store_map_[preview_ui_id] = base::MakeUnique<PrintPreviewDataStore>(); |
112 make_scoped_refptr(new PrintPreviewDataStore()); | |
113 } | |
114 | |
115 data_store_map_[preview_ui_id]->SetPreviewDataForIndex(index, | 110 data_store_map_[preview_ui_id]->SetPreviewDataForIndex(index, |
116 std::move(data_bytes)); | 111 std::move(data_bytes)); |
117 } | 112 } |
118 | 113 |
119 void PrintPreviewDataService::RemoveEntry(int32_t preview_ui_id) { | 114 void PrintPreviewDataService::RemoveEntry(int32_t preview_ui_id) { |
120 data_store_map_.erase(preview_ui_id); | 115 data_store_map_.erase(preview_ui_id); |
121 } | 116 } |
122 | 117 |
123 int PrintPreviewDataService::GetAvailableDraftPageCount( | 118 int PrintPreviewDataService::GetAvailableDraftPageCount( |
124 int32_t preview_ui_id) const { | 119 int32_t preview_ui_id) const { |
125 PreviewDataStoreMap::const_iterator it = data_store_map_.find(preview_ui_id); | 120 PreviewDataStoreMap::const_iterator it = data_store_map_.find(preview_ui_id); |
126 return (it == data_store_map_.end()) ? | 121 return (it == data_store_map_.end()) ? |
127 0 : it->second->GetAvailableDraftPageCount(); | 122 0 : it->second->GetAvailableDraftPageCount(); |
128 } | 123 } |
OLD | NEW |