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