blob: 5da3a2ff5267c45b5b4a5268b3e90c61e5e75d29 [file] [log] [blame]
[email protected]73852b8f2010-05-14 00:38:121// 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
3// found in the LICENSE file.
4
5#include "chrome/browser/printing/print_dialog_cloud.h"
6#include "chrome/browser/printing/print_dialog_cloud_internal.h"
7
8#include "app/l10n_util.h"
9#include "base/base64.h"
[email protected]4baf1c42010-05-18 18:45:2510#include "base/command_line.h"
[email protected]73852b8f2010-05-14 00:38:1211#include "base/file_util.h"
12#include "base/json/json_reader.h"
13#include "base/logging.h"
14#include "base/values.h"
15#include "chrome/browser/browser_list.h"
16#include "chrome/browser/chrome_thread.h"
17#include "chrome/browser/dom_ui/dom_ui.h"
18#include "chrome/browser/dom_ui/dom_ui_util.h"
19#include "chrome/browser/dom_ui/html_dialog_ui.h"
20#include "chrome/browser/debugger/devtools_manager.h"
[email protected]4baf1c42010-05-18 18:45:2521#include "chrome/browser/profile.h"
22#include "chrome/browser/pref_service.h"
[email protected]73852b8f2010-05-14 00:38:1223#include "chrome/browser/tab_contents/tab_contents.h"
24#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]4baf1c42010-05-18 18:45:2525#include "chrome/common/chrome_switches.h"
26#include "chrome/common/pref_names.h"
[email protected]73852b8f2010-05-14 00:38:1227#include "chrome/common/notification_observer.h"
28#include "chrome/common/notification_registrar.h"
29#include "chrome/common/notification_source.h"
30#include "chrome/common/render_messages.h"
31#include "chrome/common/url_constants.h"
32
33#include "grit/generated_resources.h"
34
35// This module implements the UI support in Chrome for cloud printing.
36// This means hosting a dialog containing HTML/JavaScript and using
37// the published cloud print user interface integration APIs to get
38// page setup settings from the dialog contents and provide the
39// generated print PDF to the dialog contents for uploading to the
40// cloud print service.
41
42// Currently, the flow between these classes is as follows:
43
44// PrintDialogCloud::CreatePrintDialogForPdf is called from
45// resource_message_filter_gtk.cc once the renderer has informed the
46// renderer host that PDF generation into the renderer host provided
47// temp file has been completed. That call is on the IO thread.
48// That, in turn, hops over to the UI thread to create an instance of
49// PrintDialogCloud.
50
51// The constructor for PrintDialogCloud creates a
52// CloudPrintHtmlDialogDelegate and asks the current active browser to
53// show an HTML dialog using that class as the delegate. That class
54// hands in the kCloudPrintResourcesURL as the URL to visit. That is
55// recognized by the GetDOMUIFactoryFunction as a signal to create an
56// ExternalHtmlDialogUI.
57
58// CloudPrintHtmlDialogDelegate also temporarily owns a
59// CloudPrintFlowHandler, a class which is responsible for the actual
60// interactions with the dialog contents, including handing in the PDF
61// print data and getting any page setup parameters that the dialog
62// contents provides. As part of bringing up the dialog,
63// HtmlDialogUI::RenderViewCreated is called (an override of
64// DOMUI::RenderViewCreated). That routine, in turn, calls the
65// delegate's GetDOMMessageHandlers routine, at which point the
66// ownership of the CloudPrintFlowHandler is handed over. A pointer
67// to the flow handler is kept to facilitate communication back and
68// forth between the two classes.
69
70// The DOMUI continues dialog bring-up, calling
71// CloudPrintFlowHandler::RegisterMessages. This is where the
72// additional object model capabilities are registered for the dialog
73// contents to use. It is also at this time that capabilities for the
74// dialog contents are adjusted to allow the dialog contents to close
75// the window. In addition, the pending URL is redirected to the
76// actual cloud print service URL. The flow controller also registers
77// for notification of when the dialog contents finish loading, which
78// is currently used to send the PDF data to the dialog contents.
79
80// In order to send the PDF data to the dialog contents, the flow
81// handler uses a CloudPrintDataSender. It creates one, letting it
82// know the name of the temporary file containing the PDF data, and
83// posts the task of reading the file
84// (CloudPrintDataSender::ReadPrintDataFile) to the file thread. That
85// routine reads in the file, and then hops over to the IO thread to
86// send that data to the dialog contents.
87
88// When the dialog contents are finished (by either being cancelled or
89// hitting the print button), the delegate is notified, and responds
90// that the dialog should be closed, at which point things are torn
91// down and released.
92
93// TODO(scottbyer):
94// http://code.google.com/p/chromium/issues/detail?id=44093 The
95// high-level flow (where the PDF data is generated before even
96// bringing up the dialog) isn't what we want.
97
98
99namespace internal_cloud_print_helpers {
100
[email protected]4baf1c42010-05-18 18:45:25101void CloudPrintService::RegisterPreferences() {
102 DCHECK(profile_);
103 PrefService* pref_service = profile_->GetPrefs();
104 if (pref_service->FindPreference(prefs::kCloudPrintServiceURL))
105 return;
106 std::wstring kDefaultCloudPrintServiceURL(
107 L"http://www.google.com/cloudprint");
108 pref_service->RegisterStringPref(prefs::kCloudPrintServiceURL,
109 kDefaultCloudPrintServiceURL);
110}
111
112// Returns the root service URL for the cloud print service. The
113// default is to point at the Google Cloud Print service. This can be
114// overridden by the command line or by the user preferences.
115GURL CloudPrintService::GetCloudPrintServiceURL() {
116 DCHECK(profile_);
117 RegisterPreferences();
118
119 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
120 GURL cloud_print_service_url = GURL(command_line.GetSwitchValueASCII(
121 switches::kCloudPrintServiceURL));
122 if (cloud_print_service_url.is_empty()) {
123 cloud_print_service_url = GURL(WideToUTF8(
124 profile_->GetPrefs()->GetString(prefs::kCloudPrintServiceURL)));
125 }
126 return cloud_print_service_url;
127}
128
129GURL CloudPrintService::GetCloudPrintServiceDialogURL() {
130 GURL cloud_print_service_url = GetCloudPrintServiceURL();
131 std::string path(cloud_print_service_url.path() + "/client/dialog.html");
132 GURL::Replacements replacements;
133 replacements.SetPathStr(path);
134 GURL cloud_print_dialog_url = cloud_print_service_url.ReplaceComponents(
135 replacements);
136 return cloud_print_dialog_url;
137}
[email protected]73852b8f2010-05-14 00:38:12138
139bool GetRealOrInt(const DictionaryValue& dictionary,
140 const std::wstring& path,
141 double* out_value) {
142 if (!dictionary.GetReal(path, out_value)) {
143 int int_value = 0;
144 if (!dictionary.GetInteger(path, &int_value))
145 return false;
146 *out_value = int_value;
147 }
148 return true;
149}
150
151// From the JSON parsed value, get the entries for the page setup
152// parameters.
153bool GetPageSetupParameters(const std::string& json,
154 ViewMsg_Print_Params& parameters) {
155 scoped_ptr<Value> parsed_value(base::JSONReader::Read(json, false));
156 DLOG_IF(ERROR, (!parsed_value.get() ||
157 !parsed_value->IsType(Value::TYPE_DICTIONARY)))
158 << "PageSetup call didn't have expected contents";
159 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY))
160 return false;
161
162 bool result = true;
163 DictionaryValue* params = static_cast<DictionaryValue*>(parsed_value.get());
164 result &= GetRealOrInt(*params, L"dpi", &parameters.dpi);
165 result &= GetRealOrInt(*params, L"min_shrink", &parameters.min_shrink);
166 result &= GetRealOrInt(*params, L"max_shrink", &parameters.max_shrink);
167 result &= params->GetBoolean(L"selection_only", &parameters.selection_only);
168 return result;
169}
170
171void CloudPrintDataSenderHelper::CallJavascriptFunction(
172 const std::wstring& function_name) {
173 dom_ui_->CallJavascriptFunction(function_name);
174}
175
176void CloudPrintDataSenderHelper::CallJavascriptFunction(
177 const std::wstring& function_name, const Value& arg) {
178 dom_ui_->CallJavascriptFunction(function_name, arg);
179}
180
181void CloudPrintDataSenderHelper::CallJavascriptFunction(
182 const std::wstring& function_name, const Value& arg1, const Value& arg2) {
183 dom_ui_->CallJavascriptFunction(function_name, arg1, arg2);
184}
185
186// Clears out the pointer we're using to communicate. Either routine is
187// potentially expensive enough that stopping whatever is in progress
188// is worth it.
189void CloudPrintDataSender::CancelPrintDataFile() {
190 AutoLock lock(lock_);
191 // We don't own helper, it was passed in to us, so no need to
192 // delete, just let it go.
193 helper_ = NULL;
194}
195
196// Grab the raw PDF file contents and massage them into shape for
197// sending to the dialog contents (and up to the cloud print server)
198// by encoding it and prefixing it with the appropriate mime type.
199// Once that is done, kick off the next part of the task on the IO
200// thread.
201void CloudPrintDataSender::ReadPrintDataFile(const FilePath& path_to_pdf) {
202 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
203 int64 file_size = 0;
204 if (file_util::GetFileSize(path_to_pdf, &file_size) && file_size != 0) {
205 std::string file_data;
206 if (file_size < kuint32max) {
207 file_data.reserve(static_cast<unsigned int>(file_size));
208 } else {
209 DLOG(WARNING) << " print data file too large to reserve space";
210 }
211 if (helper_ && file_util::ReadFileToString(path_to_pdf, &file_data)) {
212 std::string base64_data;
213 base::Base64Encode(file_data, &base64_data);
214 std::string header("data:application/pdf;base64,");
215 base64_data.insert(0, header);
216 scoped_ptr<StringValue> new_data(new StringValue(base64_data));
217 print_data_.swap(new_data);
218 ChromeThread::PostTask(ChromeThread::IO, FROM_HERE,
219 NewRunnableMethod(
220 this,
221 &CloudPrintDataSender::SendPrintDataFile));
222 }
223 }
224}
225
226// We have the data in hand that needs to be pushed into the dialog
227// contents; do so from the IO thread.
228
229// TODO(scottbyer): If the print data ends up being larger than the
230// upload limit (currently 10MB), what we need to do is upload that
231// large data to google docs and set the URL in the printing
232// JavaScript to that location, and make sure it gets deleted when not
233// needed. - 4/1/2010
234void CloudPrintDataSender::SendPrintDataFile() {
235 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
236 AutoLock lock(lock_);
237 if (helper_ && print_data_.get()) {
[email protected]9848c7e2010-06-03 16:06:56238 StringValue title(print_job_title_);
[email protected]73852b8f2010-05-14 00:38:12239
240 // Send the print data to the dialog contents. The JavaScript
241 // function is a preliminary API for prototyping purposes and is
242 // subject to change.
243 const_cast<CloudPrintDataSenderHelper*>(helper_)->CallJavascriptFunction(
244 L"printApp._printDataUrl", *print_data_, title);
245 }
246}
247
248
249void CloudPrintFlowHandler::SetDialogDelegate(
250 CloudPrintHtmlDialogDelegate* delegate) {
251 // Even if setting a new dom_ui, it means any previous task needs
252 // to be cancelled, it's now invalid.
253 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
254 CancelAnyRunningTask();
255 dialog_delegate_ = delegate;
256}
257
258// Cancels any print data sender we have in flight and removes our
259// reference to it, so when the task that is calling it finishes and
260// removes it's reference, it goes away.
261void CloudPrintFlowHandler::CancelAnyRunningTask() {
262 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
263 if (print_data_sender_.get()) {
264 print_data_sender_->CancelPrintDataFile();
265 print_data_sender_ = NULL;
266 }
267}
268
269
270void CloudPrintFlowHandler::RegisterMessages() {
271 if (!dom_ui_)
272 return;
273
274 // TODO(scottbyer) - This is where we will register messages for the
275 // UI JS to use. Needed: Call to update page setup parameters.
276 dom_ui_->RegisterMessageCallback(
277 "ShowDebugger",
278 NewCallback(this, &CloudPrintFlowHandler::HandleShowDebugger));
279 dom_ui_->RegisterMessageCallback(
280 "SendPrintData",
281 NewCallback(this, &CloudPrintFlowHandler::HandleSendPrintData));
282 dom_ui_->RegisterMessageCallback(
283 "SetPageParameters",
284 NewCallback(this, &CloudPrintFlowHandler::HandleSetPageParameters));
285
286 if (dom_ui_->tab_contents()) {
287 // Also, take the opportunity to set some (minimal) additional
288 // script permissions required for the web UI.
289
290 // TODO(scottbyer): learn how to make sure we're talking to the
291 // right web site first.
292 RenderViewHost* rvh = dom_ui_->tab_contents()->render_view_host();
293 if (rvh && rvh->delegate()) {
294 WebPreferences webkit_prefs = rvh->delegate()->GetWebkitPrefs();
295 webkit_prefs.allow_scripts_to_close_windows = true;
296 rvh->UpdateWebPreferences(webkit_prefs);
297 }
298
299 // Register for appropriate notifications, and re-direct the URL
300 // to the real server URL, now that we've gotten an HTML dialog
301 // going.
302 NavigationController* controller = &dom_ui_->tab_contents()->controller();
303 NavigationEntry* pending_entry = controller->pending_entry();
304 if (pending_entry)
[email protected]4baf1c42010-05-18 18:45:25305 pending_entry->set_url(CloudPrintService(
306 dom_ui_->GetProfile()).GetCloudPrintServiceDialogURL());
[email protected]73852b8f2010-05-14 00:38:12307 registrar_.Add(this, NotificationType::LOAD_STOP,
308 Source<NavigationController>(controller));
309 }
310}
311
312void CloudPrintFlowHandler::Observe(NotificationType type,
313 const NotificationSource& source,
314 const NotificationDetails& details) {
315 if (type == NotificationType::LOAD_STOP) {
316 // Choose one or the other. If you need to debug, bring up the
317 // debugger. You can then use the various chrome.send()
318 // registrations above to kick of the various function calls,
319 // including chrome.send("SendPrintData") in the javaScript
320 // console and watch things happen with:
321 // HandleShowDebugger(NULL);
322 HandleSendPrintData(NULL);
323 }
324}
325
326void CloudPrintFlowHandler::HandleShowDebugger(const Value* value) {
327 ShowDebugger();
328}
329
330void CloudPrintFlowHandler::ShowDebugger() {
331 if (dom_ui_) {
332 RenderViewHost* rvh = dom_ui_->tab_contents()->render_view_host();
333 if (rvh)
334 DevToolsManager::GetInstance()->OpenDevToolsWindow(rvh);
335 }
336}
337
338scoped_refptr<CloudPrintDataSender>
339CloudPrintFlowHandler::CreateCloudPrintDataSender() {
340 DCHECK(dom_ui_);
341 print_data_helper_.reset(new CloudPrintDataSenderHelper(dom_ui_));
[email protected]9848c7e2010-06-03 16:06:56342 return new CloudPrintDataSender(print_data_helper_.get(), print_job_title_);
[email protected]73852b8f2010-05-14 00:38:12343}
344
345void CloudPrintFlowHandler::HandleSendPrintData(const Value* value) {
346 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
347 // This will cancel any ReadPrintDataFile() or SendPrintDataFile()
348 // requests in flight (this is anticipation of when setting page
349 // setup parameters becomes asynchronous and may be set while some
350 // data is in flight). Then we can clear out the print data.
351 CancelAnyRunningTask();
352 if (dom_ui_) {
353 print_data_sender_ = CreateCloudPrintDataSender();
354 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
355 NewRunnableMethod(
356 print_data_sender_.get(),
357 &CloudPrintDataSender::ReadPrintDataFile,
358 path_to_pdf_));
359 }
360}
361
362void CloudPrintFlowHandler::HandleSetPageParameters(const Value* value) {
363 std::string json(dom_ui_util::GetJsonResponseFromFirstArgumentInList(value));
364 if (json.empty())
365 return;
366
367 // These are backstop default values - 72 dpi to match the screen,
368 // 8.5x11 inch paper with margins subtracted (1/4 inch top, left,
369 // right and 0.56 bottom), and the min page shrink and max page
370 // shrink values appear all over the place with no explanation.
371
372 // TODO(scottbyer): Get a Linux/ChromeOS edge for PrintSettings
373 // working so that we can get the default values from there. Fix up
374 // PrintWebViewHelper to do the same.
375 const int kDPI = 72;
376 const int kWidth = static_cast<int>((8.5-0.25-0.25)*kDPI);
377 const int kHeight = static_cast<int>((11-0.25-0.56)*kDPI);
378 const double kMinPageShrink = 1.25;
379 const double kMaxPageShrink = 2.0;
380
381 ViewMsg_Print_Params default_settings;
382 default_settings.printable_size = gfx::Size(kWidth, kHeight);
383 default_settings.dpi = kDPI;
384 default_settings.min_shrink = kMinPageShrink;
385 default_settings.max_shrink = kMaxPageShrink;
386 default_settings.desired_dpi = kDPI;
387 default_settings.document_cookie = 0;
388 default_settings.selection_only = false;
389
390 if (!GetPageSetupParameters(json, default_settings)) {
391 NOTREACHED();
392 return;
393 }
394
395 // TODO(scottbyer) - Here is where we would kick the originating
396 // renderer thread with these new parameters in order to get it to
397 // re-generate the PDF and hand it back to us. window.print() is
398 // currently synchronous, so there's a lot of work to do to get to
399 // that point.
400}
401
402CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate(
403 const FilePath& path_to_pdf,
404 int width, int height,
[email protected]9848c7e2010-06-03 16:06:56405 const std::string& json_arguments,
406 const string16& print_job_title)
407 : flow_handler_(new CloudPrintFlowHandler(path_to_pdf, print_job_title)),
[email protected]73852b8f2010-05-14 00:38:12408 owns_flow_handler_(true) {
409 Init(width, height, json_arguments);
410}
411
412CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate(
413 CloudPrintFlowHandler* flow_handler,
414 int width, int height,
415 const std::string& json_arguments)
416 : flow_handler_(flow_handler),
[email protected]18137e02010-05-25 21:10:35417 owns_flow_handler_(true) {
[email protected]73852b8f2010-05-14 00:38:12418 Init(width, height, json_arguments);
419}
420
421void CloudPrintHtmlDialogDelegate::Init(
[email protected]9848c7e2010-06-03 16:06:56422 int width, int height, const std::string& json_arguments) {
[email protected]73852b8f2010-05-14 00:38:12423 // This information is needed to show the dialog HTML content.
424 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
425 std::string cloud_print_url(chrome::kCloudPrintResourcesURL);
426 params_.url = GURL(cloud_print_url);
427 params_.height = height;
428 params_.width = width;
429 params_.json_input = json_arguments;
430
431 flow_handler_->SetDialogDelegate(this);
432}
433
434CloudPrintHtmlDialogDelegate::~CloudPrintHtmlDialogDelegate() {
435 // If the flow_handler_ is about to outlive us because we don't own
436 // it anymore, we need to have it remove it's reference to us.
437 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
438 flow_handler_->SetDialogDelegate(NULL);
439 if (owns_flow_handler_) {
440 delete flow_handler_;
441 }
442}
443
444bool CloudPrintHtmlDialogDelegate::IsDialogModal() const {
445 return true;
446}
447
448std::wstring CloudPrintHtmlDialogDelegate::GetDialogTitle() const {
449 return l10n_util::GetString(IDS_CLOUD_PRINT_TITLE);
450}
451
452GURL CloudPrintHtmlDialogDelegate::GetDialogContentURL() const {
453 return params_.url;
454}
455
456void CloudPrintHtmlDialogDelegate::GetDOMMessageHandlers(
457 std::vector<DOMMessageHandler*>* handlers) const {
458 handlers->push_back(flow_handler_);
459 // We don't own flow_handler_ anymore, but it sticks around until at
460 // least right after OnDialogClosed() is called (and this object is
461 // destroyed).
462 owns_flow_handler_ = false;
463}
464
465void CloudPrintHtmlDialogDelegate::GetDialogSize(gfx::Size* size) const {
466 size->set_width(params_.width);
467 size->set_height(params_.height);
468}
469
470std::string CloudPrintHtmlDialogDelegate::GetDialogArgs() const {
471 return params_.json_input;
472}
473
474void CloudPrintHtmlDialogDelegate::OnDialogClosed(
475 const std::string& json_retval) {
476 delete this;
477}
478
[email protected]18137e02010-05-25 21:10:35479void CloudPrintHtmlDialogDelegate::OnCloseContents(TabContents* source,
480 bool* out_close_dialog) {
481 if (out_close_dialog)
482 *out_close_dialog = true;
483}
484
[email protected]73852b8f2010-05-14 00:38:12485} // end of namespace internal_cloud_print_helpers
486
487// static, called on the IO thread. This is the main entry point into
488// creating the dialog.
489
490// TODO(scottbyer): The signature here will need to change as the
491// workflow through the printing code changes to allow for dynamically
492// changing page setup parameters while the dialog is active.
493void PrintDialogCloud::CreatePrintDialogForPdf(const FilePath& path_to_pdf) {
494 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
495
496 ChromeThread::PostTask(
497 ChromeThread::UI, FROM_HERE,
498 NewRunnableFunction(&PrintDialogCloud::CreateDialogImpl, path_to_pdf));
499}
500
501// static, called from the UI thread.
502void PrintDialogCloud::CreateDialogImpl(const FilePath& path_to_pdf) {
503 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
504 new PrintDialogCloud(path_to_pdf);
505}
506
507// Initialize the print dialog. Called on the UI thread.
508PrintDialogCloud::PrintDialogCloud(const FilePath& path_to_pdf)
509 : browser_(BrowserList::GetLastActive()) {
510
511 // TODO(scottbyer): Verify GAIA login valid, execute GAIA login if not (should
512 // be distilled out of bookmark sync.)
[email protected]9848c7e2010-06-03 16:06:56513 string16 print_job_title;
514 if (browser_ && browser_->GetSelectedTabContents())
515 print_job_title = browser_->GetSelectedTabContents()->GetTitle();
[email protected]73852b8f2010-05-14 00:38:12516
517 // TODO(scottbyer): Get the dialog width, height from the dialog
518 // contents, and take the screen size into account.
519 HtmlDialogUIDelegate* dialog_delegate =
520 new internal_cloud_print_helpers::CloudPrintHtmlDialogDelegate(
[email protected]9848c7e2010-06-03 16:06:56521 path_to_pdf, 500, 400, std::string(), print_job_title);
[email protected]73852b8f2010-05-14 00:38:12522 browser_->BrowserShowHtmlDialog(dialog_delegate, NULL);
523}
524
525PrintDialogCloud::~PrintDialogCloud() {
526}