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

Side by Side Diff: chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc

Issue 2468063003: Initial hookup of PpdProvider. (Closed)
Patch Set: Convert to ResolveJavascriptCallback, address xdai comments. 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/settings/chromeos/cups_printers_handler.h" 5 #include "chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.h"
6 6
7 #include <memory> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/path_service.h"
12 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
13 #include "base/values.h" 14 #include "base/values.h"
15 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" 16 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h"
15 #include "chrome/browser/download/download_prefs.h" 17 #include "chrome/browser/download/download_prefs.h"
16 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser_finder.h" 19 #include "chrome/browser/ui/browser_finder.h"
18 #include "chrome/browser/ui/browser_window.h" 20 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/chrome_select_file_policy.h" 21 #include "chrome/browser/ui/chrome_select_file_policy.h"
22 #include "chrome/common/chrome_paths.h"
20 #include "chromeos/dbus/dbus_thread_manager.h" 23 #include "chromeos/dbus/dbus_thread_manager.h"
21 #include "chromeos/dbus/debug_daemon_client.h" 24 #include "chromeos/dbus/debug_daemon_client.h"
22 #include "chromeos/printing/fake_printer_discoverer.h" 25 #include "chromeos/printing/fake_printer_discoverer.h"
26 #include "chromeos/printing/ppd_cache.h"
27 #include "chromeos/printing/ppd_provider.h"
23 #include "content/public/browser/browser_context.h" 28 #include "content/public/browser/browser_context.h"
24 #include "content/public/browser/browser_thread.h" 29 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/web_ui.h" 30 #include "content/public/browser/web_ui.h"
31 #include "google_apis/google_api_keys.h"
32 #include "net/url_request/url_request_context_getter.h"
26 #include "printing/backend/print_backend.h" 33 #include "printing/backend/print_backend.h"
27 #include "url/third_party/mozilla/url_parse.h" 34 #include "url/third_party/mozilla/url_parse.h"
28 35
29 namespace chromeos { 36 namespace chromeos {
30 namespace settings { 37 namespace settings {
31 38
32 namespace { 39 namespace {
33 40
34 void OnRemovedPrinter(bool success) {} 41 void OnRemovedPrinter(bool success) {}
35 42
(...skipping 27 matching lines...) Expand all
63 printer_info->SetString("printerQueue", path.substr(1)); 70 printer_info->SetString("printerQueue", path.substr(1));
64 71
65 return printer_info; 72 return printer_info;
66 } 73 }
67 74
68 } // namespace 75 } // namespace
69 76
70 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) 77 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui)
71 : printer_discoverer_(nullptr), 78 : printer_discoverer_(nullptr),
72 profile_(Profile::FromWebUI(webui)), 79 profile_(Profile::FromWebUI(webui)),
73 weak_factory_(this) {} 80 weak_factory_(this) {
81 base::FilePath ppd_cache_path;
82 CHECK(
83 base::PathService::Get(chrome::DIR_CHROMEOS_PPD_CACHE, &ppd_cache_path));
84 ppd_provider_ = chromeos::printing::PpdProvider::Create(
85 google_apis::GetAPIKey(), g_browser_process->system_request_context(),
86 chromeos::printing::PpdCache::Create(ppd_cache_path));
87 }
74 88
75 CupsPrintersHandler::~CupsPrintersHandler() {} 89 CupsPrintersHandler::~CupsPrintersHandler() {}
76 90
77 void CupsPrintersHandler::RegisterMessages() { 91 void CupsPrintersHandler::RegisterMessages() {
78 web_ui()->RegisterMessageCallback( 92 web_ui()->RegisterMessageCallback(
79 "getCupsPrintersList", 93 "getCupsPrintersList",
80 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, 94 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList,
81 base::Unretained(this))); 95 base::Unretained(this)));
82 web_ui()->RegisterMessageCallback( 96 web_ui()->RegisterMessageCallback(
83 "updateCupsPrinter", 97 "updateCupsPrinter",
84 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, 98 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter,
85 base::Unretained(this))); 99 base::Unretained(this)));
86 web_ui()->RegisterMessageCallback( 100 web_ui()->RegisterMessageCallback(
87 "removeCupsPrinter", 101 "removeCupsPrinter",
88 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, 102 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter,
89 base::Unretained(this))); 103 base::Unretained(this)));
90 web_ui()->RegisterMessageCallback( 104 web_ui()->RegisterMessageCallback(
91 "addCupsPrinter", base::Bind(&CupsPrintersHandler::HandleAddCupsPrinter, 105 "addCupsPrinter", base::Bind(&CupsPrintersHandler::HandleAddCupsPrinter,
92 base::Unretained(this))); 106 base::Unretained(this)));
93 web_ui()->RegisterMessageCallback( 107 web_ui()->RegisterMessageCallback(
108 "getCupsPrinterManufacturersList",
109 base::Bind(&CupsPrintersHandler::HandleGetCupsPrinterManufacturers,
110 base::Unretained(this)));
111 web_ui()->RegisterMessageCallback(
112 "getCupsPrinterModelsList",
113 base::Bind(&CupsPrintersHandler::HandleGetCupsPrinterModels,
114 base::Unretained(this)));
115 web_ui()->RegisterMessageCallback(
94 "selectPPDFile", base::Bind(&CupsPrintersHandler::HandleSelectPPDFile, 116 "selectPPDFile", base::Bind(&CupsPrintersHandler::HandleSelectPPDFile,
95 base::Unretained(this))); 117 base::Unretained(this)));
96 web_ui()->RegisterMessageCallback( 118 web_ui()->RegisterMessageCallback(
97 "startDiscoveringPrinters", 119 "startDiscoveringPrinters",
98 base::Bind(&CupsPrintersHandler::HandleStartDiscovery, 120 base::Bind(&CupsPrintersHandler::HandleStartDiscovery,
99 base::Unretained(this))); 121 base::Unretained(this)));
100 web_ui()->RegisterMessageCallback( 122 web_ui()->RegisterMessageCallback(
101 "stopDiscoveringPrinters", 123 "stopDiscoveringPrinters",
102 base::Bind(&CupsPrintersHandler::HandleStopDiscovery, 124 base::Bind(&CupsPrintersHandler::HandleStopDiscovery,
103 base::Unretained(this))); 125 base::Unretained(this)));
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 "cr.webUIListenerCallback", base::StringValue("on-add-cups-printer"), 235 "cr.webUIListenerCallback", base::StringValue("on-add-cups-printer"),
214 base::FundamentalValue(success), base::StringValue(printer_name)); 236 base::FundamentalValue(success), base::StringValue(printer_name));
215 } 237 }
216 238
217 void CupsPrintersHandler::OnAddPrinterError() { 239 void CupsPrintersHandler::OnAddPrinterError() {
218 CallJavascriptFunction("cr.webUIListenerCallback", 240 CallJavascriptFunction("cr.webUIListenerCallback",
219 base::StringValue("on-add-cups-printer"), 241 base::StringValue("on-add-cups-printer"),
220 base::FundamentalValue(false), base::StringValue("")); 242 base::FundamentalValue(false), base::StringValue(""));
221 } 243 }
222 244
245 void CupsPrintersHandler::HandleGetCupsPrinterManufacturers(
246 const base::ListValue* args) {
247 std::string js_callback;
248 CHECK_EQ(1U, args->GetSize());
249 CHECK(args->GetString(0, &js_callback));
250 ppd_provider_->QueryAvailable(
251 base::Bind(&CupsPrintersHandler::QueryAvailableManufacturersDone,
252 base::Unretained(this), js_callback));
skau 2016/11/02 23:20:13 Get the weakptr for CupsPrintersHandler in case we
Carlson 2016/11/02 23:28:55 Done.
253 }
254
255 void CupsPrintersHandler::HandleGetCupsPrinterModels(
256 const base::ListValue* args) {
257 std::string js_callback;
258 std::string manufacturer;
259 CHECK_EQ(2U, args->GetSize());
260 CHECK(args->GetString(0, &js_callback));
261 CHECK(args->GetString(1, &manufacturer));
262 ppd_provider_->QueryAvailable(
263 base::Bind(&CupsPrintersHandler::QueryAvailableModelsDone,
264 base::Unretained(this), js_callback, manufacturer));
skau 2016/11/02 23:20:13 ditto
Carlson 2016/11/02 23:28:55 Done.
265 }
266
223 void CupsPrintersHandler::HandleSelectPPDFile(const base::ListValue* args) { 267 void CupsPrintersHandler::HandleSelectPPDFile(const base::ListValue* args) {
224 CHECK_EQ(1U, args->GetSize()); 268 CHECK_EQ(1U, args->GetSize());
225 CHECK(args->GetString(0, &webui_callback_id_)); 269 CHECK(args->GetString(0, &webui_callback_id_));
226 270
227 base::FilePath downloads_path = DownloadPrefs::FromDownloadManager( 271 base::FilePath downloads_path = DownloadPrefs::FromDownloadManager(
228 content::BrowserContext::GetDownloadManager(profile_))->DownloadPath(); 272 content::BrowserContext::GetDownloadManager(profile_))->DownloadPath();
229 273
230 select_file_dialog_ = ui::SelectFileDialog::Create( 274 select_file_dialog_ = ui::SelectFileDialog::Create(
231 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents())); 275 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents()));
232 gfx::NativeWindow owning_window = 276 gfx::NativeWindow owning_window =
233 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents()) 277 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents())
234 ->window() 278 ->window()
235 ->GetNativeWindow(); 279 ->GetNativeWindow();
236 select_file_dialog_->SelectFile( 280 select_file_dialog_->SelectFile(
237 ui::SelectFileDialog::SELECT_OPEN_FILE, base::string16(), downloads_path, 281 ui::SelectFileDialog::SELECT_OPEN_FILE, base::string16(), downloads_path,
238 nullptr, 0, FILE_PATH_LITERAL(""), owning_window, nullptr); 282 nullptr, 0, FILE_PATH_LITERAL(""), owning_window, nullptr);
239 } 283 }
240 284
285 void CupsPrintersHandler::QueryAvailableManufacturersDone(
286 std::string js_callback,
287 chromeos::printing::PpdProvider::CallbackResultCode result_code,
288 const chromeos::printing::PpdProvider::AvailablePrintersMap& available) {
289 bool callback_result = false;
290 auto manufacturers = base::MakeUnique<base::ListValue>();
291 if (result_code == chromeos::printing::PpdProvider::SUCCESS) {
292 callback_result = true;
293 for (const auto& entry : available) {
294 manufacturers->AppendString(entry.first);
295 }
296 }
297 base::DictionaryValue response;
298 response.SetBoolean("success",
299 result_code == chromeos::printing::PpdProvider::SUCCESS);
300 response.Set("manufacturers", std::move(manufacturers));
301 ResolveJavascriptCallback(base::StringValue(js_callback), response);
302 }
303
304 void CupsPrintersHandler::QueryAvailableModelsDone(
305 std::string js_callback,
306 std::string manufacturer,
307 chromeos::printing::PpdProvider::CallbackResultCode result_code,
308 const chromeos::printing::PpdProvider::AvailablePrintersMap& available) {
309 auto models = base::MakeUnique<base::ListValue>();
310 if (result_code == chromeos::printing::PpdProvider::SUCCESS) {
311 const auto it = available.find(manufacturer);
312 if (it != available.end()) {
313 // Everything looks good.
314 models->AppendStrings(it->second);
315 }
316 }
317 base::DictionaryValue response;
318 response.SetBoolean("success",
319 result_code == chromeos::printing::PpdProvider::SUCCESS);
320 response.Set("models", std::move(models));
321 ResolveJavascriptCallback(base::StringValue(js_callback), response);
322 }
323
241 void CupsPrintersHandler::FileSelected(const base::FilePath& path, 324 void CupsPrintersHandler::FileSelected(const base::FilePath& path,
242 int index, 325 int index,
243 void* params) { 326 void* params) {
244 DCHECK(!webui_callback_id_.empty()); 327 DCHECK(!webui_callback_id_.empty());
245 ResolveJavascriptCallback(base::StringValue(webui_callback_id_), 328 ResolveJavascriptCallback(base::StringValue(webui_callback_id_),
246 base::StringValue(path.value())); 329 base::StringValue(path.value()));
247 webui_callback_id_.clear(); 330 webui_callback_id_.clear();
248 } 331 }
249 332
250 void CupsPrintersHandler::HandleStartDiscovery(const base::ListValue* args) { 333 void CupsPrintersHandler::HandleStartDiscovery(const base::ListValue* args) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 *printers_list); 365 *printers_list);
283 } 366 }
284 367
285 void CupsPrintersHandler::OnDiscoveryDone() { 368 void CupsPrintersHandler::OnDiscoveryDone() {
286 CallJavascriptFunction("cr.webUIListenerCallback", 369 CallJavascriptFunction("cr.webUIListenerCallback",
287 base::StringValue("on-printer-discovery-done")); 370 base::StringValue("on-printer-discovery-done"));
288 } 371 }
289 372
290 } // namespace settings 373 } // namespace settings
291 } // namespace chromeos 374 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.h ('k') | chrome/common/chrome_paths.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698