Disable TabDragController tests that fail with a real compositor.
[chromium-blink-merge.git] / chrome / browser / printing / print_dialog_cloud.cc
blobd3a263631d0605aabd30c50ff3b6a1ed1fd722c0
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/printing/print_dialog_cloud.h"
8 #include "base/base64.h"
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/command_line.h"
12 #include "base/file_util.h"
13 #include "base/json/json_reader.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "chrome/browser/devtools/devtools_window.h"
18 #include "chrome/browser/lifetime/application_lifetime.h"
19 #include "chrome/browser/printing/cloud_print/cloud_print_url.h"
20 #include "chrome/browser/printing/print_dialog_cloud_internal.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_dialogs.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/common/print_messages.h"
27 #include "chrome/common/url_constants.h"
28 #include "components/user_prefs/pref_registry_syncable.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/navigation_controller.h"
31 #include "content/public/browser/navigation_entry.h"
32 #include "content/public/browser/notification_registrar.h"
33 #include "content/public/browser/notification_source.h"
34 #include "content/public/browser/notification_types.h"
35 #include "content/public/browser/render_view_host.h"
36 #include "content/public/browser/web_contents.h"
37 #include "content/public/browser/web_contents_observer.h"
38 #include "content/public/browser/web_contents_view.h"
39 #include "content/public/browser/web_ui.h"
40 #include "content/public/common/frame_navigate_params.h"
41 #include "webkit/common/webpreferences.h"
43 #if defined(USE_AURA)
44 #include "ui/aura/root_window.h"
45 #include "ui/aura/window.h"
46 #endif
48 #if defined(OS_WIN)
49 #include "ui/base/win/foreground_helper.h"
50 #endif
52 // This module implements the UI support in Chrome for cloud printing.
53 // This means hosting a dialog containing HTML/JavaScript and using
54 // the published cloud print user interface integration APIs to get
55 // page setup settings from the dialog contents and provide the
56 // generated print data to the dialog contents for uploading to the
57 // cloud print service.
59 // Currently, the flow between these classes is as follows:
61 // PrintDialogCloud::CreatePrintDialogForFile is called from
62 // resource_message_filter_gtk.cc once the renderer has informed the
63 // renderer host that print data generation into the renderer host provided
64 // temp file has been completed. That call is on the FILE thread.
65 // That, in turn, hops over to the UI thread to create an instance of
66 // PrintDialogCloud.
68 // The constructor for PrintDialogCloud creates a
69 // CloudPrintWebDialogDelegate and asks the current active browser to
70 // show an HTML dialog using that class as the delegate. That class
71 // hands in the kChromeUICloudPrintResourcesURL as the URL to visit. That is
72 // recognized by the GetWebUIFactoryFunction as a signal to create an
73 // ExternalWebDialogUI.
75 // CloudPrintWebDialogDelegate also temporarily owns a
76 // CloudPrintFlowHandler, a class which is responsible for the actual
77 // interactions with the dialog contents, including handing in the
78 // print data and getting any page setup parameters that the dialog
79 // contents provides. As part of bringing up the dialog,
80 // WebDialogUI::RenderViewCreated is called (an override of
81 // WebUI::RenderViewCreated). That routine, in turn, calls the
82 // delegate's GetWebUIMessageHandlers routine, at which point the
83 // ownership of the CloudPrintFlowHandler is handed over. A pointer
84 // to the flow handler is kept to facilitate communication back and
85 // forth between the two classes.
87 // The WebUI continues dialog bring-up, calling
88 // CloudPrintFlowHandler::RegisterMessages. This is where the
89 // additional object model capabilities are registered for the dialog
90 // contents to use. It is also at this time that capabilities for the
91 // dialog contents are adjusted to allow the dialog contents to close
92 // the window. In addition, the pending URL is redirected to the
93 // actual cloud print service URL. The flow controller also registers
94 // for notification of when the dialog contents finish loading, which
95 // is currently used to send the data to the dialog contents.
97 // In order to send the data to the dialog contents, the flow
98 // handler uses a CloudPrintDataSender. It creates one, letting it
99 // know the name of the temporary file containing the data, and
100 // posts the task of reading the file
101 // (CloudPrintDataSender::ReadPrintDataFile) to the file thread. That
102 // routine reads in the file, and then hops over to the IO thread to
103 // send that data to the dialog contents.
105 // When the dialog contents are finished (by either being cancelled or
106 // hitting the print button), the delegate is notified, and responds
107 // that the dialog should be closed, at which point things are torn
108 // down and released.
110 using content::BrowserThread;
111 using content::NavigationController;
112 using content::NavigationEntry;
113 using content::RenderViewHost;
114 using content::WebContents;
115 using content::WebUIMessageHandler;
116 using ui::WebDialogDelegate;
118 namespace {
120 const int kDefaultWidth = 912;
121 const int kDefaultHeight = 633;
123 bool IsSimilarUrl(const GURL& url, const GURL& cloud_print_url) {
124 return url.host() == cloud_print_url.host() &&
125 StartsWithASCII(url.path(), cloud_print_url.path(), false) &&
126 url.scheme() == cloud_print_url.scheme();
129 class SignInObserver : public content::WebContentsObserver {
130 public:
131 SignInObserver(content::WebContents* web_contents,
132 GURL cloud_print_url,
133 const base::Closure& callback)
134 : WebContentsObserver(web_contents),
135 cloud_print_url_(cloud_print_url),
136 callback_(callback),
137 weak_ptr_factory_(this) {
140 private:
141 // Overridden from content::WebContentsObserver:
142 virtual void DidNavigateMainFrame(
143 const content::LoadCommittedDetails& details,
144 const content::FrameNavigateParams& params) OVERRIDE {
145 if (IsSimilarUrl(params.url, cloud_print_url_)) {
146 base::MessageLoop::current()->PostTask(
147 FROM_HERE,
148 base::Bind(&SignInObserver::OnSignIn,
149 weak_ptr_factory_.GetWeakPtr()));
153 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE {
154 delete this;
157 void OnSignIn() {
158 callback_.Run();
159 if (web_contents())
160 web_contents()->Close();
163 GURL cloud_print_url_;
164 base::Closure callback_;
165 base::WeakPtrFactory<SignInObserver> weak_ptr_factory_;
167 DISALLOW_COPY_AND_ASSIGN(SignInObserver);
170 } // namespace
172 namespace internal_cloud_print_helpers {
174 // From the JSON parsed value, get the entries for the page setup
175 // parameters.
176 bool GetPageSetupParameters(const std::string& json,
177 PrintMsg_Print_Params& parameters) {
178 scoped_ptr<base::Value> parsed_value(base::JSONReader::Read(json));
179 DLOG_IF(ERROR, (!parsed_value.get() ||
180 !parsed_value->IsType(base::Value::TYPE_DICTIONARY)))
181 << "PageSetup call didn't have expected contents";
182 if (!parsed_value.get() ||
183 !parsed_value->IsType(base::Value::TYPE_DICTIONARY)) {
184 return false;
187 bool result = true;
188 base::DictionaryValue* params =
189 static_cast<base::DictionaryValue*>(parsed_value.get());
190 result &= params->GetDouble("dpi", &parameters.dpi);
191 result &= params->GetDouble("min_shrink", &parameters.min_shrink);
192 result &= params->GetDouble("max_shrink", &parameters.max_shrink);
193 result &= params->GetBoolean("selection_only", &parameters.selection_only);
194 return result;
197 base::string16 GetSwitchValueString16(const CommandLine& command_line,
198 const char* switchName) {
199 #if defined(OS_WIN)
200 return command_line.GetSwitchValueNative(switchName);
201 #elif defined(OS_POSIX)
202 // POSIX Command line string types are different.
203 CommandLine::StringType native_switch_val;
204 native_switch_val = command_line.GetSwitchValueASCII(switchName);
205 // Convert the ASCII string to UTF16 to prepare to pass.
206 return base::ASCIIToUTF16(native_switch_val);
207 #endif
210 void CloudPrintDataSenderHelper::CallJavascriptFunction(
211 const std::string& function_name,
212 const base::Value& arg1,
213 const base::Value& arg2) {
214 web_ui_->CallJavascriptFunction(function_name, arg1, arg2);
217 // Clears out the pointer we're using to communicate. Either routine is
218 // potentially expensive enough that stopping whatever is in progress
219 // is worth it.
220 void CloudPrintDataSender::CancelPrintDataFile() {
221 base::AutoLock lock(lock_);
222 // We don't own helper, it was passed in to us, so no need to
223 // delete, just let it go.
224 helper_ = NULL;
227 CloudPrintDataSender::CloudPrintDataSender(
228 CloudPrintDataSenderHelper* helper,
229 const base::string16& print_job_title,
230 const base::string16& print_ticket,
231 const std::string& file_type,
232 const base::RefCountedMemory* data)
233 : helper_(helper),
234 print_job_title_(print_job_title),
235 print_ticket_(print_ticket),
236 file_type_(file_type),
237 data_(data) {
240 CloudPrintDataSender::~CloudPrintDataSender() {}
242 // We have the data in hand that needs to be pushed into the dialog
243 // contents; do so from the IO thread.
245 // TODO(scottbyer): If the print data ends up being larger than the
246 // upload limit (currently 10MB), what we need to do is upload that
247 // large data to google docs and set the URL in the printing
248 // JavaScript to that location, and make sure it gets deleted when not
249 // needed. - 4/1/2010
250 void CloudPrintDataSender::SendPrintData() {
251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
252 if (!data_.get() || !data_->size())
253 return;
255 std::string base64_data;
256 base::Base64Encode(
257 base::StringPiece(reinterpret_cast<const char*>(data_->front()),
258 data_->size()),
259 &base64_data);
260 std::string header("data:");
261 header.append(file_type_);
262 header.append(";base64,");
263 base64_data.insert(0, header);
265 base::AutoLock lock(lock_);
266 if (helper_) {
267 base::StringValue title(print_job_title_);
268 base::StringValue ticket(print_ticket_);
269 // TODO(abodenha): Change Javascript call to pass in print ticket
270 // after server side support is added. Add test for it.
272 // Send the print data to the dialog contents. The JavaScript
273 // function is a preliminary API for prototyping purposes and is
274 // subject to change.
275 helper_->CallJavascriptFunction(
276 "printApp._printDataUrl", base::StringValue(base64_data), title);
281 CloudPrintFlowHandler::CloudPrintFlowHandler(
282 const base::RefCountedMemory* data,
283 const base::string16& print_job_title,
284 const base::string16& print_ticket,
285 const std::string& file_type)
286 : dialog_delegate_(NULL),
287 data_(data),
288 print_job_title_(print_job_title),
289 print_ticket_(print_ticket),
290 file_type_(file_type) {
293 CloudPrintFlowHandler::~CloudPrintFlowHandler() {
294 // This will also cancel any task in flight.
295 CancelAnyRunningTask();
299 void CloudPrintFlowHandler::SetDialogDelegate(
300 CloudPrintWebDialogDelegate* delegate) {
301 // Even if setting a new WebUI, it means any previous task needs
302 // to be canceled, its now invalid.
303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
304 CancelAnyRunningTask();
305 dialog_delegate_ = delegate;
308 // Cancels any print data sender we have in flight and removes our
309 // reference to it, so when the task that is calling it finishes and
310 // removes its reference, it goes away.
311 void CloudPrintFlowHandler::CancelAnyRunningTask() {
312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
313 if (print_data_sender_.get()) {
314 print_data_sender_->CancelPrintDataFile();
315 print_data_sender_ = NULL;
319 void CloudPrintFlowHandler::RegisterMessages() {
320 // TODO(scottbyer) - This is where we will register messages for the
321 // UI JS to use. Needed: Call to update page setup parameters.
322 web_ui()->RegisterMessageCallback("ShowDebugger",
323 base::Bind(&CloudPrintFlowHandler::HandleShowDebugger,
324 base::Unretained(this)));
325 web_ui()->RegisterMessageCallback("SendPrintData",
326 base::Bind(&CloudPrintFlowHandler::HandleSendPrintData,
327 base::Unretained(this)));
328 web_ui()->RegisterMessageCallback("SetPageParameters",
329 base::Bind(&CloudPrintFlowHandler::HandleSetPageParameters,
330 base::Unretained(this)));
332 // Register for appropriate notifications, and re-direct the URL
333 // to the real server URL, now that we've gotten an HTML dialog
334 // going.
335 NavigationController* controller =
336 &web_ui()->GetWebContents()->GetController();
337 NavigationEntry* pending_entry = controller->GetPendingEntry();
338 if (pending_entry) {
339 Profile* profile = Profile::FromWebUI(web_ui());
340 pending_entry->SetURL(
341 CloudPrintURL(profile).GetCloudPrintServiceDialogURL());
343 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
344 content::Source<NavigationController>(controller));
345 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
346 content::Source<NavigationController>(controller));
349 void CloudPrintFlowHandler::Observe(
350 int type,
351 const content::NotificationSource& source,
352 const content::NotificationDetails& details) {
353 switch (type) {
354 case content::NOTIFICATION_LOAD_STOP: {
355 GURL url = web_ui()->GetWebContents()->GetURL();
356 if (IsCloudPrintDialogUrl(url)) {
357 // Take the opportunity to set some (minimal) additional
358 // script permissions required for the web UI.
359 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
360 if (rvh) {
361 WebPreferences webkit_prefs = rvh->GetWebkitPreferences();
362 webkit_prefs.allow_scripts_to_close_windows = true;
363 rvh->UpdateWebkitPreferences(webkit_prefs);
364 } else {
365 NOTREACHED();
367 // Choose one or the other. If you need to debug, bring up the
368 // debugger. You can then use the various chrome.send()
369 // registrations above to kick of the various function calls,
370 // including chrome.send("SendPrintData") in the javaScript
371 // console and watch things happen with:
372 // HandleShowDebugger(NULL);
373 HandleSendPrintData(NULL);
375 break;
380 void CloudPrintFlowHandler::HandleShowDebugger(const base::ListValue* args) {
381 ShowDebugger();
384 void CloudPrintFlowHandler::ShowDebugger() {
385 if (web_ui()) {
386 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
387 if (rvh)
388 DevToolsWindow::OpenDevToolsWindow(rvh);
392 scoped_refptr<CloudPrintDataSender>
393 CloudPrintFlowHandler::CreateCloudPrintDataSender() {
394 DCHECK(web_ui());
395 print_data_helper_.reset(new CloudPrintDataSenderHelper(web_ui()));
396 scoped_refptr<CloudPrintDataSender> sender(
397 new CloudPrintDataSender(print_data_helper_.get(),
398 print_job_title_,
399 print_ticket_,
400 file_type_,
401 data_.get()));
402 return sender;
405 void CloudPrintFlowHandler::HandleSendPrintData(const base::ListValue* args) {
406 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
407 // This will cancel any ReadPrintDataFile() or SendPrintDataFile()
408 // requests in flight (this is anticipation of when setting page
409 // setup parameters becomes asynchronous and may be set while some
410 // data is in flight). Then we can clear out the print data.
411 CancelAnyRunningTask();
412 if (web_ui()) {
413 print_data_sender_ = CreateCloudPrintDataSender();
414 BrowserThread::PostTask(
415 BrowserThread::IO, FROM_HERE,
416 base::Bind(&CloudPrintDataSender::SendPrintData, print_data_sender_));
420 void CloudPrintFlowHandler::HandleSetPageParameters(
421 const base::ListValue* args) {
422 std::string json;
423 bool ret = args->GetString(0, &json);
424 if (!ret || json.empty()) {
425 NOTREACHED() << "Empty json string";
426 return;
429 // These are backstop default values - 72 dpi to match the screen,
430 // 8.5x11 inch paper with margins subtracted (1/4 inch top, left,
431 // right and 0.56 bottom), and the min page shrink and max page
432 // shrink values appear all over the place with no explanation.
434 // TODO(scottbyer): Get a Linux/ChromeOS edge for PrintSettings
435 // working so that we can get the default values from there. Fix up
436 // PrintWebViewHelper to do the same.
437 const int kDPI = 72;
438 const int kWidth = static_cast<int>((8.5-0.25-0.25)*kDPI);
439 const int kHeight = static_cast<int>((11-0.25-0.56)*kDPI);
440 const double kMinPageShrink = 1.25;
441 const double kMaxPageShrink = 2.0;
443 PrintMsg_Print_Params default_settings;
444 default_settings.content_size = gfx::Size(kWidth, kHeight);
445 default_settings.printable_area = gfx::Rect(0, 0, kWidth, kHeight);
446 default_settings.dpi = kDPI;
447 default_settings.min_shrink = kMinPageShrink;
448 default_settings.max_shrink = kMaxPageShrink;
449 default_settings.desired_dpi = kDPI;
450 default_settings.document_cookie = 0;
451 default_settings.selection_only = false;
452 default_settings.preview_request_id = 0;
453 default_settings.is_first_request = true;
454 default_settings.print_to_pdf = false;
456 if (!GetPageSetupParameters(json, default_settings)) {
457 NOTREACHED();
458 return;
461 // TODO(scottbyer) - Here is where we would kick the originating
462 // renderer thread with these new parameters in order to get it to
463 // re-generate the PDF data and hand it back to us. window.print() is
464 // currently synchronous, so there's a lot of work to do to get to
465 // that point.
468 void CloudPrintFlowHandler::StoreDialogClientSize() const {
469 if (web_ui() && web_ui()->GetWebContents() &&
470 web_ui()->GetWebContents()->GetView()) {
471 gfx::Size size = web_ui()->GetWebContents()->GetView()->GetContainerSize();
472 Profile* profile = Profile::FromWebUI(web_ui());
473 profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogWidth,
474 size.width());
475 profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogHeight,
476 size.height());
480 bool CloudPrintFlowHandler::IsCloudPrintDialogUrl(const GURL& url) {
481 GURL cloud_print_url =
482 CloudPrintURL(Profile::FromWebUI(web_ui())).GetCloudPrintServiceURL();
483 return IsSimilarUrl(url, cloud_print_url);
486 CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate(
487 content::BrowserContext* browser_context,
488 gfx::NativeWindow modal_parent,
489 const base::RefCountedMemory* data,
490 const std::string& json_arguments,
491 const base::string16& print_job_title,
492 const base::string16& print_ticket,
493 const std::string& file_type)
494 : flow_handler_(
495 new CloudPrintFlowHandler(data, print_job_title, print_ticket,
496 file_type)),
497 modal_parent_(modal_parent),
498 owns_flow_handler_(true),
499 keep_alive_when_non_modal_(true) {
500 Init(browser_context, json_arguments);
503 // For unit testing.
504 CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate(
505 CloudPrintFlowHandler* flow_handler,
506 const std::string& json_arguments)
507 : flow_handler_(flow_handler),
508 modal_parent_(NULL),
509 owns_flow_handler_(true),
510 keep_alive_when_non_modal_(false) {
511 Init(NULL, json_arguments);
514 // Returns the persisted width/height for the print dialog.
515 void GetDialogWidthAndHeightFromPrefs(content::BrowserContext* browser_context,
516 int* width,
517 int* height) {
518 if (!browser_context) {
519 *width = kDefaultWidth;
520 *height = kDefaultHeight;
521 return;
524 PrefService* prefs = Profile::FromBrowserContext(browser_context)->GetPrefs();
525 *width = prefs->GetInteger(prefs::kCloudPrintDialogWidth);
526 *height = prefs->GetInteger(prefs::kCloudPrintDialogHeight);
529 void CloudPrintWebDialogDelegate::Init(content::BrowserContext* browser_context,
530 const std::string& json_arguments) {
531 // This information is needed to show the dialog HTML content.
532 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
534 params_.url = GURL(chrome::kChromeUICloudPrintResourcesURL);
535 GetDialogWidthAndHeightFromPrefs(browser_context,
536 &params_.width,
537 &params_.height);
538 params_.json_input = json_arguments;
540 flow_handler_->SetDialogDelegate(this);
541 // If we're not modal we can show the dialog with no browser.
542 // We need this to keep Chrome alive while our dialog is up.
543 if (!modal_parent_ && keep_alive_when_non_modal_)
544 chrome::StartKeepAlive();
547 CloudPrintWebDialogDelegate::~CloudPrintWebDialogDelegate() {
548 // If the flow_handler_ is about to outlive us because we don't own
549 // it anymore, we need to have it remove its reference to us.
550 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
551 flow_handler_->SetDialogDelegate(NULL);
552 if (owns_flow_handler_) {
553 delete flow_handler_;
557 ui::ModalType CloudPrintWebDialogDelegate::GetDialogModalType() const {
558 return modal_parent_ ? ui::MODAL_TYPE_WINDOW : ui::MODAL_TYPE_NONE;
561 base::string16 CloudPrintWebDialogDelegate::GetDialogTitle() const {
562 return base::string16();
565 GURL CloudPrintWebDialogDelegate::GetDialogContentURL() const {
566 return params_.url;
569 void CloudPrintWebDialogDelegate::GetWebUIMessageHandlers(
570 std::vector<WebUIMessageHandler*>* handlers) const {
571 handlers->push_back(flow_handler_);
572 // We don't own flow_handler_ anymore, but it sticks around until at
573 // least right after OnDialogClosed() is called (and this object is
574 // destroyed).
575 owns_flow_handler_ = false;
578 void CloudPrintWebDialogDelegate::GetDialogSize(gfx::Size* size) const {
579 size->set_width(params_.width);
580 size->set_height(params_.height);
583 std::string CloudPrintWebDialogDelegate::GetDialogArgs() const {
584 return params_.json_input;
587 void CloudPrintWebDialogDelegate::OnDialogClosed(
588 const std::string& json_retval) {
589 // Get the final dialog size and store it.
590 flow_handler_->StoreDialogClientSize();
592 // If we're modal we can show the dialog with no browser.
593 // End the keep-alive so that Chrome can exit.
594 if (!modal_parent_ && keep_alive_when_non_modal_) {
595 // Post to prevent recursive call tho this function.
596 base::MessageLoop::current()->PostTask(FROM_HERE,
597 base::Bind(&chrome::EndKeepAlive));
599 delete this;
602 void CloudPrintWebDialogDelegate::OnCloseContents(WebContents* source,
603 bool* out_close_dialog) {
604 if (out_close_dialog)
605 *out_close_dialog = true;
608 bool CloudPrintWebDialogDelegate::ShouldShowDialogTitle() const {
609 return false;
612 bool CloudPrintWebDialogDelegate::HandleContextMenu(
613 const content::ContextMenuParams& params) {
614 return true;
617 // Called from the UI thread, starts up the dialog.
618 void CreateDialogImpl(content::BrowserContext* browser_context,
619 gfx::NativeWindow modal_parent,
620 const base::RefCountedMemory* data,
621 const base::string16& print_job_title,
622 const base::string16& print_ticket,
623 const std::string& file_type) {
624 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
625 WebDialogDelegate* dialog_delegate =
626 new internal_cloud_print_helpers::CloudPrintWebDialogDelegate(
627 browser_context, modal_parent, data, std::string(), print_job_title,
628 print_ticket, file_type);
629 #if defined(OS_WIN)
630 gfx::NativeWindow window =
631 #endif
632 chrome::ShowWebDialog(modal_parent,
633 Profile::FromBrowserContext(browser_context),
634 dialog_delegate);
635 #if defined(OS_WIN)
636 if (window) {
637 HWND dialog_handle;
638 #if defined(USE_AURA)
639 dialog_handle = window->GetDispatcher()->host()->GetAcceleratedWidget();
640 #else
641 dialog_handle = window;
642 #endif
643 if (::GetForegroundWindow() != dialog_handle) {
644 ui::ForegroundHelper::SetForeground(dialog_handle);
647 #endif
650 void CreateDialogForFileImpl(content::BrowserContext* browser_context,
651 gfx::NativeWindow modal_parent,
652 const base::FilePath& path_to_file,
653 const base::string16& print_job_title,
654 const base::string16& print_ticket,
655 const std::string& file_type,
656 bool delete_on_close) {
657 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
658 scoped_refptr<base::RefCountedMemory> data;
659 int64 file_size = 0;
660 if (base::GetFileSize(path_to_file, &file_size) && file_size != 0) {
661 std::string file_data;
662 if (file_size < kuint32max) {
663 file_data.reserve(static_cast<unsigned int>(file_size));
664 } else {
665 DLOG(WARNING) << " print data file too large to reserve space";
667 if (base::ReadFileToString(path_to_file, &file_data)) {
668 data = base::RefCountedString::TakeString(&file_data);
671 // Proceed even for empty data to simplify testing.
672 BrowserThread::PostTask(
673 BrowserThread::UI, FROM_HERE,
674 base::Bind(&print_dialog_cloud::CreatePrintDialogForBytes,
675 browser_context, modal_parent, data, print_job_title,
676 print_ticket, file_type));
677 if (delete_on_close)
678 base::DeleteFile(path_to_file, false);
681 } // namespace internal_cloud_print_helpers
683 namespace print_dialog_cloud {
685 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
686 registry->RegisterIntegerPref(
687 prefs::kCloudPrintDialogWidth,
688 kDefaultWidth,
689 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
690 registry->RegisterIntegerPref(
691 prefs::kCloudPrintDialogHeight,
692 kDefaultHeight,
693 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
696 // Called on the FILE or UI thread. This is the main entry point into creating
697 // the dialog.
699 void CreatePrintDialogForFile(content::BrowserContext* browser_context,
700 gfx::NativeWindow modal_parent,
701 const base::FilePath& path_to_file,
702 const base::string16& print_job_title,
703 const base::string16& print_ticket,
704 const std::string& file_type,
705 bool delete_on_close) {
706 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) ||
707 BrowserThread::CurrentlyOn(BrowserThread::UI));
708 BrowserThread::PostTask(
709 BrowserThread::FILE, FROM_HERE,
710 base::Bind(&internal_cloud_print_helpers::CreateDialogForFileImpl,
711 browser_context, modal_parent, path_to_file, print_job_title,
712 print_ticket, file_type, delete_on_close));
715 void CreateCloudPrintSigninTab(Browser* browser,
716 const base::Closure& callback) {
717 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
718 CloudPrintURL cp_url(browser->profile());
719 content::WebContents* web_contents =
720 browser->OpenURL(
721 content::OpenURLParams(cp_url.GetCloudPrintSigninURL(),
722 content::Referrer(), NEW_FOREGROUND_TAB,
723 content::PAGE_TRANSITION_AUTO_BOOKMARK,
724 false));
725 new SignInObserver(web_contents, cp_url.GetCloudPrintServiceURL(), callback);
728 void CreatePrintDialogForBytes(content::BrowserContext* browser_context,
729 gfx::NativeWindow modal_parent,
730 const base::RefCountedMemory* data,
731 const base::string16& print_job_title,
732 const base::string16& print_ticket,
733 const std::string& file_type) {
734 internal_cloud_print_helpers::CreateDialogImpl(browser_context, modal_parent,
735 data, print_job_title,
736 print_ticket, file_type);
739 bool CreatePrintDialogFromCommandLine(Profile* profile,
740 const CommandLine& command_line) {
741 DCHECK(command_line.HasSwitch(switches::kCloudPrintFile));
742 if (!command_line.GetSwitchValuePath(switches::kCloudPrintFile).empty()) {
743 base::FilePath cloud_print_file;
744 cloud_print_file =
745 command_line.GetSwitchValuePath(switches::kCloudPrintFile);
746 if (!cloud_print_file.empty()) {
747 base::string16 print_job_title;
748 base::string16 print_job_print_ticket;
749 if (command_line.HasSwitch(switches::kCloudPrintJobTitle)) {
750 print_job_title =
751 internal_cloud_print_helpers::GetSwitchValueString16(
752 command_line, switches::kCloudPrintJobTitle);
754 if (command_line.HasSwitch(switches::kCloudPrintPrintTicket)) {
755 print_job_print_ticket =
756 internal_cloud_print_helpers::GetSwitchValueString16(
757 command_line, switches::kCloudPrintPrintTicket);
759 std::string file_type = "application/pdf";
760 if (command_line.HasSwitch(switches::kCloudPrintFileType)) {
761 file_type = command_line.GetSwitchValueASCII(
762 switches::kCloudPrintFileType);
765 bool delete_on_close = CommandLine::ForCurrentProcess()->HasSwitch(
766 switches::kCloudPrintDeleteFile);
768 print_dialog_cloud::CreatePrintDialogForFile(
769 profile,
770 NULL,
771 cloud_print_file,
772 print_job_title,
773 print_job_print_ticket,
774 file_type,
775 delete_on_close);
776 return true;
779 return false;
782 } // namespace print_dialog_cloud