Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 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 "chrome/browser/browser_thread.h" |
| 8 #include "chrome/browser/dom_ui/print_preview_handler.h" | |
| 9 #include "chrome/browser/dom_ui/print_preview_ui_html_source.h" | |
| 8 | 10 |
| 9 #include "base/message_loop.h" | 11 PrintPreviewUI::PrintPreviewUI(TabContents* contents) |
| 10 #include "base/singleton.h" | 12 : DOMUI(contents), |
| 11 #include "base/string_piece.h" | 13 html_source_(make_scoped_refptr(new PrintPreviewUIHTMLSource())) { |
|
James Hawkins
2011/02/03 00:53:52
Why do you need to use make_scoped_refptr() here?
Lei Zhang
2011/02/03 02:19:32
Because html_source_ is a scoped_refptr<PrintPrevi
James Hawkins
2011/02/03 21:51:40
ref_counted.h:222 -
scoped_refptr(T* p) {...}
Lei Zhang
2011/02/03 21:58:22
Removed.
| |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/browser_thread.h" | |
| 14 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" | |
| 15 #include "chrome/browser/dom_ui/print_preview_handler.h" | |
| 16 #include "chrome/browser/dom_ui/web_ui_theme_source.h" | |
| 17 #include "chrome/browser/tab_contents/tab_contents.h" | |
| 18 #include "chrome/common/jstemplate_builder.h" | |
| 19 #include "chrome/common/url_constants.h" | |
| 20 #include "grit/browser_resources.h" | |
| 21 #include "grit/generated_resources.h" | |
| 22 #include "grit/theme_resources.h" | |
| 23 #include "ui/base/l10n/l10n_util.h" | |
| 24 #include "ui/base/resource/resource_bundle.h" | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 void SetLocalizedStrings(DictionaryValue* localized_strings) { | |
| 29 localized_strings->SetString(std::string("title"), | |
| 30 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE)); | |
| 31 localized_strings->SetString(std::string("loading"), | |
| 32 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LOADING)); | |
| 33 localized_strings->SetString(std::string("noPlugin"), | |
| 34 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NO_PLUGIN)); | |
| 35 localized_strings->SetString(std::string("noPrinter"), | |
| 36 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NO_PRINTER)); | |
| 37 | |
| 38 localized_strings->SetString(std::string("printButton"), | |
| 39 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_BUTTON)); | |
| 40 localized_strings->SetString(std::string("cancelButton"), | |
| 41 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_CANCEL_BUTTON)); | |
| 42 | |
| 43 localized_strings->SetString(std::string("optionAllPages"), | |
| 44 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_ALL_PAGES)); | |
| 45 localized_strings->SetString(std::string("optionBw"), | |
| 46 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_BW)); | |
| 47 localized_strings->SetString(std::string("optionCollate"), | |
| 48 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLLATE)); | |
| 49 localized_strings->SetString(std::string("optionColor"), | |
| 50 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLOR)); | |
| 51 localized_strings->SetString(std::string("optionLandscape"), | |
| 52 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_LANDSCAPE)); | |
| 53 localized_strings->SetString(std::string("optionPortrait"), | |
| 54 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_PORTRAIT)); | |
| 55 localized_strings->SetString(std::string("optionTwoSided"), | |
| 56 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_TWO_SIDED)); | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 //////////////////////////////////////////////////////////////////////////////// | |
| 62 // | |
| 63 // PrintPreviewUIHTMLSource | |
| 64 // | |
| 65 //////////////////////////////////////////////////////////////////////////////// | |
| 66 | |
| 67 class PrintPreviewUIHTMLSource : public ChromeURLDataManager::DataSource { | |
| 68 public: | |
| 69 PrintPreviewUIHTMLSource(); | |
| 70 virtual ~PrintPreviewUIHTMLSource(); | |
| 71 | |
| 72 // Called when the network layer has requested a resource underneath | |
| 73 // the path we registered. | |
| 74 virtual void StartDataRequest(const std::string& path, | |
| 75 bool is_off_the_record, | |
| 76 int request_id); | |
| 77 virtual std::string GetMimeType(const std::string&) const; | |
| 78 | |
| 79 private: | |
| 80 DISALLOW_COPY_AND_ASSIGN(PrintPreviewUIHTMLSource); | |
| 81 }; | |
| 82 | |
| 83 PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource() | |
| 84 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()) { | |
| 85 } | |
| 86 | |
| 87 PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() {} | |
| 88 | |
| 89 void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, | |
| 90 bool is_off_the_record, | |
| 91 int request_id) { | |
| 92 // Print Preview Index page. | |
| 93 if (path.empty()) { | |
| 94 DictionaryValue localized_strings; | |
| 95 SetLocalizedStrings(&localized_strings); | |
| 96 SetFontAndTextDirection(&localized_strings); | |
| 97 | |
| 98 static const base::StringPiece print_html( | |
| 99 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 100 IDR_PRINT_PREVIEW_HTML)); | |
| 101 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( | |
| 102 print_html, &localized_strings); | |
| 103 | |
| 104 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | |
| 105 html_bytes->data.resize(full_html.size()); | |
| 106 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | |
| 107 | |
| 108 SendResponse(request_id, html_bytes); | |
| 109 return; | |
| 110 } | |
| 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 } | |
| 117 | |
| 118 std::string PrintPreviewUIHTMLSource::GetMimeType( | |
| 119 const std::string& path) const { | |
| 120 // Print Preview Index page. | |
| 121 if (path.empty()) | |
| 122 return "text/html"; | |
| 123 // Print Preview data. | |
| 124 return "application/pdf"; | |
| 125 } | |
| 126 | |
| 127 //////////////////////////////////////////////////////////////////////////////// | |
| 128 // | |
| 129 // PrintPreviewUI | |
| 130 // | |
| 131 //////////////////////////////////////////////////////////////////////////////// | |
| 132 | |
| 133 PrintPreviewUI::PrintPreviewUI(TabContents* contents) : DOMUI(contents) { | |
| 134 // PrintPreviewUI owns |handler|. | 14 // PrintPreviewUI owns |handler|. |
| 135 PrintPreviewHandler* handler = new PrintPreviewHandler(); | 15 PrintPreviewHandler* handler = new PrintPreviewHandler(); |
| 136 AddMessageHandler(handler->Attach(this)); | 16 AddMessageHandler(handler->Attach(this)); |
| 137 | 17 |
| 18 // Add a reference to |html_source_| since we need it to be valid for | |
| 19 // calls to html_source(). | |
| 20 html_source_->AddRef(); | |
|
James Hawkins
2011/02/03 00:53:52
The scoped_refptr constructor which accepts T* cal
Lei Zhang
2011/02/03 02:19:32
I was having problems where |html_source_| was get
| |
| 21 | |
| 138 // Set up the chrome://print/ source. | 22 // Set up the chrome://print/ source. |
| 139 BrowserThread::PostTask( | 23 BrowserThread::PostTask( |
| 140 BrowserThread::IO, FROM_HERE, | 24 BrowserThread::IO, FROM_HERE, |
| 141 NewRunnableMethod( | 25 NewRunnableMethod( |
| 142 ChromeURLDataManager::GetInstance(), | 26 ChromeURLDataManager::GetInstance(), |
| 143 &ChromeURLDataManager::AddDataSource, | 27 &ChromeURLDataManager::AddDataSource, |
| 144 make_scoped_refptr(new PrintPreviewUIHTMLSource()))); | 28 html_source_)); |
| 145 } | 29 } |
| 146 | 30 |
| 147 PrintPreviewUI::~PrintPreviewUI() { | 31 PrintPreviewUI::~PrintPreviewUI() { |
| 32 html_source_->Release(); | |
| 148 } | 33 } |
| 34 | |
| 35 PrintPreviewUIHTMLSource* PrintPreviewUI::html_source() { | |
| 36 return html_source_; | |
| 37 } | |
| OLD | NEW |