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