Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(751)

Side by Side Diff: chrome/browser/ui/webui/print_preview/printer_backend_proxy_chromeos.cc

Issue 2457933004: Register and select printer on click. (Closed)
Patch Set: lint Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/ui/webui/print_preview/printer_backend_proxy.h" 5 #include "chrome/browser/ui/webui/print_preview/printer_backend_proxy.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind_helpers.h"
10 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/logging.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/threading/sequenced_task_runner_handle.h"
11 #include "base/values.h" 15 #include "base/values.h"
12 #include "chrome/browser/chromeos/printing/printer_pref_manager.h" 16 #include "chrome/browser/chromeos/printing/printer_pref_manager.h"
13 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" 17 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h"
14 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/webui/print_preview/printer_capabilities.h"
15 #include "chrome/common/chrome_switches.h" 20 #include "chrome/common/chrome_switches.h"
21 #include "chromeos/dbus/dbus_thread_manager.h"
22 #include "chromeos/dbus/debug_daemon_client.h"
16 #include "chromeos/printing/printer_configuration.h" 23 #include "chromeos/printing/printer_configuration.h"
17 #include "content/public/browser/browser_thread.h" 24 #include "content/public/browser/browser_thread.h"
18 #include "printing/backend/print_backend.h" 25 #include "printing/backend/print_backend.h"
19 26
27 #if defined(USE_CUPS)
28 #include <cups/cups.h>
29
30 #include "printing/backend/cups_connection.h"
31 #endif
32
20 namespace printing { 33 namespace printing {
21 34
22 namespace { 35 namespace {
23 36
37 enum SetupResult { UNKNOWN, SUCCESS, PPD_NOT_FOUND, FAILURE };
38
24 printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) { 39 printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) {
25 PrinterBasicInfo basic_info; 40 PrinterBasicInfo basic_info;
26 basic_info.printer_display_name = printer.display_name(); 41 basic_info.printer_display_name = printer.display_name();
27 basic_info.printer_name = printer.id(); 42 basic_info.printer_name = printer.id();
28 basic_info.printer_description = printer.description(); 43 basic_info.printer_description = printer.description();
29 return basic_info; 44 return basic_info;
30 } 45 }
31 46
47 void SettingsToUIThread(std::unique_ptr<chromeos::Printer> printer,
48 const PrinterSetupCallback& cb) {
49 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
50
51 PrinterBasicInfo basic_info = ToBasicInfo(*printer);
52 std::unique_ptr<base::DictionaryValue> capabilities =
53 GetSettingsDictionary(printer->id(), basic_info);
54 content::BrowserThread::PostTask(
55 content::BrowserThread::UI, FROM_HERE,
56 base::Bind(cb, base::Passed(std::move(capabilities))));
Lei Zhang 2016/11/02 23:09:22 base/bind_helper.h says you can do base::Passed(&c
skau 2016/11/03 22:38:08 Done.
57 }
58
59 void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer,
60 printing::SetupResult result,
61 const PrinterSetupCallback& cb) {
62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
63
64 if (result != printing::SetupResult::SUCCESS) {
65 LOG(WARNING) << "Print setup failed";
66 // TODO(skau): Open printer settings if this is resolvable.
Lei Zhang 2016/11/02 23:09:22 I worry when the callback doesn't run. Is that ok
skau 2016/11/03 22:38:08 Not running the callback leaves print preview in t
67 return;
68 }
69
70 VLOG(1) << "Printer setup successful for " << printer->id()
71 << " fetching properties";
72
73 // fetch settings on the blocking pool and invoke callback.
74 content::BrowserThread::PostBlockingPoolTask(
75 FROM_HERE,
76 base::Bind(&SettingsToUIThread, base::Passed(std::move(printer)), cb));
77 }
78
79 void OnPrinterAddResult(std::unique_ptr<chromeos::Printer> printer,
80 const PrinterSetupCallback& cb,
81 bool success) {
82 // It's expected that debug daemon posts callbacks on the UI thread.
83 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
Lei Zhang 2016/11/02 23:09:22 Use DCHECK_CURRENTLY_ON().
skau 2016/11/03 22:38:08 Thanks! I hadn't seen that.
84
85 HandlePrinterSetup(std::move(printer), success ? SUCCESS : FAILURE, cb);
86 }
87
88 void OnPrinterAddError(const PrinterSetupCallback& cb) {
89 // It's expected that debug daemon posts callbacks on the UI thread.
90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
91
92 LOG(WARNING) << "Printer add failure";
93 base::MessageLoop::current()->task_runner()->PostTask(
94 FROM_HERE, base::Bind(cb, nullptr));
95 }
96
97 bool IsIppEverywhere(const chromeos::Printer& printer) {
98 // TODO(skau): Use uri, effective_make and effective_model to determine if
99 // we should do an IPP Everywhere configuration.
100 return false;
101 }
102
103 std::string GetPPDPath(const chromeos::Printer& printer) {
104 // TODO(skau): Consult the PPD Provider for the correct file path.
105 return printer.ppd_reference().user_supplied_ppd_url;
106 }
107
108 bool IsPrinterSetup(const std::string& printer_id) {
Lei Zhang 2016/11/02 23:09:22 Maybe rename this to IsPrinterConfigured? I flip b
skau 2016/11/03 22:38:08 Done.
109 // runs in blocking pool
110 scoped_refptr<printing::PrintBackend> print_backend(
111 printing::PrintBackend::CreateInstance(nullptr));
112 #if defined(USE_CUPS)
113 std::unique_ptr<printing::CupsConnection> connection =
114 base::MakeUnique<printing::CupsConnection>(GURL(), HTTP_ENCRYPT_NEVER,
115 true);
116 std::unique_ptr<printing::CupsPrinter> cups_printer =
117 connection->GetPrinter(printer_id);
118 return !!cups_printer;
119 #else
120 return false;
121 #endif
122 }
123
124 void OnPrinterSetup(std::unique_ptr<chromeos::Printer> printer,
125 const PrinterSetupCallback& cb) {
126 // This method is expected to run on the UI thread so it has access to the
127 // message loop.
128 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
129
130 // printer is not in CUPS. Attempt setup.
131 std::string ppd_path = GetPPDPath(*printer);
132 if (ppd_path.empty()) {
133 base::MessageLoop::current()->task_runner()->PostTask(
134 FROM_HERE,
135 base::Bind(&HandlePrinterSetup, base::Passed(std::move(printer)),
136 PPD_NOT_FOUND, cb));
137 return;
138 }
139
140 std::string printer_name = printer->id();
141 std::string printer_uri = printer->uri();
142 bool ipp_everywhere = IsIppEverywhere(*printer);
143 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter(
144 printer_name, printer_uri, ppd_path, ipp_everywhere,
145 base::Bind(&OnPrinterAddResult, base::Passed(std::move(printer)), cb),
146 base::Bind(&OnPrinterAddError, cb));
147 return;
148 }
149
32 } // namespace 150 } // namespace
33 151
34 std::string GetDefaultPrinterOnBlockingPoolThread() { 152 std::string GetDefaultPrinterOnBlockingPoolThread() {
35 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 153 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
36 // TODO(skau): Add default printers to ChromeOS. 154 // TODO(skau): Add default printers to ChromeOS.
37 return ""; 155 return "";
38 } 156 }
39 157
40 PrinterList EnumeratePrintersOnBlockingPoolThread(Profile* profile) { 158 PrinterList EnumeratePrintersOnBlockingPoolThread(Profile* profile) {
41 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 159 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
42 160
43 PrinterList printer_list; 161 PrinterList printer_list;
44 162
45 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 163 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
46 switches::kEnableNativeCups)) { 164 switches::kEnableNativeCups)) {
47 chromeos::PrinterPrefManager* prefs = 165 chromeos::PrinterPrefManager* prefs =
48 chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile); 166 chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile);
49 std::vector<std::unique_ptr<chromeos::Printer>> printers = 167 std::vector<std::unique_ptr<chromeos::Printer>> printers =
50 prefs->GetPrinters(); 168 prefs->GetPrinters();
51 for (const std::unique_ptr<chromeos::Printer>& printer : printers) { 169 for (const std::unique_ptr<chromeos::Printer>& printer : printers) {
52 printer_list.push_back(ToBasicInfo(*printer)); 170 printer_list.push_back(ToBasicInfo(*printer));
53 } 171 }
54 } 172 }
55 173
56 return printer_list; 174 return printer_list;
57 } 175 }
58 176
177 void ConfigurePrinterAndFetchCapabilities(Profile* profile,
178 const std::string& printer_name,
179 const PrinterSetupCallback& cb) {
180 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
181
182 chromeos::PrinterPrefManager* prefs_ =
183 chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile);
184
185 std::unique_ptr<chromeos::Printer> printer = prefs_->GetPrinter(printer_name);
186
187 if (IsPrinterSetup(printer_name)) {
188 // Printer found. Retrieve settings now.
189 SettingsToUIThread(std::move(printer), cb);
190 return;
191 }
192
193 content::BrowserThread::PostTask(
194 content::BrowserThread::UI, FROM_HERE,
195 base::Bind(&OnPrinterSetup, base::Passed(std::move(printer)), cb));
196 }
197
59 } // namespace printing 198 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698