Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: chrome/browser/dom_ui/print_preview_ui.cc

Issue 6334010: Print Preview: Store preview data in the print preview handler.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_(new PrintPreviewUIHTMLSource()) {
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
138 // Set up the chrome://print/ source. 18 // Set up the chrome://print/ source.
139 BrowserThread::PostTask( 19 BrowserThread::PostTask(
140 BrowserThread::IO, FROM_HERE, 20 BrowserThread::IO, FROM_HERE,
141 NewRunnableMethod( 21 NewRunnableMethod(
142 ChromeURLDataManager::GetInstance(), 22 ChromeURLDataManager::GetInstance(),
143 &ChromeURLDataManager::AddDataSource, 23 &ChromeURLDataManager::AddDataSource,
144 make_scoped_refptr(new PrintPreviewUIHTMLSource()))); 24 html_source_));
145 } 25 }
146 26
147 PrintPreviewUI::~PrintPreviewUI() { 27 PrintPreviewUI::~PrintPreviewUI() {
148 } 28 }
29
30 PrintPreviewUIHTMLSource* PrintPreviewUI::html_source() {
31 return html_source_.get();
32 }
OLDNEW
« no previous file with comments | « chrome/browser/dom_ui/print_preview_ui.h ('k') | chrome/browser/dom_ui/print_preview_ui_html_source.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698