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"
21 class CloudPrintWebDialogDelegateTest;
29 namespace internal_cloud_print_helpers {
31 // Small class to virtualize a few functions to aid with unit testing.
32 class CloudPrintDataSenderHelper {
34 explicit CloudPrintDataSenderHelper(content::WebUI* web_ui)
36 virtual ~CloudPrintDataSenderHelper() {}
38 // Virtualize the overrides of this function from WebUI to facilitate unit
40 virtual void CallJavascriptFunction(const std::string& function_name,
41 const base::Value& arg1,
42 const base::Value& arg2);
45 content::WebUI* web_ui_;
47 DISALLOW_COPY_AND_ASSIGN(CloudPrintDataSenderHelper);
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> {
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);
64 // Send print data (on the IO thread). We know that the WebUI pointer
65 // lifetime will outlast us, so we should be good.
68 // Cancels any ramining part of the task by clearing out the WebUI
70 void CancelPrintDataFile();
73 friend class base::RefCountedThreadSafe<CloudPrintDataSender>;
74 virtual ~CloudPrintDataSender();
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_;
83 DISALLOW_COPY_AND_ASSIGN(CloudPrintDataSender);
86 class CloudPrintWebDialogDelegate;
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
95 class CloudPrintFlowHandler : public content::WebUIMessageHandler,
96 public content::NotificationObserver {
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();
104 // WebUIMessageHandler implementation.
105 virtual void RegisterMessages() OVERRIDE;
107 // content::NotificationObserver implementation.
108 virtual void Observe(int type,
109 const content::NotificationSource& source,
110 const content::NotificationDetails& details) OVERRIDE;
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);
117 virtual void SetDialogDelegate(CloudPrintWebDialogDelegate *delegate);
118 void StoreDialogClientSize() const;
121 virtual scoped_refptr<CloudPrintDataSender> CreateCloudPrintDataSender();
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.
128 void CancelAnyRunningTask();
129 bool IsCloudPrintDialogUrl(const GURL& url);
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_;
140 DISALLOW_COPY_AND_ASSIGN(CloudPrintFlowHandler);
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
146 class CloudPrintWebDialogDelegate : public ui::WebDialogDelegate {
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();
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;
173 friend class ::CloudPrintWebDialogDelegateTest;
176 CloudPrintWebDialogDelegate(CloudPrintFlowHandler* flow_handler,
177 const std::string& json_arguments);
178 void Init(content::BrowserContext* browser_context,
179 const std::string& json_arguments);
181 CloudPrintFlowHandler* flow_handler_;
182 gfx::NativeWindow modal_parent_;
183 mutable bool owns_flow_handler_;
184 bool keep_alive_when_non_modal_;
186 // The parameters needed to display a modal web dialog.
187 ui::WebDialogUI::WebDialogParams params_;
189 DISALLOW_COPY_AND_ASSIGN(CloudPrintWebDialogDelegate);
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 bool delete_on_close);
200 } // namespace internal_cloud_print_helpers
202 #endif // CHROME_BROWSER_PRINTING_PRINT_DIALOG_CLOUD_INTERNAL_H_