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