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 ~CloudPrintFlowHandler() override
;
104 // WebUIMessageHandler implementation.
105 void RegisterMessages() override
;
107 // content::NotificationObserver implementation.
108 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::NativeView 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 ~CloudPrintWebDialogDelegate() override
;
157 // ui::WebDialogDelegate implementation:
158 ui::ModalType
GetDialogModalType() const override
;
159 base::string16
GetDialogTitle() const override
;
160 GURL
GetDialogContentURL() const override
;
161 void GetWebUIMessageHandlers(
162 std::vector
<content::WebUIMessageHandler
*>* handlers
) const override
;
163 void GetDialogSize(gfx::Size
* size
) const override
;
164 std::string
GetDialogArgs() const override
;
165 void OnDialogClosed(const std::string
& json_retval
) override
;
166 void OnCloseContents(content::WebContents
* source
,
167 bool* out_close_dialog
) override
;
168 bool ShouldShowDialogTitle() const override
;
169 bool HandleContextMenu(const content::ContextMenuParams
& params
) override
;
172 friend class ::CloudPrintWebDialogDelegateTest
;
175 CloudPrintWebDialogDelegate(CloudPrintFlowHandler
* flow_handler
,
176 const std::string
& json_arguments
);
177 void Init(content::BrowserContext
* browser_context
,
178 const std::string
& json_arguments
);
180 CloudPrintFlowHandler
* flow_handler_
;
181 gfx::NativeView modal_parent_
;
182 mutable bool owns_flow_handler_
;
183 bool keep_alive_when_non_modal_
;
185 // The parameters needed to display a modal web dialog.
186 ui::WebDialogUI::WebDialogParams params_
;
188 DISALLOW_COPY_AND_ASSIGN(CloudPrintWebDialogDelegate
);
191 void CreateDialogForFileImpl(content::BrowserContext
* browser_context
,
192 gfx::NativeView modal_parent
,
193 const base::FilePath
& path_to_file
,
194 const base::string16
& print_job_title
,
195 const base::string16
& print_ticket
,
196 const std::string
& file_type
);
198 } // namespace internal_cloud_print_helpers
200 #endif // CHROME_BROWSER_PRINTING_PRINT_DIALOG_CLOUD_INTERNAL_H_