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