Home | History | Annotate | Download | only in printing
      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   virtual 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   virtual void WebContentsDestroyed() OVERRIDE {
    158     delete this;
    159   }
    160 
    161   void OnSignIn() {
    162     callback_.Run();
    163     if (web_contents())
    164       web_contents()->Close();
    165   }
    166 
    167   GURL cloud_print_url_;
    168   base::Closure callback_;
    169   base::WeakPtrFactory<SignInObserver> weak_ptr_factory_;
    170 
    171   DISALLOW_COPY_AND_ASSIGN(SignInObserver);
    172 };
    173 
    174 }  // namespace
    175 
    176 namespace internal_cloud_print_helpers {
    177 
    178 // From the JSON parsed value, get the entries for the page setup
    179 // parameters.
    180 bool GetPageSetupParameters(const std::string& json,
    181                             PrintMsg_Print_Params& parameters) {
    182   scoped_ptr<base::Value> parsed_value(base::JSONReader::Read(json));
    183   DLOG_IF(ERROR, (!parsed_value.get() ||
    184                   !parsed_value->IsType(base::Value::TYPE_DICTIONARY)))
    185       << "PageSetup call didn't have expected contents";
    186   if (!parsed_value.get() ||
    187       !parsed_value->IsType(base::Value::TYPE_DICTIONARY)) {
    188     return false;
    189   }
    190 
    191   bool result = true;
    192   base::DictionaryValue* params =
    193       static_cast<base::DictionaryValue*>(parsed_value.get());
    194   result &= params->GetDouble("dpi", &parameters.dpi);
    195   result &= params->GetDouble("min_shrink", &parameters.min_shrink);
    196   result &= params->GetDouble("max_shrink", &parameters.max_shrink);
    197   result &= params->GetBoolean("selection_only", &parameters.selection_only);
    198   return result;
    199 }
    200 
    201 base::string16 GetSwitchValueString16(const CommandLine& command_line,
    202                                       const char* switchName) {
    203 #if defined(OS_WIN)
    204   return command_line.GetSwitchValueNative(switchName);
    205 #elif defined(OS_POSIX)
    206   // POSIX Command line string types are different.
    207   CommandLine::StringType native_switch_val;
    208   native_switch_val = command_line.GetSwitchValueASCII(switchName);
    209   // Convert the ASCII string to UTF16 to prepare to pass.
    210   return base::ASCIIToUTF16(native_switch_val);
    211 #endif
    212 }
    213 
    214 void CloudPrintDataSenderHelper::CallJavascriptFunction(
    215     const std::string& function_name,
    216     const base::Value& arg1,
    217     const base::Value& arg2) {
    218   web_ui_->CallJavascriptFunction(function_name, arg1, arg2);
    219 }
    220 
    221 // Clears out the pointer we're using to communicate.  Either routine is
    222 // potentially expensive enough that stopping whatever is in progress
    223 // is worth it.
    224 void CloudPrintDataSender::CancelPrintDataFile() {
    225   base::AutoLock lock(lock_);
    226   // We don't own helper, it was passed in to us, so no need to
    227   // delete, just let it go.
    228   helper_ = NULL;
    229 }
    230 
    231 CloudPrintDataSender::CloudPrintDataSender(
    232     CloudPrintDataSenderHelper* helper,
    233     const base::string16& print_job_title,
    234     const base::string16& print_ticket,
    235     const std::string& file_type,
    236     const base::RefCountedMemory* data)
    237     : helper_(helper),
    238       print_job_title_(print_job_title),
    239       print_ticket_(print_ticket),
    240       file_type_(file_type),
    241       data_(data) {
    242 }
    243 
    244 CloudPrintDataSender::~CloudPrintDataSender() {}
    245 
    246 // We have the data in hand that needs to be pushed into the dialog
    247 // contents; do so from the IO thread.
    248 
    249 // TODO(scottbyer): If the print data ends up being larger than the
    250 // upload limit (currently 10MB), what we need to do is upload that
    251 // large data to google docs and set the URL in the printing
    252 // JavaScript to that location, and make sure it gets deleted when not
    253 // needed. - 4/1/2010
    254 void CloudPrintDataSender::SendPrintData() {
    255   DCHECK_CURRENTLY_ON(BrowserThread::IO);
    256   if (!data_.get() || !data_->size())
    257     return;
    258 
    259   std::string base64_data;
    260   base::Base64Encode(
    261       base::StringPiece(data_->front_as<char>(), data_->size()),
    262       &base64_data);
    263   std::string header("data:");
    264   header.append(file_type_);
    265   header.append(";base64,");
    266   base64_data.insert(0, header);
    267 
    268   base::AutoLock lock(lock_);
    269   if (helper_) {
    270     base::StringValue title(print_job_title_);
    271     base::StringValue ticket(print_ticket_);
    272     // TODO(abodenha): Change Javascript call to pass in print ticket
    273     // after server side support is added. Add test for it.
    274 
    275     // Send the print data to the dialog contents.  The JavaScript
    276     // function is a preliminary API for prototyping purposes and is
    277     // subject to change.
    278     helper_->CallJavascriptFunction(
    279         "printApp._printDataUrl", base::StringValue(base64_data), title);
    280   }
    281 }
    282 
    283 
    284 CloudPrintFlowHandler::CloudPrintFlowHandler(
    285     const base::RefCountedMemory* data,
    286     const base::string16& print_job_title,
    287     const base::string16& print_ticket,
    288     const std::string& file_type)
    289     : dialog_delegate_(NULL),
    290       data_(data),
    291       print_job_title_(print_job_title),
    292       print_ticket_(print_ticket),
    293       file_type_(file_type) {
    294 }
    295 
    296 CloudPrintFlowHandler::~CloudPrintFlowHandler() {
    297   // This will also cancel any task in flight.
    298   CancelAnyRunningTask();
    299 }
    300 
    301 
    302 void CloudPrintFlowHandler::SetDialogDelegate(
    303     CloudPrintWebDialogDelegate* delegate) {
    304   // Even if setting a new WebUI, it means any previous task needs
    305   // to be canceled, its now invalid.
    306   DCHECK_CURRENTLY_ON(BrowserThread::UI);
    307   CancelAnyRunningTask();
    308   dialog_delegate_ = delegate;
    309 }
    310 
    311 // Cancels any print data sender we have in flight and removes our
    312 // reference to it, so when the task that is calling it finishes and
    313 // removes its reference, it goes away.
    314 void CloudPrintFlowHandler::CancelAnyRunningTask() {
    315   DCHECK_CURRENTLY_ON(BrowserThread::UI);
    316   if (print_data_sender_.get()) {
    317     print_data_sender_->CancelPrintDataFile();
    318     print_data_sender_ = NULL;
    319   }
    320 }
    321 
    322 void CloudPrintFlowHandler::RegisterMessages() {
    323   // TODO(scottbyer) - This is where we will register messages for the
    324   // UI JS to use.  Needed: Call to update page setup parameters.
    325   web_ui()->RegisterMessageCallback("ShowDebugger",
    326       base::Bind(&CloudPrintFlowHandler::HandleShowDebugger,
    327                  base::Unretained(this)));
    328   web_ui()->RegisterMessageCallback("SendPrintData",
    329       base::Bind(&CloudPrintFlowHandler::HandleSendPrintData,
    330                  base::Unretained(this)));
    331   web_ui()->RegisterMessageCallback("SetPageParameters",
    332       base::Bind(&CloudPrintFlowHandler::HandleSetPageParameters,
    333                  base::Unretained(this)));
    334 
    335   // Register for appropriate notifications, and re-direct the URL
    336   // to the real server URL, now that we've gotten an HTML dialog
    337   // going.
    338   NavigationController* controller =
    339       &web_ui()->GetWebContents()->GetController();
    340   NavigationEntry* pending_entry = controller->GetPendingEntry();
    341   if (pending_entry) {
    342     pending_entry->SetURL(google_util::AppendGoogleLocaleParam(
    343         cloud_devices::GetCloudPrintRelativeURL("client/dialog.html"),
    344         g_browser_process->GetApplicationLocale()));
    345   }
    346   registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
    347                  content::Source<NavigationController>(controller));
    348   registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
    349                  content::Source<NavigationController>(controller));
    350 }
    351 
    352 void CloudPrintFlowHandler::Observe(
    353     int type,
    354     const content::NotificationSource& source,
    355     const content::NotificationDetails& details) {
    356   switch (type) {
    357     case content::NOTIFICATION_LOAD_STOP: {
    358       GURL url = web_ui()->GetWebContents()->GetURL();
    359       if (IsCloudPrintDialogUrl(url)) {
    360         // Take the opportunity to set some (minimal) additional
    361         // script permissions required for the web UI.
    362         RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
    363         if (rvh) {
    364           // TODO(chrishtr): this is wrong. allow_scripts_to_close_windows will
    365           // be reset the next time a preference changes.
    366           WebPreferences webkit_prefs = rvh->GetWebkitPreferences();
    367           webkit_prefs.allow_scripts_to_close_windows = true;
    368           rvh->UpdateWebkitPreferences(webkit_prefs);
    369         } else {
    370           NOTREACHED();
    371         }
    372         // Choose one or the other.  If you need to debug, bring up the
    373         // debugger.  You can then use the various chrome.send()
    374         // registrations above to kick of the various function calls,
    375         // including chrome.send("SendPrintData") in the javaScript
    376         // console and watch things happen with:
    377         // HandleShowDebugger(NULL);
    378         HandleSendPrintData(NULL);
    379       }
    380       break;
    381     }
    382   }
    383 }
    384 
    385 void CloudPrintFlowHandler::HandleShowDebugger(const base::ListValue* args) {
    386   ShowDebugger();
    387 }
    388 
    389 void CloudPrintFlowHandler::ShowDebugger() {
    390   if (web_ui()) {
    391     WebContents* web_contents = web_ui()->GetWebContents();
    392     if (web_contents)
    393       DevToolsWindow::OpenDevToolsWindow(web_contents);
    394   }
    395 }
    396 
    397 scoped_refptr<CloudPrintDataSender>
    398 CloudPrintFlowHandler::CreateCloudPrintDataSender() {
    399   DCHECK(web_ui());
    400   print_data_helper_.reset(new CloudPrintDataSenderHelper(web_ui()));
    401   scoped_refptr<CloudPrintDataSender> sender(
    402       new CloudPrintDataSender(print_data_helper_.get(),
    403                                print_job_title_,
    404                                print_ticket_,
    405                                file_type_,
    406                                data_.get()));
    407   return sender;
    408 }
    409 
    410 void CloudPrintFlowHandler::HandleSendPrintData(const base::ListValue* args) {
    411   DCHECK_CURRENTLY_ON(BrowserThread::UI);
    412   // This will cancel any ReadPrintDataFile() or SendPrintDataFile()
    413   // requests in flight (this is anticipation of when setting page
    414   // setup parameters becomes asynchronous and may be set while some
    415   // data is in flight).  Then we can clear out the print data.
    416   CancelAnyRunningTask();
    417   if (web_ui()) {
    418     print_data_sender_ = CreateCloudPrintDataSender();
    419     BrowserThread::PostTask(
    420         BrowserThread::IO, FROM_HERE,
    421         base::Bind(&CloudPrintDataSender::SendPrintData, print_data_sender_));
    422   }
    423 }
    424 
    425 void CloudPrintFlowHandler::HandleSetPageParameters(
    426     const base::ListValue* args) {
    427   std::string json;
    428   bool ret = args->GetString(0, &json);
    429   if (!ret || json.empty()) {
    430     NOTREACHED() << "Empty json string";
    431     return;
    432   }
    433 
    434   // These are backstop default values - 72 dpi to match the screen,
    435   // 8.5x11 inch paper with margins subtracted (1/4 inch top, left,
    436   // right and 0.56 bottom), and the min page shrink and max page
    437   // shrink values appear all over the place with no explanation.
    438 
    439   // TODO(scottbyer): Get a Linux/ChromeOS edge for PrintSettings
    440   // working so that we can get the default values from there.  Fix up
    441   // PrintWebViewHelper to do the same.
    442   const int kDPI = 72;
    443   const int kWidth = static_cast<int>((8.5-0.25-0.25)*kDPI);
    444   const int kHeight = static_cast<int>((11-0.25-0.56)*kDPI);
    445   const double kMinPageShrink = 1.25;
    446   const double kMaxPageShrink = 2.0;
    447 
    448   PrintMsg_Print_Params default_settings;
    449   default_settings.content_size = gfx::Size(kWidth, kHeight);
    450   default_settings.printable_area = gfx::Rect(0, 0, kWidth, kHeight);
    451   default_settings.dpi = kDPI;
    452   default_settings.min_shrink = kMinPageShrink;
    453   default_settings.max_shrink = kMaxPageShrink;
    454   default_settings.desired_dpi = kDPI;
    455   default_settings.document_cookie = 0;
    456   default_settings.selection_only = false;
    457   default_settings.preview_request_id = 0;
    458   default_settings.is_first_request = true;
    459   default_settings.print_to_pdf = false;
    460 
    461   if (!GetPageSetupParameters(json, default_settings)) {
    462     NOTREACHED();
    463     return;
    464   }
    465 
    466   // TODO(scottbyer) - Here is where we would kick the originating
    467   // renderer thread with these new parameters in order to get it to
    468   // re-generate the PDF data and hand it back to us.  window.print() is
    469   // currently synchronous, so there's a lot of work to do to get to
    470   // that point.
    471 }
    472 
    473 void CloudPrintFlowHandler::StoreDialogClientSize() const {
    474   if (web_ui() && web_ui()->GetWebContents()) {
    475     gfx::Size size = web_ui()->GetWebContents()->GetContainerBounds().size();
    476     Profile* profile = Profile::FromWebUI(web_ui());
    477     profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogWidth,
    478                                     size.width());
    479     profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogHeight,
    480                                     size.height());
    481   }
    482 }
    483 
    484 bool CloudPrintFlowHandler::IsCloudPrintDialogUrl(const GURL& url) {
    485   GURL cloud_print_url = cloud_devices::GetCloudPrintURL();
    486   return IsSimilarUrl(url, cloud_print_url);
    487 }
    488 
    489 CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate(
    490     content::BrowserContext* browser_context,
    491     gfx::NativeWindow modal_parent,
    492     const base::RefCountedMemory* data,
    493     const std::string& json_arguments,
    494     const base::string16& print_job_title,
    495     const base::string16& print_ticket,
    496     const std::string& file_type)
    497     : flow_handler_(
    498           new CloudPrintFlowHandler(data, print_job_title, print_ticket,
    499                                     file_type)),
    500       modal_parent_(modal_parent),
    501       owns_flow_handler_(true),
    502       keep_alive_when_non_modal_(true) {
    503   Init(browser_context, json_arguments);
    504 }
    505 
    506 // For unit testing.
    507 CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate(
    508     CloudPrintFlowHandler* flow_handler,
    509     const std::string& json_arguments)
    510     : flow_handler_(flow_handler),
    511       modal_parent_(NULL),
    512       owns_flow_handler_(true),
    513       keep_alive_when_non_modal_(false) {
    514   Init(NULL, json_arguments);
    515 }
    516 
    517 // Returns the persisted width/height for the print dialog.
    518 void GetDialogWidthAndHeightFromPrefs(content::BrowserContext* browser_context,
    519                                       int* width,
    520                                       int* height) {
    521   if (!browser_context) {
    522     *width = kDefaultWidth;
    523     *height = kDefaultHeight;
    524     return;
    525   }
    526 
    527   PrefService* prefs = Profile::FromBrowserContext(browser_context)->GetPrefs();
    528   *width = prefs->GetInteger(prefs::kCloudPrintDialogWidth);
    529   *height = prefs->GetInteger(prefs::kCloudPrintDialogHeight);
    530 }
    531 
    532 void CloudPrintWebDialogDelegate::Init(content::BrowserContext* browser_context,
    533                                        const std::string& json_arguments) {
    534   // This information is needed to show the dialog HTML content.
    535   DCHECK_CURRENTLY_ON(BrowserThread::UI);
    536 
    537   params_.url = GURL(chrome::kChromeUICloudPrintResourcesURL);
    538   GetDialogWidthAndHeightFromPrefs(browser_context,
    539                                    &params_.width,
    540                                    &params_.height);
    541   params_.json_input = json_arguments;
    542 
    543   flow_handler_->SetDialogDelegate(this);
    544   // If we're not modal we can show the dialog with no browser.
    545   // We need this to keep Chrome alive while our dialog is up.
    546   if (!modal_parent_ && keep_alive_when_non_modal_)
    547     chrome::IncrementKeepAliveCount();
    548 }
    549 
    550 CloudPrintWebDialogDelegate::~CloudPrintWebDialogDelegate() {
    551   // If the flow_handler_ is about to outlive us because we don't own
    552   // it anymore, we need to have it remove its reference to us.
    553   DCHECK_CURRENTLY_ON(BrowserThread::UI);
    554   flow_handler_->SetDialogDelegate(NULL);
    555   if (owns_flow_handler_) {
    556     delete flow_handler_;
    557   }
    558 }
    559 
    560 ui::ModalType CloudPrintWebDialogDelegate::GetDialogModalType() const {
    561     return modal_parent_ ? ui::MODAL_TYPE_WINDOW : ui::MODAL_TYPE_NONE;
    562 }
    563 
    564 base::string16 CloudPrintWebDialogDelegate::GetDialogTitle() const {
    565   return base::string16();
    566 }
    567 
    568 GURL CloudPrintWebDialogDelegate::GetDialogContentURL() const {
    569   return params_.url;
    570 }
    571 
    572 void CloudPrintWebDialogDelegate::GetWebUIMessageHandlers(
    573     std::vector<WebUIMessageHandler*>* handlers) const {
    574   handlers->push_back(flow_handler_);
    575   // We don't own flow_handler_ anymore, but it sticks around until at
    576   // least right after OnDialogClosed() is called (and this object is
    577   // destroyed).
    578   owns_flow_handler_ = false;
    579 }
    580 
    581 void CloudPrintWebDialogDelegate::GetDialogSize(gfx::Size* size) const {
    582   size->set_width(params_.width);
    583   size->set_height(params_.height);
    584 }
    585 
    586 std::string CloudPrintWebDialogDelegate::GetDialogArgs() const {
    587   return params_.json_input;
    588 }
    589 
    590 void CloudPrintWebDialogDelegate::OnDialogClosed(
    591     const std::string& json_retval) {
    592   // Get the final dialog size and store it.
    593   flow_handler_->StoreDialogClientSize();
    594 
    595   // If we're modal we can show the dialog with no browser.
    596   // End the keep-alive so that Chrome can exit.
    597   if (!modal_parent_ && keep_alive_when_non_modal_) {
    598     // Post to prevent recursive call tho this function.
    599     base::MessageLoop::current()->PostTask(
    600         FROM_HERE, base::Bind(&chrome::DecrementKeepAliveCount));
    601   }
    602   delete this;
    603 }
    604 
    605 void CloudPrintWebDialogDelegate::OnCloseContents(WebContents* source,
    606                                                   bool* out_close_dialog) {
    607   if (out_close_dialog)
    608     *out_close_dialog = true;
    609 }
    610 
    611 bool CloudPrintWebDialogDelegate::ShouldShowDialogTitle() const {
    612   return false;
    613 }
    614 
    615 bool CloudPrintWebDialogDelegate::HandleContextMenu(
    616     const content::ContextMenuParams& params) {
    617   return true;
    618 }
    619 
    620 // Called from the UI thread, starts up the dialog.
    621 void CreateDialogImpl(content::BrowserContext* browser_context,
    622                       gfx::NativeWindow modal_parent,
    623                       const base::RefCountedMemory* data,
    624                       const base::string16& print_job_title,
    625                       const base::string16& print_ticket,
    626                       const std::string& file_type) {
    627   DCHECK_CURRENTLY_ON(BrowserThread::UI);
    628   WebDialogDelegate* dialog_delegate =
    629       new internal_cloud_print_helpers::CloudPrintWebDialogDelegate(
    630           browser_context, modal_parent, data, std::string(), print_job_title,
    631           print_ticket, file_type);
    632 #if defined(OS_WIN)
    633   gfx::NativeWindow window =
    634 #endif
    635       chrome::ShowWebDialog(modal_parent,
    636                             Profile::FromBrowserContext(browser_context),
    637                             dialog_delegate);
    638 #if defined(OS_WIN)
    639   if (window) {
    640     HWND dialog_handle;
    641 #if defined(USE_AURA)
    642     dialog_handle = window->GetHost()->GetAcceleratedWidget();
    643 #else
    644     dialog_handle = window;
    645 #endif
    646     if (::GetForegroundWindow() != dialog_handle) {
    647       ui::ForegroundHelper::SetForeground(dialog_handle);
    648     }
    649   }
    650 #endif
    651 }
    652 
    653 void CreateDialogForFileImpl(content::BrowserContext* browser_context,
    654                              gfx::NativeWindow modal_parent,
    655                              const base::FilePath& path_to_file,
    656                              const base::string16& print_job_title,
    657                              const base::string16& print_ticket,
    658                              const std::string& file_type) {
    659   DCHECK_CURRENTLY_ON(BrowserThread::FILE);
    660   scoped_refptr<base::RefCountedMemory> data;
    661   int64 file_size = 0;
    662   if (base::GetFileSize(path_to_file, &file_size) && file_size != 0) {
    663     std::string file_data;
    664     if (file_size < kuint32max) {
    665       file_data.reserve(static_cast<unsigned int>(file_size));
    666     } else {
    667       DLOG(WARNING) << " print data file too large to reserve space";
    668     }
    669     if (base::ReadFileToString(path_to_file, &file_data)) {
    670       data = base::RefCountedString::TakeString(&file_data);
    671     }
    672   }
    673   // Proceed even for empty data to simplify testing.
    674   BrowserThread::PostTask(
    675       BrowserThread::UI, FROM_HERE,
    676       base::Bind(&print_dialog_cloud::CreatePrintDialogForBytes,
    677                  browser_context, modal_parent, data, print_job_title,
    678                  print_ticket, file_type));
    679   base::DeleteFile(path_to_file, false);
    680 }
    681 
    682 }  // namespace internal_cloud_print_helpers
    683 
    684 namespace print_dialog_cloud {
    685 
    686 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
    687   registry->RegisterIntegerPref(
    688       prefs::kCloudPrintDialogWidth,
    689       kDefaultWidth,
    690       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    691   registry->RegisterIntegerPref(
    692       prefs::kCloudPrintDialogHeight,
    693       kDefaultHeight,
    694       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    695 }
    696 
    697 // Called on the FILE or UI thread.  This is the main entry point into creating
    698 // the dialog.
    699 
    700 void CreatePrintDialogForFile(content::BrowserContext* browser_context,
    701                               gfx::NativeWindow modal_parent,
    702                               const base::FilePath& path_to_file,
    703                               const base::string16& print_job_title,
    704                               const base::string16& print_ticket,
    705                               const std::string& file_type) {
    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));
    713 }
    714 
    715 void CreateCloudPrintSigninTab(Browser* browser,
    716                                bool add_account,
    717                                const base::Closure& callback) {
    718   DCHECK_CURRENTLY_ON(BrowserThread::UI);
    719   if (switches::IsEnableAccountConsistency() &&
    720       !browser->profile()->IsOffTheRecord()) {
    721     browser->window()->ShowAvatarBubbleFromAvatarButton(
    722         add_account ? BrowserWindow::AVATAR_BUBBLE_MODE_ADD_ACCOUNT
    723                     : BrowserWindow::AVATAR_BUBBLE_MODE_SIGNIN,
    724         signin::ManageAccountsParams());
    725   } else {
    726     GURL url = add_account ? cloud_devices::GetCloudPrintAddAccountURL()
    727                            : cloud_devices::GetCloudPrintSigninURL();
    728     content::WebContents* web_contents =
    729         browser->OpenURL(content::OpenURLParams(
    730             google_util::AppendGoogleLocaleParam(
    731                 url, g_browser_process->GetApplicationLocale()),
    732             content::Referrer(),
    733             NEW_FOREGROUND_TAB,
    734             ui::PAGE_TRANSITION_AUTO_BOOKMARK,
    735             false));
    736     new SignInObserver(web_contents, cloud_devices::GetCloudPrintURL(),
    737                         callback);
    738   }
    739 }
    740 
    741 void CreatePrintDialogForBytes(content::BrowserContext* browser_context,
    742                                gfx::NativeWindow modal_parent,
    743                                const base::RefCountedMemory* data,
    744                                const base::string16& print_job_title,
    745                                const base::string16& print_ticket,
    746                                const std::string& file_type) {
    747   internal_cloud_print_helpers::CreateDialogImpl(browser_context, modal_parent,
    748                                                  data, print_job_title,
    749                                                  print_ticket, file_type);
    750 }
    751 
    752 bool CreatePrintDialogFromCommandLine(Profile* profile,
    753                                       const CommandLine& command_line) {
    754   DCHECK(command_line.HasSwitch(switches::kCloudPrintFile));
    755   if (!command_line.GetSwitchValuePath(switches::kCloudPrintFile).empty()) {
    756     base::FilePath cloud_print_file;
    757     cloud_print_file =
    758         command_line.GetSwitchValuePath(switches::kCloudPrintFile);
    759     if (!cloud_print_file.empty()) {
    760       base::string16 print_job_title;
    761       base::string16 print_job_print_ticket;
    762       if (command_line.HasSwitch(switches::kCloudPrintJobTitle)) {
    763         print_job_title =
    764           internal_cloud_print_helpers::GetSwitchValueString16(
    765               command_line, switches::kCloudPrintJobTitle);
    766       }
    767       if (command_line.HasSwitch(switches::kCloudPrintPrintTicket)) {
    768         print_job_print_ticket =
    769           internal_cloud_print_helpers::GetSwitchValueString16(
    770               command_line, switches::kCloudPrintPrintTicket);
    771       }
    772       std::string file_type = "application/pdf";
    773       if (command_line.HasSwitch(switches::kCloudPrintFileType)) {
    774         file_type = command_line.GetSwitchValueASCII(
    775             switches::kCloudPrintFileType);
    776       }
    777 
    778       print_dialog_cloud::CreatePrintDialogForFile(profile, NULL,
    779           cloud_print_file, print_job_title, print_job_print_ticket, file_type);
    780       return true;
    781     }
    782   }
    783   return false;
    784 }
    785 
    786 }  // namespace print_dialog_cloud
    787