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

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

Issue 2542363002: Interrogate PpdProvider from PrintPreview. (Closed)
Patch Set: assert that the proxy is destructed on the ui thread Created 4 years 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/bind_helpers.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "chrome/browser/chromeos/printing/ppd_provider_factory.h"
15 #include "chrome/browser/chromeos/printing/printer_pref_manager.h" 16 #include "chrome/browser/chromeos/printing/printer_pref_manager.h"
16 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" 17 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h"
17 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/webui/print_preview/printer_capabilities.h" 19 #include "chrome/browser/ui/webui/print_preview/printer_capabilities.h"
19 #include "chrome/common/chrome_switches.h" 20 #include "chrome/common/chrome_switches.h"
20 #include "chromeos/dbus/dbus_thread_manager.h" 21 #include "chromeos/dbus/dbus_thread_manager.h"
21 #include "chromeos/dbus/debug_daemon_client.h" 22 #include "chromeos/dbus/debug_daemon_client.h"
23 #include "chromeos/printing/ppd_provider.h"
22 #include "chromeos/printing/printer_configuration.h" 24 #include "chromeos/printing/printer_configuration.h"
23 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
24 #include "printing/backend/print_backend_consts.h" 26 #include "printing/backend/print_backend_consts.h"
25 27
26 namespace printing { 28 namespace printing {
27 29
28 namespace { 30 namespace {
29 31
30 enum SetupResult { SUCCESS, PPD_NOT_FOUND, FAILURE }; 32 enum SetupResult { SUCCESS, PPD_NOT_FOUND, FAILURE };
31 33
32 // Store the name used in CUPS, Printer#id in |printer_name|, the description 34 // Store the name used in CUPS, Printer#id in |printer_name|, the description
33 // as the system_driverinfo option value, and the Printer#display_name in 35 // as the system_driverinfo option value, and the Printer#display_name in
34 // the |printer_description| field. This will match how Mac OS X presents 36 // the |printer_description| field. This will match how Mac OS X presents
35 // printer information. 37 // printer information.
36 printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) { 38 printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) {
37 PrinterBasicInfo basic_info; 39 PrinterBasicInfo basic_info;
38 40
39 // TODO(skau): Unify Mac with the other platforms for display name 41 // TODO(skau): Unify Mac with the other platforms for display name
40 // presentation so I can remove this strange code. 42 // presentation so I can remove this strange code.
41 basic_info.options[kDriverInfoTagName] = printer.description(); 43 basic_info.options[kDriverInfoTagName] = printer.description();
42 basic_info.printer_name = printer.id(); 44 basic_info.printer_name = printer.id();
43 basic_info.printer_description = printer.display_name(); 45 basic_info.printer_description = printer.display_name();
44 return basic_info; 46 return basic_info;
45 } 47 }
46 48
49 void AddPrintersToList(
50 const std::vector<std::unique_ptr<chromeos::Printer>>& printers,
51 PrinterList* list) {
52 for (const std::unique_ptr<chromeos::Printer>& printer : printers) {
53 list->push_back(ToBasicInfo(*printer));
54 }
55 }
56
47 void FetchCapabilities(std::unique_ptr<chromeos::Printer> printer, 57 void FetchCapabilities(std::unique_ptr<chromeos::Printer> printer,
48 const PrinterSetupCallback& cb) { 58 const PrinterSetupCallback& cb) {
49 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
50 60
51 PrinterBasicInfo basic_info = ToBasicInfo(*printer); 61 PrinterBasicInfo basic_info = ToBasicInfo(*printer);
52 62
53 base::PostTaskAndReplyWithResult( 63 base::PostTaskAndReplyWithResult(
54 content::BrowserThread::GetBlockingPool(), FROM_HERE, 64 content::BrowserThread::GetBlockingPool(), FROM_HERE,
55 base::Bind(&GetSettingsOnBlockingPool, printer->id(), basic_info), cb); 65 base::Bind(&GetSettingsOnBlockingPool, printer->id(), basic_info), cb);
56 } 66 }
57 67
58 void PostCallbackError(const PrinterSetupCallback& cb) { 68 void PostCallbackError(const PrinterSetupCallback& cb) {
59 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, 69 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
60 base::Bind(cb, nullptr)); 70 base::Bind(cb, nullptr));
61 } 71 }
62 72
63 void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer, 73 void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer,
64 SetupResult result, 74 SetupResult result,
65 const PrinterSetupCallback& cb) { 75 const PrinterSetupCallback& cb) {
66 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
67 77
68 if (result != SetupResult::SUCCESS) { 78 if (result != SetupResult::SUCCESS) {
69 LOG(WARNING) << "Print setup failed"; 79 LOG(WARNING) << "Print setup failed: " << result;
70 // TODO(skau): Open printer settings if this is resolvable. 80 // TODO(skau): Open printer settings if this is resolvable.
71 PostCallbackError(cb); 81 PostCallbackError(cb);
72 return; 82 return;
73 } 83 }
74 84
75 VLOG(1) << "Printer setup successful for " << printer->id() 85 VLOG(1) << "Printer setup successful for " << printer->id()
76 << " fetching properties"; 86 << " fetching properties";
77 87
78 // fetch settings on the blocking pool and invoke callback. 88 // fetch settings on the blocking pool and invoke callback.
79 FetchCapabilities(std::move(printer), cb); 89 FetchCapabilities(std::move(printer), cb);
(...skipping 15 matching lines...) Expand all
95 LOG(WARNING) << "Could not contact debugd"; 105 LOG(WARNING) << "Could not contact debugd";
96 PostCallbackError(cb); 106 PostCallbackError(cb);
97 } 107 }
98 108
99 bool IsIppEverywhere(const chromeos::Printer& printer) { 109 bool IsIppEverywhere(const chromeos::Printer& printer) {
100 // TODO(skau): Use uri, effective_make and effective_model to determine if 110 // TODO(skau): Use uri, effective_make and effective_model to determine if
101 // we should do an IPP Everywhere configuration. 111 // we should do an IPP Everywhere configuration.
102 return false; 112 return false;
103 } 113 }
104 114
105 std::string GetPPDPath(const chromeos::Printer& printer) { 115 void AddPrinter(std::unique_ptr<chromeos::Printer> printer,
106 // TODO(skau): Consult the PPD Provider for the correct file path. 116 const std::string& ppd_path,
107 return printer.ppd_reference().user_supplied_ppd_url; 117 bool ipp_everywhere,
108 } 118 const PrinterSetupCallback& cb) {
119 // Always push configuration to CUPS. It may need an update.
120 const std::string printer_name = printer->id();
121 const std::string printer_uri = printer->uri();
109 122
110 } // namespace
111
112 std::string GetDefaultPrinterOnBlockingPoolThread() {
113 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
114 // TODO(crbug.com/660898): Add default printers to ChromeOS.
115 return "";
116 }
117
118 void EnumeratePrinters(Profile* profile, const EnumeratePrintersCallback& cb) {
119 // PrinterPrefManager is not thread safe and must be called from the UI
120 // thread.
121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
122
123 PrinterList printer_list;
124
125 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
126 switches::kEnableNativeCups)) {
127 chromeos::PrinterPrefManager* prefs =
128 chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile);
129 std::vector<std::unique_ptr<chromeos::Printer>> printers =
130 prefs->GetPrinters();
131 for (const std::unique_ptr<chromeos::Printer>& printer : printers) {
132 printer_list.push_back(ToBasicInfo(*printer));
133 }
134 }
135
136 cb.Run(printer_list);
137 }
138
139 void ConfigurePrinterAndFetchCapabilities(Profile* profile,
140 const std::string& printer_name,
141 const PrinterSetupCallback& cb) {
142 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
143
144 chromeos::PrinterPrefManager* prefs =
145 chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile);
146 std::unique_ptr<chromeos::Printer> printer = prefs->GetPrinter(printer_name);
147
148 // Check if configuration is viable.
149 bool ipp_everywhere = IsIppEverywhere(*printer);
150 std::string ppd_path = GetPPDPath(*printer);
151 if (!ipp_everywhere && ppd_path.empty()) {
152 HandlePrinterSetup(std::move(printer), PPD_NOT_FOUND, cb);
153 return;
154 }
155
156 // Always push configuration to CUPS. It may need an update.
157 std::string printer_uri = printer->uri();
158 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter( 123 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter(
159 printer_name, printer_uri, ppd_path, ipp_everywhere, 124 printer_name, printer_uri, ppd_path, ipp_everywhere,
160 base::Bind(&OnPrinterAddResult, base::Passed(&printer), cb), 125 base::Bind(&OnPrinterAddResult, base::Passed(&printer), cb),
161 base::Bind(&OnPrinterAddError, cb)); 126 base::Bind(&OnPrinterAddError, cb));
162 } 127 }
163 128
129 void PPDResolve(std::unique_ptr<chromeos::Printer> printer,
130 const PrinterSetupCallback& cb,
131 chromeos::printing::PpdProvider::CallbackResultCode result,
132 base::FilePath path) {
133 switch (result) {
134 case chromeos::printing::PpdProvider::SUCCESS: {
135 DCHECK(!path.empty());
136 AddPrinter(std::move(printer), path.value() /* ppd path */,
137 false /* non-ipp-everywhere */, cb);
138 break;
139 }
140 case chromeos::printing::PpdProvider::NOT_FOUND:
141 HandlePrinterSetup(std::move(printer), PPD_NOT_FOUND, cb);
142 break;
143 default:
144 HandlePrinterSetup(std::move(printer), FAILURE, cb);
145 break;
146 }
147 }
148
149 class PrinterBackendProxyChromeos : public PrinterBackendProxy {
150 public:
151 explicit PrinterBackendProxyChromeos(Profile* profile) {
152 prefs_ = chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile);
153 ppd_provider_ = chromeos::printing::CreateProvider(profile);
154 }
155
156 ~PrinterBackendProxyChromeos() override {
157 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
158 }
159
160 void GetDefaultPrinter(const DefaultPrinterCallback& cb) override {
161 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
162 // TODO(crbug.com/660898): Add default printers to ChromeOS.
163 cb.Run("");
Carlson 2016/12/06 23:31:57 While this conforms to my own sensibilities, I thi
skau 2016/12/07 18:19:47 Done.
164 };
165
166 void EnumeratePrinters(const EnumeratePrintersCallback& cb) override {
167 // PrinterPrefManager is not thread safe and must be called from the UI
168 // thread.
169 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
170
171 PrinterList printer_list;
172
173 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
174 switches::kEnableNativeCups)) {
175 AddPrintersToList(prefs_->GetPrinters(), &printer_list);
176 }
177
178 cb.Run(printer_list);
179 };
180
181 void ConfigurePrinterAndFetchCapabilities(
182 const std::string& printer_name,
183 const PrinterSetupCallback& cb) override {
184 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
185
186 std::unique_ptr<chromeos::Printer> printer =
187 prefs_->GetPrinter(printer_name);
188 if (!printer) {
189 // If the printer was removed, the lookup will fail.
190 cb.Run(nullptr);
191 return;
192 }
193
194 if (IsIppEverywhere(*printer)) {
195 // ChromeOS registers printer by GUID rather than display name.
196 AddPrinter(std::move(printer), "" /* empty ppd path */,
197 true /* ipp everywhere */, cb);
198 return;
199 }
200
201 // Ref taken because printer is moved.
202 const chromeos::Printer::PpdReference& ppd_ref = printer->ppd_reference();
203 ppd_provider_->Resolve(ppd_ref,
204 base::Bind(&PPDResolve, base::Passed(&printer), cb));
205 };
206
207 private:
208 chromeos::PrinterPrefManager* prefs_;
209 std::unique_ptr<chromeos::printing::PpdProvider> ppd_provider_;
210
211 DISALLOW_COPY_AND_ASSIGN(PrinterBackendProxyChromeos);
212 };
213
214 } // namespace
215
216 std::unique_ptr<PrinterBackendProxy> PrinterBackendProxy::Create(
217 Profile* profile) {
218 return base::MakeUnique<PrinterBackendProxyChromeos>(profile);
219 }
220
164 } // namespace printing 221 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698