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 #ifndef CHROME_BROWSER_PRINTING_PRINT_DIALOG_CLOUD_INTERNAL_H_
      6 #define CHROME_BROWSER_PRINTING_PRINT_DIALOG_CLOUD_INTERNAL_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/callback.h"
     12 #include "base/files/file_path.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/synchronization/lock.h"
     15 #include "content/public/browser/notification_observer.h"
     16 #include "content/public/browser/notification_registrar.h"
     17 #include "content/public/browser/web_ui_message_handler.h"
     18 #include "ui/web_dialogs/web_dialog_delegate.h"
     19 #include "ui/web_dialogs/web_dialog_ui.h"
     20 
     21 class CloudPrintWebDialogDelegateTest;
     22 class GURL;
     23 
     24 namespace base {
     25 class ListValue;
     26 class Value;
     27 }
     28 
     29 namespace internal_cloud_print_helpers {
     30 
     31 // Small class to virtualize a few functions to aid with unit testing.
     32 class CloudPrintDataSenderHelper {
     33  public:
     34   explicit CloudPrintDataSenderHelper(content::WebUI* web_ui)
     35       : web_ui_(web_ui) {}
     36   virtual ~CloudPrintDataSenderHelper() {}
     37 
     38   // Virtualize the overrides of this function from WebUI to facilitate unit
     39   // testing.
     40   virtual void CallJavascriptFunction(const std::string& function_name,
     41                                       const base::Value& arg1,
     42                                       const base::Value& arg2);
     43 
     44  private:
     45   content::WebUI* web_ui_;
     46 
     47   DISALLOW_COPY_AND_ASSIGN(CloudPrintDataSenderHelper);
     48 };
     49 
     50 // Small helper class to get the print data loaded in from the PDF
     51 // file (on the FILE thread) and send it to the print dialog contents
     52 // (on the IO thread), allowing for cancellation.
     53 class CloudPrintDataSender
     54     : public base::RefCountedThreadSafe<CloudPrintDataSender> {
     55  public:
     56   // The owner of this object is also expected to own and control the
     57   // lifetime of the helper.
     58   CloudPrintDataSender(CloudPrintDataSenderHelper* helper,
     59                        const base::string16& print_job_title,
     60                        const base::string16& print_ticket,
     61                        const std::string& file_type,
     62                        const base::RefCountedMemory* data);
     63 
     64   // Send print data (on the IO thread).  We know that the WebUI pointer
     65   // lifetime will outlast us, so we should be good.
     66   void SendPrintData();
     67 
     68   // Cancels any ramining part of the task by clearing out the WebUI
     69   // helper_ ptr.
     70   void CancelPrintDataFile();
     71 
     72  private:
     73   friend class base::RefCountedThreadSafe<CloudPrintDataSender>;
     74   virtual ~CloudPrintDataSender();
     75 
     76   base::Lock lock_;
     77   CloudPrintDataSenderHelper* volatile helper_;
     78   base::string16 print_job_title_;
     79   base::string16 print_ticket_;
     80   std::string file_type_;
     81   scoped_refptr<const base::RefCountedMemory> data_;
     82 
     83   DISALLOW_COPY_AND_ASSIGN(CloudPrintDataSender);
     84 };
     85 
     86 class CloudPrintWebDialogDelegate;
     87 
     88 // The CloudPrintFlowHandler connects the state machine (the UI delegate)
     89 // to the dialog backing HTML and JS by providing WebUIMessageHandler
     90 // functions for the JS to use.  This include refreshing the page
     91 // setup parameters (which will cause a re-generation of the PDF in
     92 // the renderer process - do we want a progress throbber shown?
     93 // Probably..), and packing up the PDF and job parameters and sending
     94 // them to the cloud.
     95 class CloudPrintFlowHandler : public content::WebUIMessageHandler,
     96                               public content::NotificationObserver {
     97  public:
     98   CloudPrintFlowHandler(const base::RefCountedMemory* data,
     99                         const base::string16& print_job_title,
    100                         const base::string16& print_ticket,
    101                         const std::string& file_type);
    102   virtual ~CloudPrintFlowHandler();
    103 
    104   // WebUIMessageHandler implementation.
    105   virtual void RegisterMessages() OVERRIDE;
    106 
    107   // content::NotificationObserver implementation.
    108   virtual void Observe(int type,
    109                        const content::NotificationSource& source,
    110                        const content::NotificationDetails& details) OVERRIDE;
    111 
    112   // Callbacks from the page.
    113   void HandleShowDebugger(const base::ListValue* args);
    114   void HandleSendPrintData(const base::ListValue* args);
    115   void HandleSetPageParameters(const base::ListValue* args);
    116 
    117   virtual void SetDialogDelegate(CloudPrintWebDialogDelegate *delegate);
    118   void StoreDialogClientSize() const;
    119 
    120  private:
    121   virtual scoped_refptr<CloudPrintDataSender> CreateCloudPrintDataSender();
    122 
    123   // Call to get the debugger loaded on our hosted dialog page
    124   // specifically.  Since we're not in an official browser tab, only
    125   // way to get the debugger going.
    126   void ShowDebugger();
    127 
    128   void CancelAnyRunningTask();
    129   bool IsCloudPrintDialogUrl(const GURL& url);
    130 
    131   CloudPrintWebDialogDelegate* dialog_delegate_;
    132   content::NotificationRegistrar registrar_;
    133   scoped_refptr<const base::RefCountedMemory> data_;
    134   base::string16 print_job_title_;
    135   base::string16 print_ticket_;
    136   std::string file_type_;
    137   scoped_refptr<CloudPrintDataSender> print_data_sender_;
    138   scoped_ptr<CloudPrintDataSenderHelper> print_data_helper_;
    139 
    140   DISALLOW_COPY_AND_ASSIGN(CloudPrintFlowHandler);
    141 };
    142 
    143 // State machine used to run the printing dialog.  This class is used
    144 // to open and run the web dialog and deletes itself when the dialog
    145 // is closed.
    146 class CloudPrintWebDialogDelegate : public ui::WebDialogDelegate {
    147  public:
    148   CloudPrintWebDialogDelegate(content::BrowserContext* browser_context,
    149                               gfx::NativeWindow modal_parent,
    150                               const base::RefCountedMemory* data,
    151                               const std::string& json_arguments,
    152                               const base::string16& print_job_title,
    153                               const base::string16& print_ticket,
    154                               const std::string& file_type);
    155   virtual ~CloudPrintWebDialogDelegate();
    156 
    157   // ui::WebDialogDelegate implementation:
    158   virtual ui::ModalType GetDialogModalType() const OVERRIDE;
    159   virtual base::string16 GetDialogTitle() const OVERRIDE;
    160   virtual GURL GetDialogContentURL() const OVERRIDE;
    161   virtual void GetWebUIMessageHandlers(
    162       std::vector<content::WebUIMessageHandler*>* handlers) const OVERRIDE;
    163   virtual void GetDialogSize(gfx::Size* size) const OVERRIDE;
    164   virtual std::string GetDialogArgs() const OVERRIDE;
    165   virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE;
    166   virtual void OnCloseContents(content::WebContents* source,
    167                                bool* out_close_dialog) OVERRIDE;
    168   virtual bool ShouldShowDialogTitle() const OVERRIDE;
    169   virtual bool HandleContextMenu(
    170       const content::ContextMenuParams& params) OVERRIDE;
    171 
    172  private:
    173   friend class ::CloudPrintWebDialogDelegateTest;
    174 
    175   // For unit testing.
    176   CloudPrintWebDialogDelegate(CloudPrintFlowHandler* flow_handler,
    177                               const std::string& json_arguments);
    178   void Init(content::BrowserContext* browser_context,
    179             const std::string& json_arguments);
    180 
    181   CloudPrintFlowHandler* flow_handler_;
    182   gfx::NativeWindow modal_parent_;
    183   mutable bool owns_flow_handler_;
    184   bool keep_alive_when_non_modal_;
    185 
    186   // The parameters needed to display a modal web dialog.
    187   ui::WebDialogUI::WebDialogParams params_;
    188 
    189   DISALLOW_COPY_AND_ASSIGN(CloudPrintWebDialogDelegate);
    190 };
    191 
    192 void CreateDialogForFileImpl(content::BrowserContext* browser_context,
    193                              gfx::NativeWindow modal_parent,
    194                              const base::FilePath& path_to_file,
    195                              const base::string16& print_job_title,
    196                              const base::string16& print_ticket,
    197                              const std::string& file_type);
    198 
    199 }  // namespace internal_cloud_print_helpers
    200 
    201 #endif  // CHROME_BROWSER_PRINTING_PRINT_DIALOG_CLOUD_INTERNAL_H_
    202