OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/dom_ui/print_preview_ui.h" | 5 #include "chrome/browser/dom_ui/print_preview_ui.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> |
8 | 9 |
9 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
10 #include "app/resource_bundle.h" | 11 #include "app/resource_bundle.h" |
11 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/shared_memory.h" |
12 #include "base/singleton.h" | 14 #include "base/singleton.h" |
13 #include "base/string_piece.h" | 15 #include "base/string_piece.h" |
14 #include "base/values.h" | 16 #include "base/values.h" |
15 #include "chrome/browser/browser_thread.h" | 17 #include "chrome/browser/browser_thread.h" |
16 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" | 18 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" |
17 #include "chrome/browser/dom_ui/dom_ui_theme_source.h" | 19 #include "chrome/browser/dom_ui/dom_ui_theme_source.h" |
18 #include "chrome/browser/dom_ui/print_preview_handler.h" | 20 #include "chrome/browser/dom_ui/print_preview_handler.h" |
| 21 #include "chrome/browser/printing/print_preview_manager.h" |
19 #include "chrome/browser/tab_contents/tab_contents.h" | 22 #include "chrome/browser/tab_contents/tab_contents.h" |
20 #include "chrome/common/jstemplate_builder.h" | 23 #include "chrome/common/jstemplate_builder.h" |
21 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
22 | 25 |
23 #include "grit/browser_resources.h" | 26 #include "grit/browser_resources.h" |
24 #include "grit/generated_resources.h" | 27 #include "grit/generated_resources.h" |
25 #include "grit/theme_resources.h" | 28 #include "grit/theme_resources.h" |
26 | 29 |
27 namespace { | 30 namespace { |
28 | 31 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 } // namespace | 63 } // namespace |
61 | 64 |
62 //////////////////////////////////////////////////////////////////////////////// | 65 //////////////////////////////////////////////////////////////////////////////// |
63 // | 66 // |
64 // PrintPreviewUIHTMLSource | 67 // PrintPreviewUIHTMLSource |
65 // | 68 // |
66 //////////////////////////////////////////////////////////////////////////////// | 69 //////////////////////////////////////////////////////////////////////////////// |
67 | 70 |
68 class PrintPreviewUIHTMLSource : public ChromeURLDataManager::DataSource { | 71 class PrintPreviewUIHTMLSource : public ChromeURLDataManager::DataSource { |
69 public: | 72 public: |
70 PrintPreviewUIHTMLSource(); | 73 explicit PrintPreviewUIHTMLSource(TabContents* contents); |
71 virtual ~PrintPreviewUIHTMLSource(); | 74 virtual ~PrintPreviewUIHTMLSource(); |
72 | 75 |
73 // Called when the network layer has requested a resource underneath | 76 // Called when the network layer has requested a resource underneath |
74 // the path we registered. | 77 // the path we registered. |
75 virtual void StartDataRequest(const std::string& path, | 78 virtual void StartDataRequest(const std::string& path, |
76 bool is_off_the_record, | 79 bool is_off_the_record, |
77 int request_id); | 80 int request_id); |
78 virtual std::string GetMimeType(const std::string&) const; | 81 virtual std::string GetMimeType(const std::string&) const; |
79 | 82 |
80 private: | 83 private: |
| 84 TabContents* contents_; |
81 DISALLOW_COPY_AND_ASSIGN(PrintPreviewUIHTMLSource); | 85 DISALLOW_COPY_AND_ASSIGN(PrintPreviewUIHTMLSource); |
82 }; | 86 }; |
83 | 87 |
84 PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource() | 88 PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource(TabContents* contents) |
85 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()) { | 89 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), |
| 90 contents_(contents) { |
86 } | 91 } |
87 | 92 |
88 PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() {} | 93 PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() {} |
89 | 94 |
90 void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, | 95 void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, |
91 bool is_off_the_record, | 96 bool is_off_the_record, |
92 int request_id) { | 97 int request_id) { |
93 // Print Preview Index page. | |
94 if (path.empty()) { | 98 if (path.empty()) { |
| 99 // Print Preview Index page. |
95 DictionaryValue localized_strings; | 100 DictionaryValue localized_strings; |
96 SetLocalizedStrings(&localized_strings); | 101 SetLocalizedStrings(&localized_strings); |
97 SetFontAndTextDirection(&localized_strings); | 102 SetFontAndTextDirection(&localized_strings); |
98 | 103 |
99 static const base::StringPiece print_html( | 104 static const base::StringPiece print_html( |
100 ResourceBundle::GetSharedInstance().GetRawDataResource( | 105 ResourceBundle::GetSharedInstance().GetRawDataResource( |
101 IDR_PRINT_PREVIEW_HTML)); | 106 IDR_PRINT_PREVIEW_HTML)); |
102 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( | 107 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( |
103 print_html, &localized_strings); | 108 print_html, &localized_strings); |
104 | 109 |
105 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | 110 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
106 html_bytes->data.resize(full_html.size()); | 111 html_bytes->data.resize(full_html.size()); |
107 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | 112 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
108 | 113 |
109 SendResponse(request_id, html_bytes); | 114 SendResponse(request_id, html_bytes); |
110 return; | 115 return; |
| 116 } else if (path == "print.pdf") { |
| 117 // Print Preview data. |
| 118 printing::PrintPreviewManager::PrintPreviewData data; |
| 119 printing::PrintPreviewManager* print_preview_manager = |
| 120 printing::PrintPreviewManager::GetInstance(); |
| 121 if (!print_preview_manager->GetPrintPreviewData(contents_, &data)) { |
| 122 LOG(ERROR) << "failed to get pdf"; |
| 123 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); |
| 124 SendResponse(request_id, empty_bytes); |
| 125 return; |
| 126 } |
| 127 |
| 128 uint32 preview_data_size = data.second; |
| 129 char* preview_data = reinterpret_cast<char*>(data.first->memory()); |
| 130 |
| 131 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 132 html_bytes->data.resize(preview_data_size); |
| 133 std::vector<unsigned char>::iterator it = html_bytes->data.begin(); |
| 134 for (uint32 i = 0; i < preview_data_size; ++i, ++it) |
| 135 *it++ = *(preview_data + i); |
| 136 SendResponse(request_id, html_bytes); |
| 137 } else { |
| 138 // Invalid request. |
| 139 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); |
| 140 SendResponse(request_id, empty_bytes); |
111 } | 141 } |
112 | |
113 // Print Preview data. | |
114 NOTIMPLEMENTED() << "No PDF for you"; | |
115 scoped_refptr<RefCountedBytes> data_bytes(new RefCountedBytes); | |
116 SendResponse(request_id, data_bytes); | |
117 } | 142 } |
118 | 143 |
119 std::string PrintPreviewUIHTMLSource::GetMimeType( | 144 std::string PrintPreviewUIHTMLSource::GetMimeType( |
120 const std::string& path) const { | 145 const std::string& path) const { |
121 // Print Preview Index page. | 146 // Print Preview Index page. |
122 if (path.empty()) | 147 if (path.empty()) |
123 return "text/html"; | 148 return "text/html"; |
124 // Print Preview data. | 149 // Print Preview data. |
125 return "application/pdf"; | 150 return "application/pdf"; |
126 } | 151 } |
127 | 152 |
128 //////////////////////////////////////////////////////////////////////////////// | 153 //////////////////////////////////////////////////////////////////////////////// |
129 // | 154 // |
130 // PrintPreviewUI | 155 // PrintPreviewUI |
131 // | 156 // |
132 //////////////////////////////////////////////////////////////////////////////// | 157 //////////////////////////////////////////////////////////////////////////////// |
133 | 158 |
134 PrintPreviewUI::PrintPreviewUI(TabContents* contents) : DOMUI(contents) { | 159 PrintPreviewUI::PrintPreviewUI(TabContents* contents) : DOMUI(contents) { |
135 // PrintPreviewUI owns |handler|. | 160 // PrintPreviewUI owns |handler|. |
136 PrintPreviewHandler* handler = new PrintPreviewHandler(); | 161 PrintPreviewHandler* handler = new PrintPreviewHandler(); |
137 AddMessageHandler(handler->Attach(this)); | 162 AddMessageHandler(handler->Attach(this)); |
138 | 163 |
139 // Set up the chrome://print/ source. | 164 // Set up the chrome://print/ source. |
140 BrowserThread::PostTask( | 165 BrowserThread::PostTask( |
141 BrowserThread::IO, FROM_HERE, | 166 BrowserThread::IO, FROM_HERE, |
142 NewRunnableMethod( | 167 NewRunnableMethod( |
143 ChromeURLDataManager::GetInstance(), | 168 ChromeURLDataManager::GetInstance(), |
144 &ChromeURLDataManager::AddDataSource, | 169 &ChromeURLDataManager::AddDataSource, |
145 make_scoped_refptr(new PrintPreviewUIHTMLSource()))); | 170 make_scoped_refptr(new PrintPreviewUIHTMLSource(contents)))); |
146 } | 171 } |
147 | 172 |
148 PrintPreviewUI::~PrintPreviewUI() { | 173 PrintPreviewUI::~PrintPreviewUI() { |
149 } | 174 } |
OLD | NEW |