OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/dbus/printer_service_provider.h" | 5 #include "chrome/browser/chromeos/dbus/printer_service_provider.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
11 #include "ash/session/session_state_delegate.h" | |
12 #include "ash/shell.h" | |
13 #include "ash/wm/window_util.h" | |
14 #include "base/bind.h" | 11 #include "base/bind.h" |
15 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
16 #include "base/command_line.h" | 13 #include "base/command_line.h" |
17 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
18 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
19 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
21 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
22 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
23 #include "base/sys_info.h" | |
24 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
25 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 21 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
26 #include "chrome/browser/notifications/notification.h" | 22 #include "chrome/browser/notifications/notification.h" |
27 #include "chrome/browser/notifications/notification_delegate.h" | 23 #include "chrome/browser/notifications/notification_delegate.h" |
28 #include "chrome/browser/notifications/notification_ui_manager.h" | 24 #include "chrome/browser/notifications/notification_ui_manager.h" |
29 #include "chrome/browser/profiles/profile_manager.h" | |
30 #include "chrome/browser/ui/browser.h" | |
31 #include "chrome/browser/ui/browser_tabstrip.h" | |
32 #include "chrome/browser/ui/browser_window.h" | |
33 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | |
34 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | |
35 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
36 #include "chrome/common/chrome_version_info.h" | |
37 #include "chrome/common/extensions/api/webstore_widget_private.h" | 25 #include "chrome/common/extensions/api/webstore_widget_private.h" |
38 #include "chrome/common/extensions/extension_constants.h" | 26 #include "chrome/common/extensions/extension_constants.h" |
39 #include "chrome/grit/generated_resources.h" | 27 #include "chrome/grit/generated_resources.h" |
40 #include "chromeos/chromeos_switches.h" | 28 #include "chromeos/chromeos_switches.h" |
41 #include "components/user_manager/user.h" | 29 #include "components/user_manager/user.h" |
42 #include "components/user_manager/user_manager.h" | 30 #include "components/user_manager/user_manager.h" |
43 #include "content/public/browser/browser_thread.h" | |
44 #include "content/public/browser/web_contents.h" | |
45 #include "dbus/bus.h" | 31 #include "dbus/bus.h" |
46 #include "dbus/exported_object.h" | 32 #include "dbus/exported_object.h" |
47 #include "dbus/message.h" | 33 #include "dbus/message.h" |
48 #include "device/usb/usb_ids.h" | 34 #include "device/usb/usb_ids.h" |
49 #include "extensions/browser/event_router.h" | 35 #include "extensions/browser/event_router.h" |
50 #include "extensions/browser/extension_registry.h" | 36 #include "extensions/browser/extension_registry.h" |
51 #include "extensions/common/extension.h" | 37 #include "extensions/common/extension.h" |
52 #include "extensions/common/extension_set.h" | 38 #include "extensions/common/extension_set.h" |
53 #include "extensions/common/permissions/api_permission.h" | 39 #include "extensions/common/permissions/api_permission.h" |
54 #include "extensions/common/permissions/permissions_data.h" | 40 #include "extensions/common/permissions/permissions_data.h" |
55 #include "extensions/common/permissions/usb_device_permission.h" | 41 #include "extensions/common/permissions/usb_device_permission.h" |
56 #include "grit/theme_resources.h" | 42 #include "grit/theme_resources.h" |
57 #include "net/base/escape.h" | |
58 #include "third_party/cros_system_api/dbus/service_constants.h" | 43 #include "third_party/cros_system_api/dbus/service_constants.h" |
59 #include "ui/aura/window.h" | |
60 #include "ui/base/l10n/l10n_util.h" | 44 #include "ui/base/l10n/l10n_util.h" |
61 #include "ui/base/resource/resource_bundle.h" | 45 #include "ui/base/resource/resource_bundle.h" |
62 | 46 |
63 namespace webstore_widget_private_api = | 47 namespace webstore_widget_private_api = |
64 extensions::api::webstore_widget_private; | 48 extensions::api::webstore_widget_private; |
65 | 49 |
66 namespace { | 50 namespace { |
67 | 51 |
68 const char kPrinterAdded[] = "PrinterAdded"; | 52 const char kPrinterAdded[] = "PrinterAdded"; |
69 | 53 |
70 const char kPrinterProviderFoundNotificationID[] = | 54 const char kPrinterProviderFoundNotificationID[] = |
71 "chrome://settings/printer/printer_app_found"; | 55 "chrome://settings/printer/printer_app_found"; |
72 | 56 |
73 const char kNoPrinterProviderNotificationID[] = | 57 const char kNoPrinterProviderNotificationID[] = |
74 "chrome://settings/printer/no_printer_app"; | 58 "chrome://settings/printer/no_printer_app"; |
75 | 59 |
76 enum PrinterServiceEvent { | 60 enum PrinterServiceEvent { |
77 PRINTER_ADDED, | 61 PRINTER_ADDED, |
78 PAGE_DISPLAYED, | 62 PAGE_DISPLAYED, |
79 PRINTER_SERVICE_EVENT_MAX, | 63 PRINTER_SERVICE_EVENT_MAX, |
80 }; | 64 }; |
81 | 65 |
82 // TODO(vitalybuka): update URL with more relevant information. | |
83 const char kCloudPrintLearnUrl[] = | |
84 "https://www.google.com/landing/cloudprint/index.html"; | |
85 | |
86 void ActivateContents(Browser* browser, content::WebContents* contents) { | |
87 browser->tab_strip_model()->ActivateTabAt( | |
88 browser->tab_strip_model()->GetIndexOfWebContents(contents), false); | |
89 } | |
90 | |
91 Browser* ActivateAndGetBrowserForUrl(GURL url) { | |
92 for (TabContentsIterator it; !it.done(); it.Next()) { | |
93 if (it->GetLastCommittedURL() == url) { | |
94 ActivateContents(it.browser(), *it); | |
95 return it.browser(); | |
96 } | |
97 } | |
98 return nullptr; | |
99 } | |
100 | |
101 bool HexStringToUInt16(const std::string& input, uint16* output) { | 66 bool HexStringToUInt16(const std::string& input, uint16* output) { |
102 uint32 output_uint = 0; | 67 uint32 output_uint = 0; |
103 if (!base::HexStringToUInt(input, &output_uint) || | 68 if (!base::HexStringToUInt(input, &output_uint) || |
104 output_uint > std::numeric_limits<uint16>::max()) | 69 output_uint > std::numeric_limits<uint16>::max()) |
105 return false; | 70 return false; |
106 *output = static_cast<uint16>(output_uint); | 71 *output = static_cast<uint16>(output_uint); |
107 return true; | 72 return true; |
108 } | 73 } |
109 | 74 |
110 void FindOrOpenCloudPrintPage(const std::string& /* vendor */, | |
111 const std::string& /* product */) { | |
112 UMA_HISTOGRAM_ENUMERATION("PrinterService.PrinterServiceEvent", PRINTER_ADDED, | |
113 PRINTER_SERVICE_EVENT_MAX); | |
114 if (!ash::Shell::GetInstance()->session_state_delegate()-> | |
115 IsActiveUserSessionStarted() || | |
116 ash::Shell::GetInstance()->session_state_delegate()->IsScreenLocked()) { | |
117 return; | |
118 } | |
119 | |
120 Profile* profile = ProfileManager::GetLastUsedProfile(); | |
121 if (!profile) | |
122 return; | |
123 | |
124 GURL url(kCloudPrintLearnUrl); | |
125 | |
126 if (!ActivateAndGetBrowserForUrl(url)) { | |
127 chrome::ScopedTabbedBrowserDisplayer displayer( | |
128 profile, chrome::HOST_DESKTOP_TYPE_ASH); | |
129 UMA_HISTOGRAM_ENUMERATION("PrinterService.PrinterServiceEvent", | |
Vitaly Buka (NO REVIEWS)
2015/05/15 22:00:18
would you like to move PAGE_DISPLAYED, even into S
tbarzic
2015/05/15 22:36:55
deprecated PAGE_DISPLAYED and added new enum value
| |
130 PAGE_DISPLAYED, PRINTER_SERVICE_EVENT_MAX); | |
131 chrome::AddSelectedTabWithURL(displayer.browser(), url, | |
132 ui::PAGE_TRANSITION_LINK); | |
133 } | |
134 } | |
135 | |
136 base::string16 GetNotificationTitle(uint16 vendor_id, uint16 product_id) { | 75 base::string16 GetNotificationTitle(uint16 vendor_id, uint16 product_id) { |
137 const char* vendor_name = device::UsbIds::GetVendorName(vendor_id); | 76 const char* vendor_name = device::UsbIds::GetVendorName(vendor_id); |
138 if (vendor_name) { | 77 if (vendor_name) { |
139 return l10n_util::GetStringFUTF16(IDS_PRINTER_DETECTED_NOTIFICATION_TITLE, | 78 return l10n_util::GetStringFUTF16(IDS_PRINTER_DETECTED_NOTIFICATION_TITLE, |
140 base::UTF8ToUTF16(vendor_name)); | 79 base::UTF8ToUTF16(vendor_name)); |
141 } else { | 80 } else { |
142 return l10n_util::GetStringUTF16( | 81 return l10n_util::GetStringUTF16( |
143 IDS_PRINTER_DETECTED_NOTIFICATION_TITLE_UNKNOWN_VENDOR); | 82 IDS_PRINTER_DETECTED_NOTIFICATION_TITLE_UNKNOWN_VENDOR); |
144 } | 83 } |
145 } | 84 } |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 const std::string& interface_name, | 290 const std::string& interface_name, |
352 const std::string& method_name, | 291 const std::string& method_name, |
353 bool success) { | 292 bool success) { |
354 if (!success) { | 293 if (!success) { |
355 LOG(ERROR) << "Failed to export " << interface_name << "." | 294 LOG(ERROR) << "Failed to export " << interface_name << "." |
356 << method_name; | 295 << method_name; |
357 } | 296 } |
358 DVLOG(1) << "Method exported: " << interface_name << "." << method_name; | 297 DVLOG(1) << "Method exported: " << interface_name << "." << method_name; |
359 } | 298 } |
360 | 299 |
361 void PrinterServiceProvider::ShowCloudPrintHelp(const std::string& vendor, | |
362 const std::string& product) { | |
363 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
364 base::Bind(&FindOrOpenCloudPrintPage, vendor, | |
365 product)); | |
366 } | |
367 | |
368 void PrinterServiceProvider::PrinterAdded( | 300 void PrinterServiceProvider::PrinterAdded( |
369 dbus::MethodCall* method_call, | 301 dbus::MethodCall* method_call, |
370 dbus::ExportedObject::ResponseSender response_sender) { | 302 dbus::ExportedObject::ResponseSender response_sender) { |
371 DVLOG(1) << "PrinterAdded " << method_call->ToString(); | 303 DVLOG(1) << "PrinterAdded " << method_call->ToString(); |
372 dbus::MessageReader reader(method_call); | 304 dbus::MessageReader reader(method_call); |
373 | 305 |
374 std::string vendor_id; | 306 std::string vendor_id; |
375 reader.PopString(&vendor_id); | 307 reader.PopString(&vendor_id); |
376 StringToUpperASCII(&vendor_id); | 308 StringToUpperASCII(&vendor_id); |
377 | 309 |
378 std::string product_id; | 310 std::string product_id; |
379 reader.PopString(&product_id); | 311 reader.PopString(&product_id); |
380 StringToUpperASCII(&product_id); | 312 StringToUpperASCII(&product_id); |
381 | 313 |
382 // Send an empty response. | 314 // Send an empty response. |
383 response_sender.Run(dbus::Response::FromMethodCall(method_call)); | 315 response_sender.Run(dbus::Response::FromMethodCall(method_call)); |
384 | 316 |
385 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 317 UMA_HISTOGRAM_ENUMERATION("PrinterService.PrinterServiceEvent", PRINTER_ADDED, |
318 PRINTER_SERVICE_EVENT_MAX); | |
319 | |
320 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
386 switches::kEnablePrinterAppSearch)) { | 321 switches::kEnablePrinterAppSearch)) { |
387 ShowPrinterPluggedNotification( | |
388 notification_ui_manager_ ? notification_ui_manager_ | |
389 : g_browser_process->notification_ui_manager(), | |
390 vendor_id, product_id); | |
391 return; | 322 return; |
392 } | 323 } |
393 | 324 |
394 // Disable showing Cloudprint help on canary and dev channel, as these have | 325 ShowPrinterPluggedNotification( |
395 // support for printerProvider API. | 326 notification_ui_manager_ ? notification_ui_manager_ |
396 // TODO(tbarzic): Remove this and offer the user to search for an extension | 327 : g_browser_process->notification_ui_manager(), |
397 // that can act as a print driver (using printerProvider API) for USB printers | 328 vendor_id, product_id); |
398 // detected by this service. http://crbug.com/439448 | |
399 if (base::SysInfo::IsRunningOnChromeOS() && | |
400 chrome::VersionInfo::GetChannel() <= chrome::VersionInfo::CHANNEL_DEV) | |
401 return; | |
402 | |
403 ShowCloudPrintHelp(vendor_id, product_id); | |
404 } | 329 } |
405 | 330 |
406 } // namespace chromeos | 331 } // namespace chromeos |
407 | 332 |
OLD | NEW |