blob: 5ba846208e16f15ae5c55a948be35093c2bcf1d9 [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]631bb742011-11-02 11:29:39115
[email protected]73852b8f2010-05-14 00:38:12116namespace internal_cloud_print_helpers {
117
[email protected]73852b8f2010-05-14 00:38:12118// From the JSON parsed value, get the entries for the page setup
119// parameters.
120bool GetPageSetupParameters(const std::string& json,
[email protected]1375e3ab2011-03-24 17:07:22121 PrintMsg_Print_Params& parameters) {
[email protected]cd5785752012-04-11 00:15:41122 scoped_ptr<Value> parsed_value(base::JSONReader::Read(json));
[email protected]73852b8f2010-05-14 00:38:12123 DLOG_IF(ERROR, (!parsed_value.get() ||
124 !parsed_value->IsType(Value::TYPE_DICTIONARY)))
125 << "PageSetup call didn't have expected contents";
126 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY))
127 return false;
128
129 bool result = true;
130 DictionaryValue* params = static_cast<DictionaryValue*>(parsed_value.get());
[email protected]05c7da62011-05-05 17:23:56131 result &= params->GetDouble("dpi", &parameters.dpi);
132 result &= params->GetDouble("min_shrink", &parameters.min_shrink);
133 result &= params->GetDouble("max_shrink", &parameters.max_shrink);
[email protected]a65175d2010-08-17 04:00:57134 result &= params->GetBoolean("selection_only", &parameters.selection_only);
[email protected]73852b8f2010-05-14 00:38:12135 return result;
136}
137
[email protected]e8368e92011-08-20 04:05:56138string16 GetSwitchValueString16(const CommandLine& command_line,
139 const char* switchName) {
140#ifdef OS_WIN
141 CommandLine::StringType native_switch_val;
142 native_switch_val = command_line.GetSwitchValueNative(switchName);
143 return string16(native_switch_val);
144#elif defined(OS_POSIX)
145 // POSIX Command line string types are different.
146 CommandLine::StringType native_switch_val;
147 native_switch_val = command_line.GetSwitchValueASCII(switchName);
148 // Convert the ASCII string to UTF16 to prepare to pass.
149 return string16(ASCIIToUTF16(native_switch_val));
150#endif
151}
152
[email protected]73852b8f2010-05-14 00:38:12153void CloudPrintDataSenderHelper::CallJavascriptFunction(
154 const std::wstring& function_name) {
[email protected]adcf8492011-03-09 22:41:39155 web_ui_->CallJavascriptFunction(WideToASCII(function_name));
[email protected]73852b8f2010-05-14 00:38:12156}
157
158void CloudPrintDataSenderHelper::CallJavascriptFunction(
159 const std::wstring& function_name, const Value& arg) {
[email protected]adcf8492011-03-09 22:41:39160 web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg);
[email protected]73852b8f2010-05-14 00:38:12161}
162
163void CloudPrintDataSenderHelper::CallJavascriptFunction(
164 const std::wstring& function_name, const Value& arg1, const Value& arg2) {
[email protected]adcf8492011-03-09 22:41:39165 web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg1, arg2);
[email protected]73852b8f2010-05-14 00:38:12166}
167
[email protected]e8368e92011-08-20 04:05:56168void CloudPrintDataSenderHelper::CallJavascriptFunction(
169 const std::wstring& function_name,
170 const Value& arg1,
171 const Value& arg2,
172 const Value& arg3) {
[email protected]46adf7ff2011-12-30 00:53:09173 web_ui_->CallJavascriptFunction(
174 WideToASCII(function_name), arg1, arg2, arg3);
[email protected]e8368e92011-08-20 04:05:56175}
176
[email protected]73852b8f2010-05-14 00:38:12177// Clears out the pointer we're using to communicate. Either routine is
178// potentially expensive enough that stopping whatever is in progress
179// is worth it.
180void CloudPrintDataSender::CancelPrintDataFile() {
[email protected]20305ec2011-01-21 04:55:52181 base::AutoLock lock(lock_);
[email protected]73852b8f2010-05-14 00:38:12182 // We don't own helper, it was passed in to us, so no need to
183 // delete, just let it go.
184 helper_ = NULL;
185}
186
[email protected]38e08982010-10-22 17:28:43187CloudPrintDataSender::CloudPrintDataSender(CloudPrintDataSenderHelper* helper,
[email protected]a984bdf2011-03-15 20:17:16188 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56189 const string16& print_ticket,
[email protected]a984bdf2011-03-15 20:17:16190 const std::string& file_type)
[email protected]38e08982010-10-22 17:28:43191 : helper_(helper),
[email protected]a984bdf2011-03-15 20:17:16192 print_job_title_(print_job_title),
[email protected]e8368e92011-08-20 04:05:56193 print_ticket_(print_ticket),
[email protected]a984bdf2011-03-15 20:17:16194 file_type_(file_type) {
[email protected]38e08982010-10-22 17:28:43195}
196
197CloudPrintDataSender::~CloudPrintDataSender() {}
198
[email protected]a984bdf2011-03-15 20:17:16199// Grab the raw file contents and massage them into shape for
[email protected]73852b8f2010-05-14 00:38:12200// sending to the dialog contents (and up to the cloud print server)
201// by encoding it and prefixing it with the appropriate mime type.
202// Once that is done, kick off the next part of the task on the IO
203// thread.
[email protected]a984bdf2011-03-15 20:17:16204void CloudPrintDataSender::ReadPrintDataFile(const FilePath& path_to_file) {
[email protected]ba4f1132010-10-09 02:02:35205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]73852b8f2010-05-14 00:38:12206 int64 file_size = 0;
[email protected]a984bdf2011-03-15 20:17:16207 if (file_util::GetFileSize(path_to_file, &file_size) && file_size != 0) {
[email protected]73852b8f2010-05-14 00:38:12208 std::string file_data;
209 if (file_size < kuint32max) {
210 file_data.reserve(static_cast<unsigned int>(file_size));
211 } else {
212 DLOG(WARNING) << " print data file too large to reserve space";
213 }
[email protected]a984bdf2011-03-15 20:17:16214 if (helper_ && file_util::ReadFileToString(path_to_file, &file_data)) {
[email protected]73852b8f2010-05-14 00:38:12215 std::string base64_data;
216 base::Base64Encode(file_data, &base64_data);
[email protected]a984bdf2011-03-15 20:17:16217 std::string header("data:");
218 header.append(file_type_);
219 header.append(";base64,");
[email protected]73852b8f2010-05-14 00:38:12220 base64_data.insert(0, header);
221 scoped_ptr<StringValue> new_data(new StringValue(base64_data));
222 print_data_.swap(new_data);
[email protected]3e2dd4fa2011-11-10 06:06:40223 BrowserThread::PostTask(
224 BrowserThread::IO, FROM_HERE,
225 base::Bind(&CloudPrintDataSender::SendPrintDataFile, this));
[email protected]73852b8f2010-05-14 00:38:12226 }
227 }
228}
229
230// We have the data in hand that needs to be pushed into the dialog
231// contents; do so from the IO thread.
232
233// TODO(scottbyer): If the print data ends up being larger than the
234// upload limit (currently 10MB), what we need to do is upload that
235// large data to google docs and set the URL in the printing
236// JavaScript to that location, and make sure it gets deleted when not
237// needed. - 4/1/2010
238void CloudPrintDataSender::SendPrintDataFile() {
[email protected]ba4f1132010-10-09 02:02:35239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]20305ec2011-01-21 04:55:52240 base::AutoLock lock(lock_);
[email protected]73852b8f2010-05-14 00:38:12241 if (helper_ && print_data_.get()) {
[email protected]9848c7e2010-06-03 16:06:56242 StringValue title(print_job_title_);
[email protected]e8368e92011-08-20 04:05:56243 StringValue ticket(print_ticket_);
244 // TODO(abodenha): Change Javascript call to pass in print ticket
245 // after server side support is added. Add test for it.
[email protected]73852b8f2010-05-14 00:38:12246
247 // Send the print data to the dialog contents. The JavaScript
248 // function is a preliminary API for prototyping purposes and is
249 // subject to change.
250 const_cast<CloudPrintDataSenderHelper*>(helper_)->CallJavascriptFunction(
251 L"printApp._printDataUrl", *print_data_, title);
252 }
253}
254
255
[email protected]a984bdf2011-03-15 20:17:16256CloudPrintFlowHandler::CloudPrintFlowHandler(const FilePath& path_to_file,
257 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56258 const string16& print_ticket,
[email protected]4cd49022012-01-19 20:37:37259 const std::string& file_type,
260 bool close_after_signin,
261 const base::Closure& callback)
[email protected]c7bf7452011-09-12 21:31:50262 : dialog_delegate_(NULL),
263 path_to_file_(path_to_file),
[email protected]a984bdf2011-03-15 20:17:16264 print_job_title_(print_job_title),
[email protected]e8368e92011-08-20 04:05:56265 print_ticket_(print_ticket),
[email protected]4cd49022012-01-19 20:37:37266 file_type_(file_type),
267 close_after_signin_(close_after_signin),
268 callback_(callback) {
[email protected]38e08982010-10-22 17:28:43269}
270
271CloudPrintFlowHandler::~CloudPrintFlowHandler() {
272 // This will also cancel any task in flight.
273 CancelAnyRunningTask();
274}
275
276
[email protected]73852b8f2010-05-14 00:38:12277void CloudPrintFlowHandler::SetDialogDelegate(
[email protected]5835871a2012-04-25 21:56:55278 CloudPrintWebDialogDelegate* delegate) {
[email protected]7b748982011-02-14 19:28:23279 // Even if setting a new WebUI, it means any previous task needs
[email protected]a2c92a1c2012-04-03 12:32:14280 // to be canceled, its now invalid.
[email protected]ba4f1132010-10-09 02:02:35281 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12282 CancelAnyRunningTask();
283 dialog_delegate_ = delegate;
284}
285
286// Cancels any print data sender we have in flight and removes our
287// reference to it, so when the task that is calling it finishes and
[email protected]a2c92a1c2012-04-03 12:32:14288// removes its reference, it goes away.
[email protected]73852b8f2010-05-14 00:38:12289void CloudPrintFlowHandler::CancelAnyRunningTask() {
[email protected]ba4f1132010-10-09 02:02:35290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12291 if (print_data_sender_.get()) {
292 print_data_sender_->CancelPrintDataFile();
293 print_data_sender_ = NULL;
294 }
295}
296
[email protected]73852b8f2010-05-14 00:38:12297void CloudPrintFlowHandler::RegisterMessages() {
[email protected]73852b8f2010-05-14 00:38:12298 // TODO(scottbyer) - This is where we will register messages for the
299 // UI JS to use. Needed: Call to update page setup parameters.
[email protected]46adf7ff2011-12-30 00:53:09300 web_ui()->RegisterMessageCallback("ShowDebugger",
[email protected]ba4fc242011-10-04 18:56:56301 base::Bind(&CloudPrintFlowHandler::HandleShowDebugger,
302 base::Unretained(this)));
[email protected]46adf7ff2011-12-30 00:53:09303 web_ui()->RegisterMessageCallback("SendPrintData",
[email protected]ba4fc242011-10-04 18:56:56304 base::Bind(&CloudPrintFlowHandler::HandleSendPrintData,
305 base::Unretained(this)));
[email protected]46adf7ff2011-12-30 00:53:09306 web_ui()->RegisterMessageCallback("SetPageParameters",
[email protected]ba4fc242011-10-04 18:56:56307 base::Bind(&CloudPrintFlowHandler::HandleSetPageParameters,
308 base::Unretained(this)));
[email protected]73852b8f2010-05-14 00:38:12309
[email protected]0eb25c42011-08-11 14:50:14310 // Register for appropriate notifications, and re-direct the URL
311 // to the real server URL, now that we've gotten an HTML dialog
312 // going.
[email protected]c5eed492012-01-04 17:07:50313 NavigationController* controller =
[email protected]01ec4ec2012-01-18 04:13:47314 &web_ui()->GetWebContents()->GetController();
[email protected]10f417c52011-12-28 21:04:23315 NavigationEntry* pending_entry = controller->GetPendingEntry();
[email protected]0eb25c42011-08-11 14:50:14316 if (pending_entry) {
[email protected]46adf7ff2011-12-30 00:53:09317 Profile* profile = Profile::FromWebUI(web_ui());
[email protected]4cd49022012-01-19 20:37:37318 if (close_after_signin_) {
319 pending_entry->SetURL(
320 CloudPrintURL(profile).GetCloudPrintSigninURL());
321 } else {
322 pending_entry->SetURL(
323 CloudPrintURL(profile).GetCloudPrintServiceDialogURL());
324 }
[email protected]73852b8f2010-05-14 00:38:12325 }
[email protected]0eb25c42011-08-11 14:50:14326 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50327 content::Source<NavigationController>(controller));
[email protected]4cd49022012-01-19 20:37:37328 if (close_after_signin_) {
329 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
330 content::Source<NavigationController>(controller));
331 }
[email protected]73852b8f2010-05-14 00:38:12332}
333
[email protected]6c2381d2011-10-19 02:52:53334void CloudPrintFlowHandler::Observe(
335 int type,
336 const content::NotificationSource& source,
337 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27338 if (type == content::NOTIFICATION_LOAD_STOP) {
[email protected]20c52d22011-06-20 22:42:42339 // Take the opportunity to set some (minimal) additional
340 // script permissions required for the web UI.
[email protected]01ec4ec2012-01-18 04:13:47341 GURL url = web_ui()->GetWebContents()->GetURL();
[email protected]20c52d22011-06-20 22:42:42342 GURL dialog_url = CloudPrintURL(
[email protected]46adf7ff2011-12-30 00:53:09343 Profile::FromWebUI(web_ui())).GetCloudPrintServiceDialogURL();
[email protected]20c52d22011-06-20 22:42:42344 if (url.host() == dialog_url.host() &&
345 url.path() == dialog_url.path() &&
346 url.scheme() == dialog_url.scheme()) {
[email protected]01ec4ec2012-01-18 04:13:47347 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
[email protected]746d3052012-05-22 15:15:47348 if (rvh) {
349 webkit_glue::WebPreferences webkit_prefs = rvh->GetWebkitPreferences();
[email protected]20c52d22011-06-20 22:42:42350 webkit_prefs.allow_scripts_to_close_windows = true;
[email protected]9abd51f2011-09-21 19:11:35351 rvh->UpdateWebkitPreferences(webkit_prefs);
[email protected]20c52d22011-06-20 22:42:42352 } else {
[email protected]746d3052012-05-22 15:15:47353 NOTREACHED();
[email protected]20c52d22011-06-20 22:42:42354 }
355 }
356
[email protected]73852b8f2010-05-14 00:38:12357 // Choose one or the other. If you need to debug, bring up the
358 // debugger. You can then use the various chrome.send()
359 // registrations above to kick of the various function calls,
360 // including chrome.send("SendPrintData") in the javaScript
361 // console and watch things happen with:
362 // HandleShowDebugger(NULL);
363 HandleSendPrintData(NULL);
364 }
[email protected]4cd49022012-01-19 20:37:37365 if (close_after_signin_ &&
366 type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) {
367 GURL url = web_ui()->GetWebContents()->GetURL();
368 GURL dialog_url = CloudPrintURL(
369 Profile::FromWebUI(web_ui())).GetCloudPrintServiceURL();
370
371 if (url.host() == dialog_url.host() &&
372 url.path() == dialog_url.path() &&
373 url.scheme() == dialog_url.scheme()) {
374 StoreDialogClientSize();
375 web_ui()->GetWebContents()->GetRenderViewHost()->ClosePage();
376 callback_.Run();
377 }
378 }
[email protected]73852b8f2010-05-14 00:38:12379}
380
[email protected]88942a22010-08-19 20:34:43381void CloudPrintFlowHandler::HandleShowDebugger(const ListValue* args) {
[email protected]73852b8f2010-05-14 00:38:12382 ShowDebugger();
383}
384
385void CloudPrintFlowHandler::ShowDebugger() {
[email protected]46adf7ff2011-12-30 00:53:09386 if (web_ui()) {
[email protected]01ec4ec2012-01-18 04:13:47387 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
[email protected]73852b8f2010-05-14 00:38:12388 if (rvh)
[email protected]aebdd072011-07-07 12:36:59389 DevToolsWindow::OpenDevToolsWindow(rvh);
[email protected]73852b8f2010-05-14 00:38:12390 }
391}
392
393scoped_refptr<CloudPrintDataSender>
394CloudPrintFlowHandler::CreateCloudPrintDataSender() {
[email protected]46adf7ff2011-12-30 00:53:09395 DCHECK(web_ui());
396 print_data_helper_.reset(new CloudPrintDataSenderHelper(web_ui()));
[email protected]a984bdf2011-03-15 20:17:16397 return new CloudPrintDataSender(print_data_helper_.get(),
398 print_job_title_,
[email protected]e8368e92011-08-20 04:05:56399 print_ticket_,
[email protected]a984bdf2011-03-15 20:17:16400 file_type_);
[email protected]73852b8f2010-05-14 00:38:12401}
402
[email protected]88942a22010-08-19 20:34:43403void CloudPrintFlowHandler::HandleSendPrintData(const ListValue* args) {
[email protected]ba4f1132010-10-09 02:02:35404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12405 // This will cancel any ReadPrintDataFile() or SendPrintDataFile()
406 // requests in flight (this is anticipation of when setting page
407 // setup parameters becomes asynchronous and may be set while some
408 // data is in flight). Then we can clear out the print data.
409 CancelAnyRunningTask();
[email protected]46adf7ff2011-12-30 00:53:09410 if (web_ui()) {
[email protected]73852b8f2010-05-14 00:38:12411 print_data_sender_ = CreateCloudPrintDataSender();
[email protected]3e2dd4fa2011-11-10 06:06:40412 BrowserThread::PostTask(
413 BrowserThread::FILE, FROM_HERE,
414 base::Bind(&CloudPrintDataSender::ReadPrintDataFile,
415 print_data_sender_.get(), path_to_file_));
[email protected]73852b8f2010-05-14 00:38:12416 }
417}
418
[email protected]88942a22010-08-19 20:34:43419void CloudPrintFlowHandler::HandleSetPageParameters(const ListValue* args) {
[email protected]036056a32011-03-03 21:05:01420 std::string json;
[email protected]e675f7b2011-06-22 17:32:12421 bool ret = args->GetString(0, &json);
422 if (!ret || json.empty()) {
[email protected]036056a32011-03-03 21:05:01423 NOTREACHED() << "Empty json string";
[email protected]73852b8f2010-05-14 00:38:12424 return;
[email protected]036056a32011-03-03 21:05:01425 }
[email protected]73852b8f2010-05-14 00:38:12426
427 // These are backstop default values - 72 dpi to match the screen,
428 // 8.5x11 inch paper with margins subtracted (1/4 inch top, left,
429 // right and 0.56 bottom), and the min page shrink and max page
430 // shrink values appear all over the place with no explanation.
431
432 // TODO(scottbyer): Get a Linux/ChromeOS edge for PrintSettings
433 // working so that we can get the default values from there. Fix up
434 // PrintWebViewHelper to do the same.
435 const int kDPI = 72;
436 const int kWidth = static_cast<int>((8.5-0.25-0.25)*kDPI);
437 const int kHeight = static_cast<int>((11-0.25-0.56)*kDPI);
438 const double kMinPageShrink = 1.25;
439 const double kMaxPageShrink = 2.0;
440
[email protected]1375e3ab2011-03-24 17:07:22441 PrintMsg_Print_Params default_settings;
[email protected]10980442011-12-04 22:33:05442 default_settings.content_size = gfx::Size(kWidth, kHeight);
[email protected]732b8132012-01-10 23:17:32443 default_settings.printable_area = gfx::Rect(0, 0, kWidth, kHeight);
[email protected]73852b8f2010-05-14 00:38:12444 default_settings.dpi = kDPI;
445 default_settings.min_shrink = kMinPageShrink;
446 default_settings.max_shrink = kMaxPageShrink;
447 default_settings.desired_dpi = kDPI;
448 default_settings.document_cookie = 0;
449 default_settings.selection_only = false;
[email protected]718af822011-08-12 22:11:33450 default_settings.preview_request_id = 0;
451 default_settings.is_first_request = true;
[email protected]732b8132012-01-10 23:17:32452 default_settings.print_to_pdf = false;
[email protected]73852b8f2010-05-14 00:38:12453
454 if (!GetPageSetupParameters(json, default_settings)) {
455 NOTREACHED();
456 return;
457 }
458
459 // TODO(scottbyer) - Here is where we would kick the originating
460 // renderer thread with these new parameters in order to get it to
[email protected]a984bdf2011-03-15 20:17:16461 // re-generate the PDF data and hand it back to us. window.print() is
[email protected]73852b8f2010-05-14 00:38:12462 // currently synchronous, so there's a lot of work to do to get to
463 // that point.
464}
465
[email protected]ea161da2010-11-02 21:57:35466void CloudPrintFlowHandler::StoreDialogClientSize() const {
[email protected]01ec4ec2012-01-18 04:13:47467 if (web_ui() && web_ui()->GetWebContents() &&
468 web_ui()->GetWebContents()->GetView()) {
469 gfx::Size size = web_ui()->GetWebContents()->GetView()->GetContainerSize();
[email protected]46adf7ff2011-12-30 00:53:09470 Profile* profile = Profile::FromWebUI(web_ui());
[email protected]0eb25c42011-08-11 14:50:14471 profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogWidth,
472 size.width());
473 profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogHeight,
474 size.height());
[email protected]ea161da2010-11-02 21:57:35475 }
476}
477
[email protected]5835871a2012-04-25 21:56:55478CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate(
[email protected]b5b79d72012-05-24 19:42:28479 content::BrowserContext* browser_context,
480 gfx::NativeWindow modal_parent,
[email protected]a984bdf2011-03-15 20:17:16481 const FilePath& path_to_file,
[email protected]9848c7e2010-06-03 16:06:56482 const std::string& json_arguments,
[email protected]e39027a2011-01-24 21:41:54483 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56484 const string16& print_ticket,
[email protected]a984bdf2011-03-15 20:17:16485 const std::string& file_type,
[email protected]4cd49022012-01-19 20:37:37486 bool delete_on_close,
487 bool close_after_signin,
488 const base::Closure& callback)
[email protected]d955fc92011-09-19 20:49:03489 : delete_on_close_(delete_on_close),
490 flow_handler_(new CloudPrintFlowHandler(path_to_file,
[email protected]a984bdf2011-03-15 20:17:16491 print_job_title,
[email protected]e8368e92011-08-20 04:05:56492 print_ticket,
[email protected]4cd49022012-01-19 20:37:37493 file_type,
494 close_after_signin,
495 callback)),
[email protected]b5b79d72012-05-24 19:42:28496 modal_parent_(modal_parent),
[email protected]6ddda232011-04-22 15:41:47497 owns_flow_handler_(true),
[email protected]b5b79d72012-05-24 19:42:28498 path_to_file_(path_to_file),
499 keep_alive_when_non_modal_(true) {
500 Init(browser_context, json_arguments);
[email protected]73852b8f2010-05-14 00:38:12501}
502
[email protected]05acb55472011-02-03 00:11:07503// For unit testing.
[email protected]5835871a2012-04-25 21:56:55504CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate(
[email protected]b5b79d72012-05-24 19:42:28505 const FilePath& path_to_file,
[email protected]73852b8f2010-05-14 00:38:12506 CloudPrintFlowHandler* flow_handler,
[email protected]e39027a2011-01-24 21:41:54507 const std::string& json_arguments,
[email protected]d955fc92011-09-19 20:49:03508 bool delete_on_close)
509 : delete_on_close_(delete_on_close),
510 flow_handler_(flow_handler),
[email protected]b5b79d72012-05-24 19:42:28511 modal_parent_(NULL),
512 owns_flow_handler_(true),
513 keep_alive_when_non_modal_(false) {
514 Init(NULL, json_arguments);
[email protected]73852b8f2010-05-14 00:38:12515}
516
[email protected]b5b79d72012-05-24 19:42:28517// Returns the persisted width/height for the print dialog.
518void GetDialogWidthAndHeightFromPrefs(content::BrowserContext* browser_context,
519 int* width,
520 int* height) {
521 const int kDefaultWidth = 912;
522 const int kDefaultHeight = 633;
523 if (!browser_context) {
524 *width = kDefaultWidth;
525 *height = kDefaultHeight;
526 return;
527 }
528
529 Profile* profile = Profile::FromBrowserContext(browser_context);
530 if (!profile->GetPrefs()->FindPreference(prefs::kCloudPrintDialogWidth)) {
531 profile->GetPrefs()->RegisterIntegerPref(prefs::kCloudPrintDialogWidth,
532 kDefaultWidth,
533 PrefService::UNSYNCABLE_PREF);
534 }
535 if (!profile->GetPrefs()->FindPreference(prefs::kCloudPrintDialogHeight)) {
536 profile->GetPrefs()->RegisterIntegerPref(prefs::kCloudPrintDialogHeight,
537 kDefaultHeight,
538 PrefService::UNSYNCABLE_PREF);
539 }
540
541 *width = profile->GetPrefs()->GetInteger(prefs::kCloudPrintDialogWidth);
542 *height = profile->GetPrefs()->GetInteger(prefs::kCloudPrintDialogHeight);
543}
544
545void CloudPrintWebDialogDelegate::Init(content::BrowserContext* browser_context,
[email protected]5835871a2012-04-25 21:56:55546 const std::string& json_arguments) {
[email protected]73852b8f2010-05-14 00:38:12547 // This information is needed to show the dialog HTML content.
[email protected]ba4f1132010-10-09 02:02:35548 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b5b79d72012-05-24 19:42:28549
[email protected]89f550b2011-06-08 18:34:03550 params_.url = GURL(chrome::kChromeUICloudPrintResourcesURL);
[email protected]b5b79d72012-05-24 19:42:28551 GetDialogWidthAndHeightFromPrefs(browser_context,
552 &params_.width,
553 &params_.height);
[email protected]73852b8f2010-05-14 00:38:12554 params_.json_input = json_arguments;
555
556 flow_handler_->SetDialogDelegate(this);
[email protected]e39027a2011-01-24 21:41:54557 // If we're not modal we can show the dialog with no browser.
558 // We need this to keep Chrome alive while our dialog is up.
[email protected]b5b79d72012-05-24 19:42:28559 if (!modal_parent_ && keep_alive_when_non_modal_)
[email protected]2e6389f2012-05-18 19:41:25560 browser::StartKeepAlive();
[email protected]73852b8f2010-05-14 00:38:12561}
562
[email protected]5835871a2012-04-25 21:56:55563CloudPrintWebDialogDelegate::~CloudPrintWebDialogDelegate() {
[email protected]73852b8f2010-05-14 00:38:12564 // If the flow_handler_ is about to outlive us because we don't own
[email protected]a2c92a1c2012-04-03 12:32:14565 // it anymore, we need to have it remove its reference to us.
[email protected]ba4f1132010-10-09 02:02:35566 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12567 flow_handler_->SetDialogDelegate(NULL);
568 if (owns_flow_handler_) {
569 delete flow_handler_;
570 }
571}
572
[email protected]5835871a2012-04-25 21:56:55573ui::ModalType CloudPrintWebDialogDelegate::GetDialogModalType() const {
[email protected]b5b79d72012-05-24 19:42:28574 return modal_parent_ ? ui::MODAL_TYPE_WINDOW : ui::MODAL_TYPE_NONE;
[email protected]73852b8f2010-05-14 00:38:12575}
576
[email protected]5835871a2012-04-25 21:56:55577string16 CloudPrintWebDialogDelegate::GetDialogTitle() const {
[email protected]bdae5c12011-08-05 21:49:06578 return string16();
[email protected]73852b8f2010-05-14 00:38:12579}
580
[email protected]5835871a2012-04-25 21:56:55581GURL CloudPrintWebDialogDelegate::GetDialogContentURL() const {
[email protected]73852b8f2010-05-14 00:38:12582 return params_.url;
583}
584
[email protected]5835871a2012-04-25 21:56:55585void CloudPrintWebDialogDelegate::GetWebUIMessageHandlers(
[email protected]36e12172011-02-08 23:46:02586 std::vector<WebUIMessageHandler*>* handlers) const {
[email protected]73852b8f2010-05-14 00:38:12587 handlers->push_back(flow_handler_);
588 // We don't own flow_handler_ anymore, but it sticks around until at
589 // least right after OnDialogClosed() is called (and this object is
590 // destroyed).
591 owns_flow_handler_ = false;
592}
593
[email protected]5835871a2012-04-25 21:56:55594void CloudPrintWebDialogDelegate::GetDialogSize(gfx::Size* size) const {
[email protected]73852b8f2010-05-14 00:38:12595 size->set_width(params_.width);
596 size->set_height(params_.height);
597}
598
[email protected]5835871a2012-04-25 21:56:55599std::string CloudPrintWebDialogDelegate::GetDialogArgs() const {
[email protected]73852b8f2010-05-14 00:38:12600 return params_.json_input;
601}
602
[email protected]5835871a2012-04-25 21:56:55603void CloudPrintWebDialogDelegate::OnDialogClosed(
[email protected]73852b8f2010-05-14 00:38:12604 const std::string& json_retval) {
[email protected]ea161da2010-11-02 21:57:35605 // Get the final dialog size and store it.
606 flow_handler_->StoreDialogClientSize();
[email protected]6ddda232011-04-22 15:41:47607
[email protected]d955fc92011-09-19 20:49:03608 if (delete_on_close_) {
[email protected]3e2dd4fa2011-11-10 06:06:40609 BrowserThread::PostTask(
610 BrowserThread::FILE, FROM_HERE,
611 base::Bind(&internal_cloud_print_helpers::Delete, path_to_file_));
[email protected]6ddda232011-04-22 15:41:47612 }
613
[email protected]e39027a2011-01-24 21:41:54614 // If we're modal we can show the dialog with no browser.
615 // End the keep-alive so that Chrome can exit.
[email protected]b5b79d72012-05-24 19:42:28616 if (!modal_parent_ && keep_alive_when_non_modal_)
[email protected]2e6389f2012-05-18 19:41:25617 browser::EndKeepAlive();
[email protected]73852b8f2010-05-14 00:38:12618 delete this;
619}
620
[email protected]5835871a2012-04-25 21:56:55621void CloudPrintWebDialogDelegate::OnCloseContents(WebContents* source,
622 bool* out_close_dialog) {
[email protected]18137e02010-05-25 21:10:35623 if (out_close_dialog)
624 *out_close_dialog = true;
625}
626
[email protected]5835871a2012-04-25 21:56:55627bool CloudPrintWebDialogDelegate::ShouldShowDialogTitle() const {
[email protected]ea161da2010-11-02 21:57:35628 return false;
629}
630
[email protected]5835871a2012-04-25 21:56:55631bool CloudPrintWebDialogDelegate::HandleContextMenu(
[email protected]35be7ec2012-02-12 20:42:51632 const content::ContextMenuParams& params) {
[email protected]34478212011-04-19 01:35:46633 return true;
634}
635
[email protected]b5b79d72012-05-24 19:42:28636void CreatePrintDialogForBytesImpl(content::BrowserContext* browser_context,
637 gfx::NativeWindow modal_parent,
638 scoped_refptr<base::RefCountedBytes> data,
[email protected]d955fc92011-09-19 20:49:03639 const string16& print_job_title,
640 const string16& print_ticket,
[email protected]b5b79d72012-05-24 19:42:28641 const std::string& file_type) {
[email protected]d955fc92011-09-19 20:49:03642 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
643 // TODO([email protected]) Writing the PDF to a file before printing
644 // is wasteful. Modify the dialog flow to pull PDF data from memory.
645 // See http://code.google.com/p/chromium/issues/detail?id=44093
646 FilePath path;
647 if (file_util::CreateTemporaryFile(&path)) {
648 file_util::WriteFile(path,
649 reinterpret_cast<const char*>(data->front()),
650 data->size());
651 }
[email protected]b5b79d72012-05-24 19:42:28652 print_dialog_cloud::CreatePrintDialogForFile(browser_context,
653 modal_parent,
654 path,
[email protected]d955fc92011-09-19 20:49:03655 print_job_title,
656 print_ticket,
657 file_type,
[email protected]d955fc92011-09-19 20:49:03658 true);
659}
660
[email protected]6085c70d2011-03-22 22:51:07661// Called from the UI thread, starts up the dialog.
[email protected]b5b79d72012-05-24 19:42:28662void CreateDialogImpl(content::BrowserContext* browser_context,
663 gfx::NativeWindow modal_parent,
664 const FilePath& path_to_file,
[email protected]6085c70d2011-03-22 22:51:07665 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56666 const string16& print_ticket,
[email protected]6085c70d2011-03-22 22:51:07667 const std::string& file_type,
[email protected]4cd49022012-01-19 20:37:37668 bool delete_on_close,
669 bool close_after_signin,
670 const base::Closure& callback) {
[email protected]ba4f1132010-10-09 02:02:35671 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]5835871a2012-04-25 21:56:55672 WebDialogDelegate* dialog_delegate =
673 new internal_cloud_print_helpers::CloudPrintWebDialogDelegate(
[email protected]b5b79d72012-05-24 19:42:28674 browser_context,
675 modal_parent,
676 path_to_file,
677 std::string(),
678 print_job_title,
679 print_ticket,
680 file_type,
681 delete_on_close,
682 close_after_signin,
[email protected]4cd49022012-01-19 20:37:37683 callback);
[email protected]b5b79d72012-05-24 19:42:28684 browser::ShowWebDialog(modal_parent,
685 Profile::FromBrowserContext(browser_context),
686 NULL,
687 dialog_delegate);
[email protected]73852b8f2010-05-14 00:38:12688}
[email protected]6085c70d2011-03-22 22:51:07689
[email protected]b5b79d72012-05-24 19:42:28690void CreateDialogSigninImpl(content::BrowserContext* browser_context,
691 gfx::NativeWindow modal_parent,
692 const base::Closure& callback) {
693 CreateDialogImpl(browser_context, modal_parent, FilePath(), string16(),
694 string16(), std::string(), false, true, callback);
[email protected]4cd49022012-01-19 20:37:37695}
696
[email protected]b5b79d72012-05-24 19:42:28697void CreateDialogFullImpl(content::BrowserContext* browser_context,
698 gfx::NativeWindow modal_parent,
699 const FilePath& path_to_file,
700 const string16& print_job_title,
701 const string16& print_ticket,
702 const std::string& file_type,
703 bool delete_on_close) {
704 CreateDialogImpl(browser_context, modal_parent, path_to_file, print_job_title,
705 print_ticket, file_type, delete_on_close, false,
706 base::Closure());
[email protected]4cd49022012-01-19 20:37:37707}
708
709
710
[email protected]6ddda232011-04-22 15:41:47711// Provides a runnable function to delete a file.
712void Delete(const FilePath& file_path) {
713 file_util::Delete(file_path, false);
714}
715
[email protected]6085c70d2011-03-22 22:51:07716} // namespace internal_cloud_print_helpers
717
718namespace print_dialog_cloud {
719
720// Called on the FILE or UI thread. This is the main entry point into creating
721// the dialog.
722
723// TODO(scottbyer): The signature here will need to change as the
724// workflow through the printing code changes to allow for dynamically
725// changing page setup parameters while the dialog is active.
[email protected]b5b79d72012-05-24 19:42:28726void CreatePrintDialogForFile(content::BrowserContext* browser_context,
727 gfx::NativeWindow modal_parent,
728 const FilePath& path_to_file,
[email protected]6085c70d2011-03-22 22:51:07729 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56730 const string16& print_ticket,
[email protected]6085c70d2011-03-22 22:51:07731 const std::string& file_type,
[email protected]d955fc92011-09-19 20:49:03732 bool delete_on_close) {
[email protected]6085c70d2011-03-22 22:51:07733 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) ||
734 BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]6085c70d2011-03-22 22:51:07735 BrowserThread::PostTask(
736 BrowserThread::UI, FROM_HERE,
[email protected]4cd49022012-01-19 20:37:37737 base::Bind(&internal_cloud_print_helpers::CreateDialogFullImpl,
[email protected]b5b79d72012-05-24 19:42:28738 browser_context, modal_parent, path_to_file, print_job_title,
739 print_ticket, file_type, delete_on_close));
[email protected]d955fc92011-09-19 20:49:03740}
741
[email protected]b5b79d72012-05-24 19:42:28742void CreateCloudPrintSigninDialog(content::BrowserContext* browser_context,
743 gfx::NativeWindow modal_parent,
744 const base::Closure& callback) {
[email protected]4cd49022012-01-19 20:37:37745 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
746
747 BrowserThread::PostTask(
748 BrowserThread::UI, FROM_HERE,
749 base::Bind(&internal_cloud_print_helpers::CreateDialogSigninImpl,
[email protected]b5b79d72012-05-24 19:42:28750 browser_context,
751 modal_parent,
[email protected]4cd49022012-01-19 20:37:37752 callback));
753}
754
[email protected]b5b79d72012-05-24 19:42:28755void CreatePrintDialogForBytes(content::BrowserContext* browser_context,
756 gfx::NativeWindow modal_parent,
757 scoped_refptr<base::RefCountedBytes> data,
[email protected]d955fc92011-09-19 20:49:03758 const string16& print_job_title,
759 const string16& print_ticket,
[email protected]b5b79d72012-05-24 19:42:28760 const std::string& file_type) {
[email protected]d955fc92011-09-19 20:49:03761 // TODO([email protected]) Avoid cloning the PDF data. Make use of a
762 // shared memory object instead.
763 // http://code.google.com/p/chromium/issues/detail?id=44093
[email protected]ebbccb952012-04-20 09:51:31764 scoped_refptr<base::RefCountedBytes> cloned_data =
765 new base::RefCountedBytes(data->data());
[email protected]d955fc92011-09-19 20:49:03766 BrowserThread::PostTask(
767 BrowserThread::FILE, FROM_HERE,
[email protected]3e2dd4fa2011-11-10 06:06:40768 base::Bind(&internal_cloud_print_helpers::CreatePrintDialogForBytesImpl,
[email protected]b5b79d72012-05-24 19:42:28769 browser_context, modal_parent, cloned_data, print_job_title,
770 print_ticket, file_type));
[email protected]6085c70d2011-03-22 22:51:07771}
772
[email protected]65c9d89a2011-04-13 21:02:39773bool CreatePrintDialogFromCommandLine(const CommandLine& command_line) {
[email protected]87ab41e72012-01-04 18:45:11774 DCHECK(command_line.HasSwitch(switches::kCloudPrintFile));
[email protected]65c9d89a2011-04-13 21:02:39775 if (!command_line.GetSwitchValuePath(switches::kCloudPrintFile).empty()) {
776 FilePath cloud_print_file;
777 cloud_print_file =
778 command_line.GetSwitchValuePath(switches::kCloudPrintFile);
779 if (!cloud_print_file.empty()) {
780 string16 print_job_title;
[email protected]e8368e92011-08-20 04:05:56781 string16 print_job_print_ticket;
[email protected]65c9d89a2011-04-13 21:02:39782 if (command_line.HasSwitch(switches::kCloudPrintJobTitle)) {
[email protected]e8368e92011-08-20 04:05:56783 print_job_title =
784 internal_cloud_print_helpers::GetSwitchValueString16(
785 command_line, switches::kCloudPrintJobTitle);
786 }
787 if (command_line.HasSwitch(switches::kCloudPrintPrintTicket)) {
788 print_job_print_ticket =
789 internal_cloud_print_helpers::GetSwitchValueString16(
790 command_line, switches::kCloudPrintPrintTicket);
[email protected]65c9d89a2011-04-13 21:02:39791 }
792 std::string file_type = "application/pdf";
793 if (command_line.HasSwitch(switches::kCloudPrintFileType)) {
794 file_type = command_line.GetSwitchValueASCII(
795 switches::kCloudPrintFileType);
796 }
[email protected]e8368e92011-08-20 04:05:56797
[email protected]d955fc92011-09-19 20:49:03798 bool delete_on_close = CommandLine::ForCurrentProcess()->HasSwitch(
799 switches::kCloudPrintDeleteFile);
800
[email protected]b5b79d72012-05-24 19:42:28801 print_dialog_cloud::CreatePrintDialogForFile(
802 ProfileManager::GetDefaultProfile(),
803 NULL,
804 cloud_print_file,
805 print_job_title,
806 print_job_print_ticket,
807 file_type,
808 delete_on_close);
[email protected]65c9d89a2011-04-13 21:02:39809 return true;
810 }
811 }
812 return false;
813}
814
[email protected]6085c70d2011-03-22 22:51:07815} // end namespace