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 #ifndef CHROME_BROWSER_PRINTING_PRINT_DIALOG_CLOUD_INTERNAL_H_
6 #define CHROME_BROWSER_PRINTING_PRINT_DIALOG_CLOUD_INTERNAL_H_
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"
22 class CloudPrintWebDialogDelegateTest
;
30 namespace internal_cloud_print_helpers
{
32 // Small class to virtualize a few functions to aid with unit testing.
33 class CloudPrintDataSenderHelper
{
35 explicit CloudPrintDataSenderHelper(content::WebUI
* web_ui
)
37 virtual ~CloudPrintDataSenderHelper() {}
39 // Virtualize the overrides of these three functions from WebUI to
40 // facilitate unit testing.
41 virtual void CallJavascriptFunction(const std::wstring
& function_name
);
42 virtual void CallJavascriptFunction(const std::wstring
& function_name
,
43 const base::Value
& arg
);
44 virtual void CallJavascriptFunction(const std::wstring
& function_name
,
45 const base::Value
& arg1
,
46 const base::Value
& arg2
);
47 virtual void CallJavascriptFunction(const std::wstring
& function_name
,
48 const base::Value
& arg1
,
49 const base::Value
& arg2
,
50 const base::Value
& arg3
);
53 content::WebUI
* web_ui_
;
55 DISALLOW_COPY_AND_ASSIGN(CloudPrintDataSenderHelper
);
58 // Small helper class to get the print data loaded in from the PDF
59 // file (on the FILE thread) and send it to the print dialog contents
60 // (on the IO thread), allowing for cancellation.
61 class CloudPrintDataSender
62 : public base::RefCountedThreadSafe
<CloudPrintDataSender
> {
64 // The owner of this object is also expected to own and control the
65 // lifetime of the helper.
66 CloudPrintDataSender(CloudPrintDataSenderHelper
* helper
,
67 const string16
& print_job_title
,
68 const string16
& print_ticket
,
69 const std::string
& file_type
,
70 const base::RefCountedMemory
* data
);
72 // Send print data (on the IO thread). We know that the WebUI pointer
73 // lifetime will outlast us, so we should be good.
76 // Cancels any ramining part of the task by clearing out the WebUI
78 void CancelPrintDataFile();
81 friend class base::RefCountedThreadSafe
<CloudPrintDataSender
>;
82 virtual ~CloudPrintDataSender();
85 CloudPrintDataSenderHelper
* volatile helper_
;
86 string16 print_job_title_
;
87 string16 print_ticket_
;
88 std::string file_type_
;
89 scoped_refptr
<const base::RefCountedMemory
> data_
;
91 DISALLOW_COPY_AND_ASSIGN(CloudPrintDataSender
);
94 class CloudPrintWebDialogDelegate
;
96 // The CloudPrintFlowHandler connects the state machine (the UI delegate)
97 // to the dialog backing HTML and JS by providing WebUIMessageHandler
98 // functions for the JS to use. This include refreshing the page
99 // setup parameters (which will cause a re-generation of the PDF in
100 // the renderer process - do we want a progress throbber shown?
101 // Probably..), and packing up the PDF and job parameters and sending
102 // them to the cloud.
103 class CloudPrintFlowHandler
: public content::WebUIMessageHandler
,
104 public content::NotificationObserver
{
106 CloudPrintFlowHandler(const base::RefCountedMemory
* data
,
107 const string16
& print_job_title
,
108 const string16
& print_ticket
,
109 const std::string
& file_type
,
110 bool close_after_signin
,
111 const base::Closure
& callback
);
112 virtual ~CloudPrintFlowHandler();
114 // WebUIMessageHandler implementation.
115 virtual void RegisterMessages() OVERRIDE
;
117 // content::NotificationObserver implementation.
118 virtual void Observe(int type
,
119 const content::NotificationSource
& source
,
120 const content::NotificationDetails
& details
) OVERRIDE
;
122 // Callbacks from the page.
123 void HandleShowDebugger(const base::ListValue
* args
);
124 void HandleSendPrintData(const base::ListValue
* args
);
125 void HandleSetPageParameters(const base::ListValue
* args
);
127 virtual void SetDialogDelegate(CloudPrintWebDialogDelegate
*delegate
);
128 void StoreDialogClientSize() const;
130 bool NavigationToURLDidCloseDialog(const GURL
& url
);
133 virtual scoped_refptr
<CloudPrintDataSender
> CreateCloudPrintDataSender();
135 // Call to get the debugger loaded on our hosted dialog page
136 // specifically. Since we're not in an official browser tab, only
137 // way to get the debugger going.
140 void CancelAnyRunningTask();
142 CloudPrintWebDialogDelegate
* dialog_delegate_
;
143 content::NotificationRegistrar registrar_
;
144 scoped_refptr
<const base::RefCountedMemory
> data_
;
145 string16 print_job_title_
;
146 string16 print_ticket_
;
147 std::string file_type_
;
148 scoped_refptr
<CloudPrintDataSender
> print_data_sender_
;
149 scoped_ptr
<CloudPrintDataSenderHelper
> print_data_helper_
;
150 bool close_after_signin_
;
151 base::Closure callback_
;
153 DISALLOW_COPY_AND_ASSIGN(CloudPrintFlowHandler
);
156 // State machine used to run the printing dialog. This class is used
157 // to open and run the web dialog and deletes itself when the dialog
159 class CloudPrintWebDialogDelegate
: public ui::WebDialogDelegate
{
161 CloudPrintWebDialogDelegate(content::BrowserContext
* browser_context
,
162 gfx::NativeWindow modal_parent
,
163 const base::RefCountedMemory
* data
,
164 const std::string
& json_arguments
,
165 const string16
& print_job_title
,
166 const string16
& print_ticket
,
167 const std::string
& file_type
,
168 bool close_after_signin
,
169 const base::Closure
& callback
);
170 virtual ~CloudPrintWebDialogDelegate();
172 // ui::WebDialogDelegate implementation:
173 virtual ui::ModalType
GetDialogModalType() const OVERRIDE
;
174 virtual string16
GetDialogTitle() const OVERRIDE
;
175 virtual GURL
GetDialogContentURL() const OVERRIDE
;
176 virtual void GetWebUIMessageHandlers(
177 std::vector
<content::WebUIMessageHandler
*>* handlers
) const OVERRIDE
;
178 virtual void GetDialogSize(gfx::Size
* size
) const OVERRIDE
;
179 virtual std::string
GetDialogArgs() const OVERRIDE
;
180 virtual void OnDialogClosed(const std::string
& json_retval
) OVERRIDE
;
181 virtual void OnCloseContents(content::WebContents
* source
,
182 bool* out_close_dialog
) OVERRIDE
;
183 virtual bool ShouldShowDialogTitle() const OVERRIDE
;
184 virtual bool HandleContextMenu(
185 const content::ContextMenuParams
& params
) OVERRIDE
;
186 virtual bool HandleOpenURLFromTab(
187 content::WebContents
* source
,
188 const content::OpenURLParams
& params
,
189 content::WebContents
** out_new_contents
) OVERRIDE
;
192 friend class ::CloudPrintWebDialogDelegateTest
;
195 CloudPrintWebDialogDelegate(CloudPrintFlowHandler
* flow_handler
,
196 const std::string
& json_arguments
);
197 void Init(content::BrowserContext
* browser_context
,
198 const std::string
& json_arguments
);
200 CloudPrintFlowHandler
* flow_handler_
;
201 gfx::NativeWindow modal_parent_
;
202 mutable bool owns_flow_handler_
;
203 bool keep_alive_when_non_modal_
;
205 // The parameters needed to display a modal web dialog.
206 ui::WebDialogUI::WebDialogParams params_
;
208 DISALLOW_COPY_AND_ASSIGN(CloudPrintWebDialogDelegate
);
211 void CreateDialogForFileImpl(content::BrowserContext
* browser_context
,
212 gfx::NativeWindow modal_parent
,
213 const base::FilePath
& path_to_file
,
214 const string16
& print_job_title
,
215 const string16
& print_ticket
,
216 const std::string
& file_type
,
217 bool delete_on_close
);
218 void CreateDialogSigninImpl(content::BrowserContext
* browser_context
,
219 gfx::NativeWindow modal_parent
,
220 const base::Closure
& callback
);
222 } // namespace internal_cloud_print_helpers
224 #endif // CHROME_BROWSER_PRINTING_PRINT_DIALOG_CLOUD_INTERNAL_H_