1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/print_preview_handler.h"
#include <string>
#include "base/json/json_reader.h"
#include "base/threading/thread.h"
#include "base/values.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/printing/print_preview_tab_controller.h"
#include "chrome/browser/ui/webui/print_preview_ui_html_source.h"
#include "chrome/browser/ui/webui/print_preview_ui.h"
#include "chrome/common/print_messages.h"
#include "content/browser/browser_thread.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "printing/backend/print_backend.h"
#include "printing/metafile.h"
#include "printing/metafile_impl.h"
#include "printing/print_job_constants.h"
namespace {
const bool kColorDefaultValue = false;
const bool kLandscapeDefaultValue = false;
TabContents* GetInitiatorTab(TabContents* preview_tab) {
printing::PrintPreviewTabController* tab_controller =
printing::PrintPreviewTabController::GetInstance();
if (!tab_controller)
return NULL;
return tab_controller->GetInitiatorTab(preview_tab);
}
// Get the print job settings dictionary from |args|. The caller takes
// ownership of the returned DictionaryValue. Returns NULL on failure.
DictionaryValue* GetSettingsDictionary(const ListValue* args) {
std::string json_str;
if (!args->GetString(0, &json_str)) {
NOTREACHED() << "Could not read JSON argument";
return NULL;
}
if (json_str.empty()) {
NOTREACHED() << "Empty print job settings";
return NULL;
}
scoped_ptr<DictionaryValue> settings(static_cast<DictionaryValue*>(
base::JSONReader::Read(json_str, false)));
if (!settings.get() || !settings->IsType(Value::TYPE_DICTIONARY)) {
NOTREACHED() << "Print job settings must be a dictionary.";
return NULL;
}
if (settings->empty()) {
NOTREACHED() << "Print job settings dictionary is empty";
return NULL;
}
return settings.release();
}
} // namespace
class EnumeratePrintersTaskProxy
: public base::RefCountedThreadSafe<EnumeratePrintersTaskProxy,
BrowserThread::DeleteOnUIThread> {
public:
EnumeratePrintersTaskProxy(const base::WeakPtr<PrintPreviewHandler>& handler,
printing::PrintBackend* print_backend)
: handler_(handler),
print_backend_(print_backend) {
}
void EnumeratePrinters() {
ListValue* printers = new ListValue;
int default_printer_index = -1;
printing::PrinterList printer_list;
print_backend_->EnumeratePrinters(&printer_list);
int i = 0;
for (printing::PrinterList::iterator index = printer_list.begin();
index != printer_list.end(); ++index, ++i) {
printers->Append(new StringValue(index->printer_name));
if (index->is_default)
default_printer_index = i;
}
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableMethod(this,
&EnumeratePrintersTaskProxy::SendPrinterList,
printers,
new FundamentalValue(default_printer_index)));
}
void SendPrinterList(ListValue* printers,
FundamentalValue* default_printer_index) {
if (handler_)
handler_->SendPrinterList(*printers, *default_printer_index);
delete printers;
delete default_printer_index;
}
private:
friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
friend class DeleteTask<EnumeratePrintersTaskProxy>;
~EnumeratePrintersTaskProxy() {}
base::WeakPtr<PrintPreviewHandler> handler_;
scoped_refptr<printing::PrintBackend> print_backend_;
DISALLOW_COPY_AND_ASSIGN(EnumeratePrintersTaskProxy);
};
// A Task implementation that stores a PDF file on disk.
class PrintToPdfTask : public Task {
public:
// Takes ownership of |metafile|.
PrintToPdfTask(printing::Metafile* metafile, const FilePath& path)
: metafile_(metafile), path_(path) {
}
~PrintToPdfTask() {}
// Task implementation
virtual void Run() {
metafile_->SaveTo(path_);
}
private:
// The metafile holding the PDF data.
scoped_ptr<printing::Metafile> metafile_;
// The absolute path where the file will be saved.
FilePath path_;
};
PrintPreviewHandler::PrintPreviewHandler()
: print_backend_(printing::PrintBackend::CreateInstance(NULL)),
need_to_generate_preview_(true),
color_(kColorDefaultValue),
landscape_(kLandscapeDefaultValue) {
}
PrintPreviewHandler::~PrintPreviewHandler() {
if (select_file_dialog_.get())
select_file_dialog_->ListenerDestroyed();
}
void PrintPreviewHandler::RegisterMessages() {
web_ui_->RegisterMessageCallback("getPrinters",
NewCallback(this, &PrintPreviewHandler::HandleGetPrinters));
web_ui_->RegisterMessageCallback("getPreview",
NewCallback(this, &PrintPreviewHandler::HandleGetPreview));
web_ui_->RegisterMessageCallback("print",
NewCallback(this, &PrintPreviewHandler::HandlePrint));
}
void PrintPreviewHandler::HandleGetPrinters(const ListValue*) {
scoped_refptr<EnumeratePrintersTaskProxy> task =
new EnumeratePrintersTaskProxy(AsWeakPtr(), print_backend_.get());
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
NewRunnableMethod(task.get(),
&EnumeratePrintersTaskProxy::EnumeratePrinters));
}
void PrintPreviewHandler::HandleGetPreview(const ListValue* args) {
TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents());
if (!initiator_tab)
return;
scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args));
if (!settings.get())
return;
// Handle settings that do not require print preview regeneration.
ProcessColorSetting(*settings);
// Handle settings that require print preview regeneration.
ProcessLandscapeSetting(*settings);
if (need_to_generate_preview_) {
RenderViewHost* rvh = initiator_tab->render_view_host();
rvh->Send(new PrintMsg_PrintPreview(rvh->routing_id(), *settings));
need_to_generate_preview_ = false;
}
}
void PrintPreviewHandler::HandlePrint(const ListValue* args) {
TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents());
if (initiator_tab) {
RenderViewHost* rvh = initiator_tab->render_view_host();
rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id()));
}
scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args));
if (!settings.get())
return;
bool print_to_pdf;
settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf);
if (print_to_pdf) {
SelectFile();
} else {
RenderViewHost* rvh = web_ui_->GetRenderViewHost();
rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings));
}
}
void PrintPreviewHandler::SendPrinterList(
const ListValue& printers,
const FundamentalValue& default_printer_index) {
web_ui_->CallJavascriptFunction("setPrinters", printers,
default_printer_index);
}
void PrintPreviewHandler::ProcessColorSetting(const DictionaryValue& settings) {
bool color = kColorDefaultValue;
settings.GetBoolean(printing::kSettingColor, &color);
if (color != color_) {
color_ = color;
FundamentalValue color_value(color_);
web_ui_->CallJavascriptFunction("setColor", color_value);
}
}
void PrintPreviewHandler::ProcessLandscapeSetting(
const DictionaryValue& settings) {
bool landscape = kLandscapeDefaultValue;
settings.GetBoolean(printing::kSettingLandscape, &landscape);
if (landscape != landscape_) {
landscape_ = landscape;
need_to_generate_preview_ = true;
}
}
void PrintPreviewHandler::SelectFile() {
SelectFileDialog::FileTypeInfo file_type_info;
file_type_info.extensions.resize(1);
file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf"));
if (!select_file_dialog_.get())
select_file_dialog_ = SelectFileDialog::Create(this);
select_file_dialog_->SelectFile(
SelectFileDialog::SELECT_SAVEAS_FILE,
string16(),
FilePath(),
&file_type_info,
0,
FILE_PATH_LITERAL(""),
platform_util::GetTopLevel(
web_ui_->tab_contents()->GetNativeView()),
NULL);
}
void PrintPreviewHandler::FileSelected(const FilePath& path,
int index, void* params) {
PrintPreviewUIHTMLSource::PrintPreviewData data;
PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_);
pp_ui->html_source()->GetPrintPreviewData(&data);
DCHECK(data.first != NULL);
DCHECK(data.second > 0);
printing::PreviewMetafile* metafile = new printing::PreviewMetafile;
metafile->InitFromData(data.first->memory(), data.second);
PrintToPdfTask* task = new PrintToPdfTask(metafile, path);
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task);
}
|