blob: 1e37cdc0643ba61f3431131b220245baea1c9783 [file] [log] [blame]
[email protected]87ab41e72012-01-04 18:45:111// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]73852b8f2010-05-14 00:38:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/printing/print_dialog_cloud.h"
6#include "chrome/browser/printing/print_dialog_cloud_internal.h"
7
[email protected]73852b8f2010-05-14 00:38:128#include "base/base64.h"
[email protected]ba4fc242011-10-04 18:56:569#include "base/bind.h"
10#include "base/bind_helpers.h"
[email protected]65c9d89a2011-04-13 21:02:3911#include "base/command_line.h"
[email protected]73852b8f2010-05-14 00:38:1212#include "base/file_util.h"
13#include "base/json/json_reader.h"
[email protected]4b8852a2011-06-10 17:24:4614#include "base/utf_string_conversions.h"
[email protected]73852b8f2010-05-14 00:38:1215#include "base/values.h"
[email protected]0744cc72011-08-12 12:05:4516#include "chrome/browser/browser_process_impl.h"
[email protected]dce502762011-07-20 08:53:4917#include "chrome/browser/debugger/devtools_window.h"
[email protected]2e6389f2012-05-18 19:41:2518#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]ea161da2010-11-02 21:57:3519#include "chrome/browser/prefs/pref_service.h"
[email protected]2283eead2010-09-29 23:17:3020#include "chrome/browser/printing/cloud_print/cloud_print_url.h"
[email protected]8ecad5e2010-12-02 21:18:3321#include "chrome/browser/profiles/profile.h"
[email protected]e39027a2011-01-24 21:41:5422#include "chrome/browser/profiles/profile_manager.h"
[email protected]508326df2012-05-23 16:01:1923#include "chrome/browser/ui/browser_dialogs.h"
[email protected]65c9d89a2011-04-13 21:02:3924#include "chrome/common/chrome_switches.h"
[email protected]ea161da2010-11-02 21:57:3525#include "chrome/common/pref_names.h"
[email protected]1375e3ab2011-03-24 17:07:2226#include "chrome/common/print_messages.h"
[email protected]73852b8f2010-05-14 00:38:1227#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4928#include "content/public/browser/browser_thread.h"
[email protected]cdcb1dee2012-01-04 00:46:2029#include "content/public/browser/navigation_controller.h"
[email protected]022af742011-12-28 18:37:2530#include "content/public/browser/navigation_entry.h"
[email protected]6c2381d2011-10-19 02:52:5331#include "content/public/browser/notification_registrar.h"
32#include "content/public/browser/notification_source.h"
[email protected]0d6e9bd2011-10-18 04:29:1633#include "content/public/browser/notification_types.h"
[email protected]9c1662b2012-03-06 15:44:3334#include "content/public/browser/render_view_host.h"
[email protected]0ec4898e2011-12-30 21:09:2435#include "content/public/browser/web_contents.h"
[email protected]8643e6d2012-01-18 20:26:1036#include "content/public/browser/web_contents_view.h"
[email protected]01ec4ec2012-01-18 04:13:4737#include "content/public/browser/web_ui.h"
[email protected]fca6b9b2012-05-22 17:11:0538#include "grit/generated_resources.h"
[email protected]c051a1b2011-01-21 23:30:1739#include "ui/base/l10n/l10n_util.h"
[email protected]fca6b9b2012-05-22 17:11:0540#include "webkit/glue/webpreferences.h"
[email protected]520c2022012-03-15 00:13:1541
42#if defined(OS_WIN)
43#include "ui/base/win/foreground_helper.h"
44#endif
45
[email protected]73852b8f2010-05-14 00:38:1246// This module implements the UI support in Chrome for cloud printing.
47// This means hosting a dialog containing HTML/JavaScript and using
48// the published cloud print user interface integration APIs to get
49// page setup settings from the dialog contents and provide the
[email protected]a984bdf2011-03-15 20:17:1650// generated print data to the dialog contents for uploading to the
[email protected]73852b8f2010-05-14 00:38:1251// cloud print service.
52
53// Currently, the flow between these classes is as follows:
54
[email protected]a984bdf2011-03-15 20:17:1655// PrintDialogCloud::CreatePrintDialogForFile is called from
[email protected]73852b8f2010-05-14 00:38:1256// resource_message_filter_gtk.cc once the renderer has informed the
[email protected]a984bdf2011-03-15 20:17:1657// renderer host that print data generation into the renderer host provided
[email protected]032682b2011-01-12 22:05:0258// temp file has been completed. That call is on the FILE thread.
[email protected]73852b8f2010-05-14 00:38:1259// That, in turn, hops over to the UI thread to create an instance of
60// PrintDialogCloud.
61
62// The constructor for PrintDialogCloud creates a
[email protected]5835871a2012-04-25 21:56:5563// CloudPrintWebDialogDelegate and asks the current active browser to
[email protected]73852b8f2010-05-14 00:38:1264// show an HTML dialog using that class as the delegate. That class
[email protected]89f550b2011-06-08 18:34:0365// hands in the kChromeUICloudPrintResourcesURL as the URL to visit. That is
[email protected]80a8fad2011-01-29 04:02:3866// recognized by the GetWebUIFactoryFunction as a signal to create an
[email protected]02b5ccc2012-04-30 23:58:3167// ExternalWebDialogUI.
[email protected]73852b8f2010-05-14 00:38:1268
[email protected]5835871a2012-04-25 21:56:5569// CloudPrintWebDialogDelegate also temporarily owns a
[email protected]73852b8f2010-05-14 00:38:1270// CloudPrintFlowHandler, a class which is responsible for the actual
[email protected]a984bdf2011-03-15 20:17:1671// interactions with the dialog contents, including handing in the
[email protected]73852b8f2010-05-14 00:38:1272// print data and getting any page setup parameters that the dialog
73// contents provides. As part of bringing up the dialog,
[email protected]02b5ccc2012-04-30 23:58:3174// WebDialogUI::RenderViewCreated is called (an override of
[email protected]c39f9bf2011-02-12 00:43:5575// WebUI::RenderViewCreated). That routine, in turn, calls the
[email protected]36e12172011-02-08 23:46:0276// delegate's GetWebUIMessageHandlers routine, at which point the
[email protected]73852b8f2010-05-14 00:38:1277// ownership of the CloudPrintFlowHandler is handed over. A pointer
78// to the flow handler is kept to facilitate communication back and
79// forth between the two classes.
80
[email protected]c39f9bf2011-02-12 00:43:5581// The WebUI continues dialog bring-up, calling
[email protected]73852b8f2010-05-14 00:38:1282// CloudPrintFlowHandler::RegisterMessages. This is where the
83// additional object model capabilities are registered for the dialog
84// contents to use. It is also at this time that capabilities for the
85// dialog contents are adjusted to allow the dialog contents to close
86// the window. In addition, the pending URL is redirected to the
87// actual cloud print service URL. The flow controller also registers
88// for notification of when the dialog contents finish loading, which
[email protected]a984bdf2011-03-15 20:17:1689// is currently used to send the data to the dialog contents.
[email protected]73852b8f2010-05-14 00:38:1290
[email protected]a984bdf2011-03-15 20:17:1691// In order to send the data to the dialog contents, the flow
[email protected]73852b8f2010-05-14 00:38:1292// handler uses a CloudPrintDataSender. It creates one, letting it
[email protected]a984bdf2011-03-15 20:17:1693// know the name of the temporary file containing the data, and
[email protected]73852b8f2010-05-14 00:38:1294// posts the task of reading the file
95// (CloudPrintDataSender::ReadPrintDataFile) to the file thread. That
96// routine reads in the file, and then hops over to the IO thread to
97// send that data to the dialog contents.
98
99// When the dialog contents are finished (by either being cancelled or
100// hitting the print button), the delegate is notified, and responds
101// that the dialog should be closed, at which point things are torn
102// down and released.
103
104// TODO(scottbyer):
105// http://code.google.com/p/chromium/issues/detail?id=44093 The
[email protected]a984bdf2011-03-15 20:17:16106// high-level flow (where the data is generated before even
[email protected]73852b8f2010-05-14 00:38:12107// bringing up the dialog) isn't what we want.
108
[email protected]631bb742011-11-02 11:29:39109using content::BrowserThread;
[email protected]c5eed492012-01-04 17:07:50110using content::NavigationController;
[email protected]10f417c52011-12-28 21:04:23111using content::NavigationEntry;
[email protected]eaabba22012-03-07 15:02:11112using content::RenderViewHost;
[email protected]a81343d232011-12-27 07:39:20113using content::WebContents;
[email protected]26e2632a2011-12-31 04:02:55114using content::WebUIMessageHandler;
[email protected]20c07f8e2012-05-31 08:43:14115using ui::WebDialogDelegate;
[email protected]631bb742011-11-02 11:29:39116
[email protected]73852b8f2010-05-14 00:38:12117namespace internal_cloud_print_helpers {
118
[email protected]73852b8f2010-05-14 00:38:12119// From the JSON parsed value, get the entries for the page setup
120// parameters.
121bool GetPageSetupParameters(const std::string& json,
[email protected]1375e3ab2011-03-24 17:07:22122 PrintMsg_Print_Params& parameters) {
[email protected]cd5785752012-04-11 00:15:41123 scoped_ptr<Value> parsed_value(base::JSONReader::Read(json));
[email protected]73852b8f2010-05-14 00:38:12124 DLOG_IF(ERROR, (!parsed_value.get() ||
125 !parsed_value->IsType(Value::TYPE_DICTIONARY)))
126 << "PageSetup call didn't have expected contents";
127 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY))
128 return false;
129
130 bool result = true;
131 DictionaryValue* params = static_cast<DictionaryValue*>(parsed_value.get());
[email protected]05c7da62011-05-05 17:23:56132 result &= params->GetDouble("dpi", &parameters.dpi);
133 result &= params->GetDouble("min_shrink", &parameters.min_shrink);
134 result &= params->GetDouble("max_shrink", &parameters.max_shrink);
[email protected]a65175d2010-08-17 04:00:57135 result &= params->GetBoolean("selection_only", &parameters.selection_only);
[email protected]73852b8f2010-05-14 00:38:12136 return result;
137}
138
[email protected]e8368e92011-08-20 04:05:56139string16 GetSwitchValueString16(const CommandLine& command_line,
140 const char* switchName) {
141#ifdef OS_WIN
142 CommandLine::StringType native_switch_val;
143 native_switch_val = command_line.GetSwitchValueNative(switchName);
144 return string16(native_switch_val);
145#elif defined(OS_POSIX)
146 // POSIX Command line string types are different.
147 CommandLine::StringType native_switch_val;
148 native_switch_val = command_line.GetSwitchValueASCII(switchName);
149 // Convert the ASCII string to UTF16 to prepare to pass.
150 return string16(ASCIIToUTF16(native_switch_val));
151#endif
152}
153
[email protected]73852b8f2010-05-14 00:38:12154void CloudPrintDataSenderHelper::CallJavascriptFunction(
155 const std::wstring& function_name) {
[email protected]adcf8492011-03-09 22:41:39156 web_ui_->CallJavascriptFunction(WideToASCII(function_name));
[email protected]73852b8f2010-05-14 00:38:12157}
158
159void CloudPrintDataSenderHelper::CallJavascriptFunction(
160 const std::wstring& function_name, const Value& arg) {
[email protected]adcf8492011-03-09 22:41:39161 web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg);
[email protected]73852b8f2010-05-14 00:38:12162}
163
164void CloudPrintDataSenderHelper::CallJavascriptFunction(
165 const std::wstring& function_name, const Value& arg1, const Value& arg2) {
[email protected]adcf8492011-03-09 22:41:39166 web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg1, arg2);
[email protected]73852b8f2010-05-14 00:38:12167}
168
[email protected]e8368e92011-08-20 04:05:56169void CloudPrintDataSenderHelper::CallJavascriptFunction(
170 const std::wstring& function_name,
171 const Value& arg1,
172 const Value& arg2,
173 const Value& arg3) {
[email protected]46adf7ff2011-12-30 00:53:09174 web_ui_->CallJavascriptFunction(
175 WideToASCII(function_name), arg1, arg2, arg3);
[email protected]e8368e92011-08-20 04:05:56176}
177
[email protected]73852b8f2010-05-14 00:38:12178// Clears out the pointer we're using to communicate. Either routine is
179// potentially expensive enough that stopping whatever is in progress
180// is worth it.
181void CloudPrintDataSender::CancelPrintDataFile() {
[email protected]20305ec2011-01-21 04:55:52182 base::AutoLock lock(lock_);
[email protected]73852b8f2010-05-14 00:38:12183 // We don't own helper, it was passed in to us, so no need to
184 // delete, just let it go.
185 helper_ = NULL;
186}
187
[email protected]38e08982010-10-22 17:28:43188CloudPrintDataSender::CloudPrintDataSender(CloudPrintDataSenderHelper* helper,
[email protected]a984bdf2011-03-15 20:17:16189 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56190 const string16& print_ticket,
[email protected]a984bdf2011-03-15 20:17:16191 const std::string& file_type)
[email protected]38e08982010-10-22 17:28:43192 : helper_(helper),
[email protected]a984bdf2011-03-15 20:17:16193 print_job_title_(print_job_title),
[email protected]e8368e92011-08-20 04:05:56194 print_ticket_(print_ticket),
[email protected]a984bdf2011-03-15 20:17:16195 file_type_(file_type) {
[email protected]38e08982010-10-22 17:28:43196}
197
198CloudPrintDataSender::~CloudPrintDataSender() {}
199
[email protected]a984bdf2011-03-15 20:17:16200// Grab the raw file contents and massage them into shape for
[email protected]73852b8f2010-05-14 00:38:12201// sending to the dialog contents (and up to the cloud print server)
202// by encoding it and prefixing it with the appropriate mime type.
203// Once that is done, kick off the next part of the task on the IO
204// thread.
[email protected]a984bdf2011-03-15 20:17:16205void CloudPrintDataSender::ReadPrintDataFile(const FilePath& path_to_file) {
[email protected]ba4f1132010-10-09 02:02:35206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]73852b8f2010-05-14 00:38:12207 int64 file_size = 0;
[email protected]a984bdf2011-03-15 20:17:16208 if (file_util::GetFileSize(path_to_file, &file_size) && file_size != 0) {
[email protected]73852b8f2010-05-14 00:38:12209 std::string file_data;
210 if (file_size < kuint32max) {
211 file_data.reserve(static_cast<unsigned int>(file_size));
212 } else {
213 DLOG(WARNING) << " print data file too large to reserve space";
214 }
[email protected]a984bdf2011-03-15 20:17:16215 if (helper_ && file_util::ReadFileToString(path_to_file, &file_data)) {
[email protected]73852b8f2010-05-14 00:38:12216 std::string base64_data;
217 base::Base64Encode(file_data, &base64_data);
[email protected]a984bdf2011-03-15 20:17:16218 std::string header("data:");
219 header.append(file_type_);
220 header.append(";base64,");
[email protected]73852b8f2010-05-14 00:38:12221 base64_data.insert(0, header);
222 scoped_ptr<StringValue> new_data(new StringValue(base64_data));
223 print_data_.swap(new_data);
[email protected]3e2dd4fa2011-11-10 06:06:40224 BrowserThread::PostTask(
225 BrowserThread::IO, FROM_HERE,
226 base::Bind(&CloudPrintDataSender::SendPrintDataFile, this));
[email protected]73852b8f2010-05-14 00:38:12227 }
228 }
229}
230
231// We have the data in hand that needs to be pushed into the dialog
232// contents; do so from the IO thread.
233
234// TODO(scottbyer): If the print data ends up being larger than the
235// upload limit (currently 10MB), what we need to do is upload that
236// large data to google docs and set the URL in the printing
237// JavaScript to that location, and make sure it gets deleted when not
238// needed. - 4/1/2010
239void CloudPrintDataSender::SendPrintDataFile() {
[email protected]ba4f1132010-10-09 02:02:35240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]20305ec2011-01-21 04:55:52241 base::AutoLock lock(lock_);
[email protected]73852b8f2010-05-14 00:38:12242 if (helper_ && print_data_.get()) {
[email protected]9848c7e2010-06-03 16:06:56243 StringValue title(print_job_title_);
[email protected]e8368e92011-08-20 04:05:56244 StringValue ticket(print_ticket_);
245 // TODO(abodenha): Change Javascript call to pass in print ticket
246 // after server side support is added. Add test for it.
[email protected]73852b8f2010-05-14 00:38:12247
248 // Send the print data to the dialog contents. The JavaScript
249 // function is a preliminary API for prototyping purposes and is
250 // subject to change.
251 const_cast<CloudPrintDataSenderHelper*>(helper_)->CallJavascriptFunction(
252 L"printApp._printDataUrl", *print_data_, title);
253 }
254}
255
256
[email protected]a984bdf2011-03-15 20:17:16257CloudPrintFlowHandler::CloudPrintFlowHandler(const FilePath& path_to_file,
258 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56259 const string16& print_ticket,
[email protected]4cd49022012-01-19 20:37:37260 const std::string& file_type,
261 bool close_after_signin,
262 const base::Closure& callback)
[email protected]c7bf7452011-09-12 21:31:50263 : dialog_delegate_(NULL),
264 path_to_file_(path_to_file),
[email protected]a984bdf2011-03-15 20:17:16265 print_job_title_(print_job_title),
[email protected]e8368e92011-08-20 04:05:56266 print_ticket_(print_ticket),
[email protected]4cd49022012-01-19 20:37:37267 file_type_(file_type),
268 close_after_signin_(close_after_signin),
269 callback_(callback) {
[email protected]38e08982010-10-22 17:28:43270}
271
272CloudPrintFlowHandler::~CloudPrintFlowHandler() {
273 // This will also cancel any task in flight.
274 CancelAnyRunningTask();
275}
276
277
[email protected]73852b8f2010-05-14 00:38:12278void CloudPrintFlowHandler::SetDialogDelegate(
[email protected]5835871a2012-04-25 21:56:55279 CloudPrintWebDialogDelegate* delegate) {
[email protected]7b748982011-02-14 19:28:23280 // Even if setting a new WebUI, it means any previous task needs
[email protected]a2c92a1c2012-04-03 12:32:14281 // to be canceled, its now invalid.
[email protected]ba4f1132010-10-09 02:02:35282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12283 CancelAnyRunningTask();
284 dialog_delegate_ = delegate;
285}
286
287// Cancels any print data sender we have in flight and removes our
288// reference to it, so when the task that is calling it finishes and
[email protected]a2c92a1c2012-04-03 12:32:14289// removes its reference, it goes away.
[email protected]73852b8f2010-05-14 00:38:12290void CloudPrintFlowHandler::CancelAnyRunningTask() {
[email protected]ba4f1132010-10-09 02:02:35291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12292 if (print_data_sender_.get()) {
293 print_data_sender_->CancelPrintDataFile();
294 print_data_sender_ = NULL;
295 }
296}
297
[email protected]73852b8f2010-05-14 00:38:12298void CloudPrintFlowHandler::RegisterMessages() {
[email protected]73852b8f2010-05-14 00:38:12299 // TODO(scottbyer) - This is where we will register messages for the
300 // UI JS to use. Needed: Call to update page setup parameters.
[email protected]46adf7ff2011-12-30 00:53:09301 web_ui()->RegisterMessageCallback("ShowDebugger",
[email protected]ba4fc242011-10-04 18:56:56302 base::Bind(&CloudPrintFlowHandler::HandleShowDebugger,
303 base::Unretained(this)));
[email protected]46adf7ff2011-12-30 00:53:09304 web_ui()->RegisterMessageCallback("SendPrintData",
[email protected]ba4fc242011-10-04 18:56:56305 base::Bind(&CloudPrintFlowHandler::HandleSendPrintData,
306 base::Unretained(this)));
[email protected]46adf7ff2011-12-30 00:53:09307 web_ui()->RegisterMessageCallback("SetPageParameters",
[email protected]ba4fc242011-10-04 18:56:56308 base::Bind(&CloudPrintFlowHandler::HandleSetPageParameters,
309 base::Unretained(this)));
[email protected]73852b8f2010-05-14 00:38:12310
[email protected]0eb25c42011-08-11 14:50:14311 // Register for appropriate notifications, and re-direct the URL
312 // to the real server URL, now that we've gotten an HTML dialog
313 // going.
[email protected]c5eed492012-01-04 17:07:50314 NavigationController* controller =
[email protected]01ec4ec2012-01-18 04:13:47315 &web_ui()->GetWebContents()->GetController();
[email protected]10f417c52011-12-28 21:04:23316 NavigationEntry* pending_entry = controller->GetPendingEntry();
[email protected]0eb25c42011-08-11 14:50:14317 if (pending_entry) {
[email protected]46adf7ff2011-12-30 00:53:09318 Profile* profile = Profile::FromWebUI(web_ui());
[email protected]4cd49022012-01-19 20:37:37319 if (close_after_signin_) {
320 pending_entry->SetURL(
321 CloudPrintURL(profile).GetCloudPrintSigninURL());
322 } else {
323 pending_entry->SetURL(
324 CloudPrintURL(profile).GetCloudPrintServiceDialogURL());
325 }
[email protected]73852b8f2010-05-14 00:38:12326 }
[email protected]0eb25c42011-08-11 14:50:14327 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50328 content::Source<NavigationController>(controller));
[email protected]4cd49022012-01-19 20:37:37329 if (close_after_signin_) {
330 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
331 content::Source<NavigationController>(controller));
332 }
[email protected]73852b8f2010-05-14 00:38:12333}
334
[email protected]6c2381d2011-10-19 02:52:53335void CloudPrintFlowHandler::Observe(
336 int type,
337 const content::NotificationSource& source,
338 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27339 if (type == content::NOTIFICATION_LOAD_STOP) {
[email protected]20c52d22011-06-20 22:42:42340 // Take the opportunity to set some (minimal) additional
341 // script permissions required for the web UI.
[email protected]01ec4ec2012-01-18 04:13:47342 GURL url = web_ui()->GetWebContents()->GetURL();
[email protected]20c52d22011-06-20 22:42:42343 GURL dialog_url = CloudPrintURL(
[email protected]46adf7ff2011-12-30 00:53:09344 Profile::FromWebUI(web_ui())).GetCloudPrintServiceDialogURL();
[email protected]20c52d22011-06-20 22:42:42345 if (url.host() == dialog_url.host() &&
346 url.path() == dialog_url.path() &&
347 url.scheme() == dialog_url.scheme()) {
[email protected]01ec4ec2012-01-18 04:13:47348 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
[email protected]746d3052012-05-22 15:15:47349 if (rvh) {
350 webkit_glue::WebPreferences webkit_prefs = rvh->GetWebkitPreferences();
[email protected]20c52d22011-06-20 22:42:42351 webkit_prefs.allow_scripts_to_close_windows = true;
[email protected]9abd51f2011-09-21 19:11:35352 rvh->UpdateWebkitPreferences(webkit_prefs);
[email protected]20c52d22011-06-20 22:42:42353 } else {
[email protected]746d3052012-05-22 15:15:47354 NOTREACHED();
[email protected]20c52d22011-06-20 22:42:42355 }
356 }
357
[email protected]73852b8f2010-05-14 00:38:12358 // Choose one or the other. If you need to debug, bring up the
359 // debugger. You can then use the various chrome.send()
360 // registrations above to kick of the various function calls,
361 // including chrome.send("SendPrintData") in the javaScript
362 // console and watch things happen with:
363 // HandleShowDebugger(NULL);
364 HandleSendPrintData(NULL);
365 }
[email protected]4cd49022012-01-19 20:37:37366 if (close_after_signin_ &&
367 type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) {
368 GURL url = web_ui()->GetWebContents()->GetURL();
369 GURL dialog_url = CloudPrintURL(
370 Profile::FromWebUI(web_ui())).GetCloudPrintServiceURL();
371
372 if (url.host() == dialog_url.host() &&
373 url.path() == dialog_url.path() &&
374 url.scheme() == dialog_url.scheme()) {
375 StoreDialogClientSize();
376 web_ui()->GetWebContents()->GetRenderViewHost()->ClosePage();
377 callback_.Run();
378 }
379 }
[email protected]73852b8f2010-05-14 00:38:12380}
381
[email protected]88942a22010-08-19 20:34:43382void CloudPrintFlowHandler::HandleShowDebugger(const ListValue* args) {
[email protected]73852b8f2010-05-14 00:38:12383 ShowDebugger();
384}
385
386void CloudPrintFlowHandler::ShowDebugger() {
[email protected]46adf7ff2011-12-30 00:53:09387 if (web_ui()) {
[email protected]01ec4ec2012-01-18 04:13:47388 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
[email protected]73852b8f2010-05-14 00:38:12389 if (rvh)
[email protected]aebdd072011-07-07 12:36:59390 DevToolsWindow::OpenDevToolsWindow(rvh);
[email protected]73852b8f2010-05-14 00:38:12391 }
392}
393
394scoped_refptr<CloudPrintDataSender>
395CloudPrintFlowHandler::CreateCloudPrintDataSender() {
[email protected]46adf7ff2011-12-30 00:53:09396 DCHECK(web_ui());
397 print_data_helper_.reset(new CloudPrintDataSenderHelper(web_ui()));
[email protected]a984bdf2011-03-15 20:17:16398 return new CloudPrintDataSender(print_data_helper_.get(),
399 print_job_title_,
[email protected]e8368e92011-08-20 04:05:56400 print_ticket_,
[email protected]a984bdf2011-03-15 20:17:16401 file_type_);
[email protected]73852b8f2010-05-14 00:38:12402}
403
[email protected]88942a22010-08-19 20:34:43404void CloudPrintFlowHandler::HandleSendPrintData(const ListValue* args) {
[email protected]ba4f1132010-10-09 02:02:35405 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12406 // This will cancel any ReadPrintDataFile() or SendPrintDataFile()
407 // requests in flight (this is anticipation of when setting page
408 // setup parameters becomes asynchronous and may be set while some
409 // data is in flight). Then we can clear out the print data.
410 CancelAnyRunningTask();
[email protected]46adf7ff2011-12-30 00:53:09411 if (web_ui()) {
[email protected]73852b8f2010-05-14 00:38:12412 print_data_sender_ = CreateCloudPrintDataSender();
[email protected]3e2dd4fa2011-11-10 06:06:40413 BrowserThread::PostTask(
414 BrowserThread::FILE, FROM_HERE,
415 base::Bind(&CloudPrintDataSender::ReadPrintDataFile,
416 print_data_sender_.get(), path_to_file_));
[email protected]73852b8f2010-05-14 00:38:12417 }
418}
419
[email protected]88942a22010-08-19 20:34:43420void CloudPrintFlowHandler::HandleSetPageParameters(const ListValue* args) {
[email protected]036056a32011-03-03 21:05:01421 std::string json;
[email protected]e675f7b2011-06-22 17:32:12422 bool ret = args->GetString(0, &json);
423 if (!ret || json.empty()) {
[email protected]036056a32011-03-03 21:05:01424 NOTREACHED() << "Empty json string";
[email protected]73852b8f2010-05-14 00:38:12425 return;
[email protected]036056a32011-03-03 21:05:01426 }
[email protected]73852b8f2010-05-14 00:38:12427
428 // These are backstop default values - 72 dpi to match the screen,
429 // 8.5x11 inch paper with margins subtracted (1/4 inch top, left,
430 // right and 0.56 bottom), and the min page shrink and max page
431 // shrink values appear all over the place with no explanation.
432
433 // TODO(scottbyer): Get a Linux/ChromeOS edge for PrintSettings
434 // working so that we can get the default values from there. Fix up
435 // PrintWebViewHelper to do the same.
436 const int kDPI = 72;
437 const int kWidth = static_cast<int>((8.5-0.25-0.25)*kDPI);
438 const int kHeight = static_cast<int>((11-0.25-0.56)*kDPI);
439 const double kMinPageShrink = 1.25;
440 const double kMaxPageShrink = 2.0;
441
[email protected]1375e3ab2011-03-24 17:07:22442 PrintMsg_Print_Params default_settings;
[email protected]10980442011-12-04 22:33:05443 default_settings.content_size = gfx::Size(kWidth, kHeight);
[email protected]732b8132012-01-10 23:17:32444 default_settings.printable_area = gfx::Rect(0, 0, kWidth, kHeight);
[email protected]73852b8f2010-05-14 00:38:12445 default_settings.dpi = kDPI;
446 default_settings.min_shrink = kMinPageShrink;
447 default_settings.max_shrink = kMaxPageShrink;
448 default_settings.desired_dpi = kDPI;
449 default_settings.document_cookie = 0;
450 default_settings.selection_only = false;
[email protected]718af822011-08-12 22:11:33451 default_settings.preview_request_id = 0;
452 default_settings.is_first_request = true;
[email protected]732b8132012-01-10 23:17:32453 default_settings.print_to_pdf = false;
[email protected]73852b8f2010-05-14 00:38:12454
455 if (!GetPageSetupParameters(json, default_settings)) {
456 NOTREACHED();
457 return;
458 }
459
460 // TODO(scottbyer) - Here is where we would kick the originating
461 // renderer thread with these new parameters in order to get it to
[email protected]a984bdf2011-03-15 20:17:16462 // re-generate the PDF data and hand it back to us. window.print() is
[email protected]73852b8f2010-05-14 00:38:12463 // currently synchronous, so there's a lot of work to do to get to
464 // that point.
465}
466
[email protected]ea161da2010-11-02 21:57:35467void CloudPrintFlowHandler::StoreDialogClientSize() const {
[email protected]01ec4ec2012-01-18 04:13:47468 if (web_ui() && web_ui()->GetWebContents() &&
469 web_ui()->GetWebContents()->GetView()) {
470 gfx::Size size = web_ui()->GetWebContents()->GetView()->GetContainerSize();
[email protected]46adf7ff2011-12-30 00:53:09471 Profile* profile = Profile::FromWebUI(web_ui());
[email protected]0eb25c42011-08-11 14:50:14472 profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogWidth,
473 size.width());
474 profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogHeight,
475 size.height());
[email protected]ea161da2010-11-02 21:57:35476 }
477}
478
[email protected]5835871a2012-04-25 21:56:55479CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate(
[email protected]b5b79d72012-05-24 19:42:28480 content::BrowserContext* browser_context,
481 gfx::NativeWindow modal_parent,
[email protected]a984bdf2011-03-15 20:17:16482 const FilePath& path_to_file,
[email protected]9848c7e2010-06-03 16:06:56483 const std::string& json_arguments,
[email protected]e39027a2011-01-24 21:41:54484 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56485 const string16& print_ticket,
[email protected]a984bdf2011-03-15 20:17:16486 const std::string& file_type,
[email protected]4cd49022012-01-19 20:37:37487 bool delete_on_close,
488 bool close_after_signin,
489 const base::Closure& callback)
[email protected]d955fc92011-09-19 20:49:03490 : delete_on_close_(delete_on_close),
491 flow_handler_(new CloudPrintFlowHandler(path_to_file,
[email protected]a984bdf2011-03-15 20:17:16492 print_job_title,
[email protected]e8368e92011-08-20 04:05:56493 print_ticket,
[email protected]4cd49022012-01-19 20:37:37494 file_type,
495 close_after_signin,
496 callback)),
[email protected]b5b79d72012-05-24 19:42:28497 modal_parent_(modal_parent),
[email protected]6ddda232011-04-22 15:41:47498 owns_flow_handler_(true),
[email protected]b5b79d72012-05-24 19:42:28499 path_to_file_(path_to_file),
500 keep_alive_when_non_modal_(true) {
501 Init(browser_context, json_arguments);
[email protected]73852b8f2010-05-14 00:38:12502}
503
[email protected]05acb55472011-02-03 00:11:07504// For unit testing.
[email protected]5835871a2012-04-25 21:56:55505CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate(
[email protected]b5b79d72012-05-24 19:42:28506 const FilePath& path_to_file,
[email protected]73852b8f2010-05-14 00:38:12507 CloudPrintFlowHandler* flow_handler,
[email protected]e39027a2011-01-24 21:41:54508 const std::string& json_arguments,
[email protected]d955fc92011-09-19 20:49:03509 bool delete_on_close)
510 : delete_on_close_(delete_on_close),
511 flow_handler_(flow_handler),
[email protected]b5b79d72012-05-24 19:42:28512 modal_parent_(NULL),
513 owns_flow_handler_(true),
514 keep_alive_when_non_modal_(false) {
515 Init(NULL, json_arguments);
[email protected]73852b8f2010-05-14 00:38:12516}
517
[email protected]b5b79d72012-05-24 19:42:28518// Returns the persisted width/height for the print dialog.
519void GetDialogWidthAndHeightFromPrefs(content::BrowserContext* browser_context,
520 int* width,
521 int* height) {
522 const int kDefaultWidth = 912;
523 const int kDefaultHeight = 633;
524 if (!browser_context) {
525 *width = kDefaultWidth;
526 *height = kDefaultHeight;
527 return;
528 }
529
530 Profile* profile = Profile::FromBrowserContext(browser_context);
531 if (!profile->GetPrefs()->FindPreference(prefs::kCloudPrintDialogWidth)) {
532 profile->GetPrefs()->RegisterIntegerPref(prefs::kCloudPrintDialogWidth,
533 kDefaultWidth,
534 PrefService::UNSYNCABLE_PREF);
535 }
536 if (!profile->GetPrefs()->FindPreference(prefs::kCloudPrintDialogHeight)) {
537 profile->GetPrefs()->RegisterIntegerPref(prefs::kCloudPrintDialogHeight,
538 kDefaultHeight,
539 PrefService::UNSYNCABLE_PREF);
540 }
541
542 *width = profile->GetPrefs()->GetInteger(prefs::kCloudPrintDialogWidth);
543 *height = profile->GetPrefs()->GetInteger(prefs::kCloudPrintDialogHeight);
544}
545
546void CloudPrintWebDialogDelegate::Init(content::BrowserContext* browser_context,
[email protected]5835871a2012-04-25 21:56:55547 const std::string& json_arguments) {
[email protected]73852b8f2010-05-14 00:38:12548 // This information is needed to show the dialog HTML content.
[email protected]ba4f1132010-10-09 02:02:35549 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b5b79d72012-05-24 19:42:28550
[email protected]89f550b2011-06-08 18:34:03551 params_.url = GURL(chrome::kChromeUICloudPrintResourcesURL);
[email protected]b5b79d72012-05-24 19:42:28552 GetDialogWidthAndHeightFromPrefs(browser_context,
553 &params_.width,
554 &params_.height);
[email protected]73852b8f2010-05-14 00:38:12555 params_.json_input = json_arguments;
556
557 flow_handler_->SetDialogDelegate(this);
[email protected]e39027a2011-01-24 21:41:54558 // If we're not modal we can show the dialog with no browser.
559 // We need this to keep Chrome alive while our dialog is up.
[email protected]b5b79d72012-05-24 19:42:28560 if (!modal_parent_ && keep_alive_when_non_modal_)
[email protected]2e6389f2012-05-18 19:41:25561 browser::StartKeepAlive();
[email protected]73852b8f2010-05-14 00:38:12562}
563
[email protected]5835871a2012-04-25 21:56:55564CloudPrintWebDialogDelegate::~CloudPrintWebDialogDelegate() {
[email protected]73852b8f2010-05-14 00:38:12565 // If the flow_handler_ is about to outlive us because we don't own
[email protected]a2c92a1c2012-04-03 12:32:14566 // it anymore, we need to have it remove its reference to us.
[email protected]ba4f1132010-10-09 02:02:35567 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12568 flow_handler_->SetDialogDelegate(NULL);
569 if (owns_flow_handler_) {
570 delete flow_handler_;
571 }
572}
573
[email protected]5835871a2012-04-25 21:56:55574ui::ModalType CloudPrintWebDialogDelegate::GetDialogModalType() const {
[email protected]b5b79d72012-05-24 19:42:28575 return modal_parent_ ? ui::MODAL_TYPE_WINDOW : ui::MODAL_TYPE_NONE;
[email protected]73852b8f2010-05-14 00:38:12576}
577
[email protected]5835871a2012-04-25 21:56:55578string16 CloudPrintWebDialogDelegate::GetDialogTitle() const {
[email protected]bdae5c12011-08-05 21:49:06579 return string16();
[email protected]73852b8f2010-05-14 00:38:12580}
581
[email protected]5835871a2012-04-25 21:56:55582GURL CloudPrintWebDialogDelegate::GetDialogContentURL() const {
[email protected]73852b8f2010-05-14 00:38:12583 return params_.url;
584}
585
[email protected]5835871a2012-04-25 21:56:55586void CloudPrintWebDialogDelegate::GetWebUIMessageHandlers(
[email protected]36e12172011-02-08 23:46:02587 std::vector<WebUIMessageHandler*>* handlers) const {
[email protected]73852b8f2010-05-14 00:38:12588 handlers->push_back(flow_handler_);
589 // We don't own flow_handler_ anymore, but it sticks around until at
590 // least right after OnDialogClosed() is called (and this object is
591 // destroyed).
592 owns_flow_handler_ = false;
593}
594
[email protected]5835871a2012-04-25 21:56:55595void CloudPrintWebDialogDelegate::GetDialogSize(gfx::Size* size) const {
[email protected]73852b8f2010-05-14 00:38:12596 size->set_width(params_.width);
597 size->set_height(params_.height);
598}
599
[email protected]5835871a2012-04-25 21:56:55600std::string CloudPrintWebDialogDelegate::GetDialogArgs() const {
[email protected]73852b8f2010-05-14 00:38:12601 return params_.json_input;
602}
603
[email protected]5835871a2012-04-25 21:56:55604void CloudPrintWebDialogDelegate::OnDialogClosed(
[email protected]73852b8f2010-05-14 00:38:12605 const std::string& json_retval) {
[email protected]ea161da2010-11-02 21:57:35606 // Get the final dialog size and store it.
607 flow_handler_->StoreDialogClientSize();
[email protected]6ddda232011-04-22 15:41:47608
[email protected]d955fc92011-09-19 20:49:03609 if (delete_on_close_) {
[email protected]3e2dd4fa2011-11-10 06:06:40610 BrowserThread::PostTask(
611 BrowserThread::FILE, FROM_HERE,
612 base::Bind(&internal_cloud_print_helpers::Delete, path_to_file_));
[email protected]6ddda232011-04-22 15:41:47613 }
614
[email protected]e39027a2011-01-24 21:41:54615 // If we're modal we can show the dialog with no browser.
616 // End the keep-alive so that Chrome can exit.
[email protected]b5b79d72012-05-24 19:42:28617 if (!modal_parent_ && keep_alive_when_non_modal_)
[email protected]2e6389f2012-05-18 19:41:25618 browser::EndKeepAlive();
[email protected]73852b8f2010-05-14 00:38:12619 delete this;
620}
621
[email protected]5835871a2012-04-25 21:56:55622void CloudPrintWebDialogDelegate::OnCloseContents(WebContents* source,
623 bool* out_close_dialog) {
[email protected]18137e02010-05-25 21:10:35624 if (out_close_dialog)
625 *out_close_dialog = true;
626}
627
[email protected]5835871a2012-04-25 21:56:55628bool CloudPrintWebDialogDelegate::ShouldShowDialogTitle() const {
[email protected]ea161da2010-11-02 21:57:35629 return false;
630}
631
[email protected]5835871a2012-04-25 21:56:55632bool CloudPrintWebDialogDelegate::HandleContextMenu(
[email protected]35be7ec2012-02-12 20:42:51633 const content::ContextMenuParams& params) {
[email protected]34478212011-04-19 01:35:46634 return true;
635}
636
[email protected]b5b79d72012-05-24 19:42:28637void CreatePrintDialogForBytesImpl(content::BrowserContext* browser_context,
638 gfx::NativeWindow modal_parent,
639 scoped_refptr<base::RefCountedBytes> data,
[email protected]d955fc92011-09-19 20:49:03640 const string16& print_job_title,
641 const string16& print_ticket,
[email protected]b5b79d72012-05-24 19:42:28642 const std::string& file_type) {
[email protected]d955fc92011-09-19 20:49:03643 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
644 // TODO([email protected]) Writing the PDF to a file before printing
645 // is wasteful. Modify the dialog flow to pull PDF data from memory.
646 // See http://code.google.com/p/chromium/issues/detail?id=44093
647 FilePath path;
648 if (file_util::CreateTemporaryFile(&path)) {
649 file_util::WriteFile(path,
650 reinterpret_cast<const char*>(data->front()),
651 data->size());
652 }
[email protected]b5b79d72012-05-24 19:42:28653 print_dialog_cloud::CreatePrintDialogForFile(browser_context,
654 modal_parent,
655 path,
[email protected]d955fc92011-09-19 20:49:03656 print_job_title,
657 print_ticket,
658 file_type,
[email protected]d955fc92011-09-19 20:49:03659 true);
660}
661
[email protected]6085c70d2011-03-22 22:51:07662// Called from the UI thread, starts up the dialog.
[email protected]b5b79d72012-05-24 19:42:28663void CreateDialogImpl(content::BrowserContext* browser_context,
664 gfx::NativeWindow modal_parent,
665 const FilePath& path_to_file,
[email protected]6085c70d2011-03-22 22:51:07666 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56667 const string16& print_ticket,
[email protected]6085c70d2011-03-22 22:51:07668 const std::string& file_type,
[email protected]4cd49022012-01-19 20:37:37669 bool delete_on_close,
670 bool close_after_signin,
671 const base::Closure& callback) {
[email protected]ba4f1132010-10-09 02:02:35672 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]5835871a2012-04-25 21:56:55673 WebDialogDelegate* dialog_delegate =
674 new internal_cloud_print_helpers::CloudPrintWebDialogDelegate(
[email protected]b5b79d72012-05-24 19:42:28675 browser_context,
676 modal_parent,
677 path_to_file,
678 std::string(),
679 print_job_title,
680 print_ticket,
681 file_type,
682 delete_on_close,
683 close_after_signin,
[email protected]4cd49022012-01-19 20:37:37684 callback);
[email protected]b5b79d72012-05-24 19:42:28685 browser::ShowWebDialog(modal_parent,
686 Profile::FromBrowserContext(browser_context),
687 NULL,
688 dialog_delegate);
[email protected]73852b8f2010-05-14 00:38:12689}
[email protected]6085c70d2011-03-22 22:51:07690
[email protected]b5b79d72012-05-24 19:42:28691void CreateDialogSigninImpl(content::BrowserContext* browser_context,
692 gfx::NativeWindow modal_parent,
693 const base::Closure& callback) {
694 CreateDialogImpl(browser_context, modal_parent, FilePath(), string16(),
695 string16(), std::string(), false, true, callback);
[email protected]4cd49022012-01-19 20:37:37696}
697
[email protected]b5b79d72012-05-24 19:42:28698void CreateDialogFullImpl(content::BrowserContext* browser_context,
699 gfx::NativeWindow modal_parent,
700 const FilePath& path_to_file,
701 const string16& print_job_title,
702 const string16& print_ticket,
703 const std::string& file_type,
704 bool delete_on_close) {
705 CreateDialogImpl(browser_context, modal_parent, path_to_file, print_job_title,
706 print_ticket, file_type, delete_on_close, false,
707 base::Closure());
[email protected]4cd49022012-01-19 20:37:37708}
709
710
711
[email protected]6ddda232011-04-22 15:41:47712// Provides a runnable function to delete a file.
713void Delete(const FilePath& file_path) {
714 file_util::Delete(file_path, false);
715}
716
[email protected]6085c70d2011-03-22 22:51:07717} // namespace internal_cloud_print_helpers
718
719namespace print_dialog_cloud {
720
721// Called on the FILE or UI thread. This is the main entry point into creating
722// the dialog.
723
724// TODO(scottbyer): The signature here will need to change as the
725// workflow through the printing code changes to allow for dynamically
726// changing page setup parameters while the dialog is active.
[email protected]b5b79d72012-05-24 19:42:28727void CreatePrintDialogForFile(content::BrowserContext* browser_context,
728 gfx::NativeWindow modal_parent,
729 const FilePath& path_to_file,
[email protected]6085c70d2011-03-22 22:51:07730 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56731 const string16& print_ticket,
[email protected]6085c70d2011-03-22 22:51:07732 const std::string& file_type,
[email protected]d955fc92011-09-19 20:49:03733 bool delete_on_close) {
[email protected]6085c70d2011-03-22 22:51:07734 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) ||
735 BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]6085c70d2011-03-22 22:51:07736 BrowserThread::PostTask(
737 BrowserThread::UI, FROM_HERE,
[email protected]4cd49022012-01-19 20:37:37738 base::Bind(&internal_cloud_print_helpers::CreateDialogFullImpl,
[email protected]b5b79d72012-05-24 19:42:28739 browser_context, modal_parent, path_to_file, print_job_title,
740 print_ticket, file_type, delete_on_close));
[email protected]d955fc92011-09-19 20:49:03741}
742
[email protected]b5b79d72012-05-24 19:42:28743void CreateCloudPrintSigninDialog(content::BrowserContext* browser_context,
744 gfx::NativeWindow modal_parent,
745 const base::Closure& callback) {
[email protected]4cd49022012-01-19 20:37:37746 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
747
748 BrowserThread::PostTask(
749 BrowserThread::UI, FROM_HERE,
750 base::Bind(&internal_cloud_print_helpers::CreateDialogSigninImpl,
[email protected]b5b79d72012-05-24 19:42:28751 browser_context,
752 modal_parent,
[email protected]4cd49022012-01-19 20:37:37753 callback));
754}
755
[email protected]b5b79d72012-05-24 19:42:28756void CreatePrintDialogForBytes(content::BrowserContext* browser_context,
757 gfx::NativeWindow modal_parent,
758 scoped_refptr<base::RefCountedBytes> data,
[email protected]d955fc92011-09-19 20:49:03759 const string16& print_job_title,
760 const string16& print_ticket,
[email protected]b5b79d72012-05-24 19:42:28761 const std::string& file_type) {
[email protected]d955fc92011-09-19 20:49:03762 // TODO([email protected]) Avoid cloning the PDF data. Make use of a
763 // shared memory object instead.
764 // http://code.google.com/p/chromium/issues/detail?id=44093
[email protected]ebbccb952012-04-20 09:51:31765 scoped_refptr<base::RefCountedBytes> cloned_data =
766 new base::RefCountedBytes(data->data());
[email protected]d955fc92011-09-19 20:49:03767 BrowserThread::PostTask(
768 BrowserThread::FILE, FROM_HERE,
[email protected]3e2dd4fa2011-11-10 06:06:40769 base::Bind(&internal_cloud_print_helpers::CreatePrintDialogForBytesImpl,
[email protected]b5b79d72012-05-24 19:42:28770 browser_context, modal_parent, cloned_data, print_job_title,
771 print_ticket, file_type));
[email protected]6085c70d2011-03-22 22:51:07772}
773
[email protected]65c9d89a2011-04-13 21:02:39774bool CreatePrintDialogFromCommandLine(const CommandLine& command_line) {
[email protected]87ab41e72012-01-04 18:45:11775 DCHECK(command_line.HasSwitch(switches::kCloudPrintFile));
[email protected]65c9d89a2011-04-13 21:02:39776 if (!command_line.GetSwitchValuePath(switches::kCloudPrintFile).empty()) {
777 FilePath cloud_print_file;
778 cloud_print_file =
779 command_line.GetSwitchValuePath(switches::kCloudPrintFile);
780 if (!cloud_print_file.empty()) {
781 string16 print_job_title;
[email protected]e8368e92011-08-20 04:05:56782 string16 print_job_print_ticket;
[email protected]65c9d89a2011-04-13 21:02:39783 if (command_line.HasSwitch(switches::kCloudPrintJobTitle)) {
[email protected]e8368e92011-08-20 04:05:56784 print_job_title =
785 internal_cloud_print_helpers::GetSwitchValueString16(
786 command_line, switches::kCloudPrintJobTitle);
787 }
788 if (command_line.HasSwitch(switches::kCloudPrintPrintTicket)) {
789 print_job_print_ticket =
790 internal_cloud_print_helpers::GetSwitchValueString16(
791 command_line, switches::kCloudPrintPrintTicket);
[email protected]65c9d89a2011-04-13 21:02:39792 }
793 std::string file_type = "application/pdf";
794 if (command_line.HasSwitch(switches::kCloudPrintFileType)) {
795 file_type = command_line.GetSwitchValueASCII(
796 switches::kCloudPrintFileType);
797 }
[email protected]e8368e92011-08-20 04:05:56798
[email protected]d955fc92011-09-19 20:49:03799 bool delete_on_close = CommandLine::ForCurrentProcess()->HasSwitch(
800 switches::kCloudPrintDeleteFile);
801
[email protected]b5b79d72012-05-24 19:42:28802 print_dialog_cloud::CreatePrintDialogForFile(
803 ProfileManager::GetDefaultProfile(),
804 NULL,
805 cloud_print_file,
806 print_job_title,
807 print_job_print_ticket,
808 file_type,
809 delete_on_close);
[email protected]65c9d89a2011-04-13 21:02:39810 return true;
811 }
812 }
813 return false;
814}
815
[email protected]6085c70d2011-03-22 22:51:07816} // end namespace