| OLD | NEW |
| 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 <utility> | 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" |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 base::ListValue* printers_list = new base::ListValue; | 141 base::ListValue* printers_list = new base::ListValue; |
| 142 for (const std::unique_ptr<Printer>& printer : printers) { | 142 for (const std::unique_ptr<Printer>& printer : printers) { |
| 143 std::unique_ptr<base::DictionaryValue> printer_info = | 143 std::unique_ptr<base::DictionaryValue> printer_info = |
| 144 GetPrinterInfo(*printer.get()); | 144 GetPrinterInfo(*printer.get()); |
| 145 printers_list->Append(std::move(printer_info)); | 145 printers_list->Append(std::move(printer_info)); |
| 146 } | 146 } |
| 147 | 147 |
| 148 std::unique_ptr<base::DictionaryValue> response = | 148 std::unique_ptr<base::DictionaryValue> response = |
| 149 base::MakeUnique<base::DictionaryValue>(); | 149 base::MakeUnique<base::DictionaryValue>(); |
| 150 response->Set("printerList", printers_list); | 150 response->Set("printerList", printers_list); |
| 151 ResolveJavascriptCallback(base::StringValue(callback_id), *response); | 151 ResolveJavascriptCallback(base::Value(callback_id), *response); |
| 152 } | 152 } |
| 153 | 153 |
| 154 void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) { | 154 void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) { |
| 155 std::string printer_id; | 155 std::string printer_id; |
| 156 std::string printer_name; | 156 std::string printer_name; |
| 157 CHECK(args->GetString(0, &printer_id)); | 157 CHECK(args->GetString(0, &printer_id)); |
| 158 CHECK(args->GetString(1, &printer_name)); | 158 CHECK(args->GetString(1, &printer_name)); |
| 159 | 159 |
| 160 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); | 160 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); |
| 161 printer->set_display_name(printer_name); | 161 printer->set_display_name(printer_name); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 | 244 |
| 245 void CupsPrintersHandler::OnAddedPrinter(std::unique_ptr<Printer> printer, | 245 void CupsPrintersHandler::OnAddedPrinter(std::unique_ptr<Printer> printer, |
| 246 chromeos::SetupResult result_code) { | 246 chromeos::SetupResult result_code) { |
| 247 std::string printer_name = printer->display_name(); | 247 std::string printer_name = printer->display_name(); |
| 248 bool success = (result_code == chromeos::SetupResult::SUCCESS); | 248 bool success = (result_code == chromeos::SetupResult::SUCCESS); |
| 249 if (success) { | 249 if (success) { |
| 250 PrintersManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( | 250 PrintersManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( |
| 251 std::move(printer)); | 251 std::move(printer)); |
| 252 } | 252 } |
| 253 CallJavascriptFunction("cr.webUIListenerCallback", | 253 CallJavascriptFunction("cr.webUIListenerCallback", |
| 254 base::StringValue("on-add-cups-printer"), | 254 base::Value("on-add-cups-printer"), |
| 255 base::Value(success), base::StringValue(printer_name)); | 255 base::Value(success), base::Value(printer_name)); |
| 256 } | 256 } |
| 257 | 257 |
| 258 void CupsPrintersHandler::OnAddPrinterError() { | 258 void CupsPrintersHandler::OnAddPrinterError() { |
| 259 CallJavascriptFunction("cr.webUIListenerCallback", | 259 CallJavascriptFunction("cr.webUIListenerCallback", |
| 260 base::StringValue("on-add-cups-printer"), | 260 base::Value("on-add-cups-printer"), base::Value(false), |
| 261 base::Value(false), base::StringValue("")); | 261 base::Value("")); |
| 262 } | 262 } |
| 263 | 263 |
| 264 void CupsPrintersHandler::HandleGetCupsPrinterManufacturers( | 264 void CupsPrintersHandler::HandleGetCupsPrinterManufacturers( |
| 265 const base::ListValue* args) { | 265 const base::ListValue* args) { |
| 266 AllowJavascript(); | 266 AllowJavascript(); |
| 267 std::string js_callback; | 267 std::string js_callback; |
| 268 CHECK_EQ(1U, args->GetSize()); | 268 CHECK_EQ(1U, args->GetSize()); |
| 269 CHECK(args->GetString(0, &js_callback)); | 269 CHECK(args->GetString(0, &js_callback)); |
| 270 ppd_provider_->ResolveManufacturers( | 270 ppd_provider_->ResolveManufacturers( |
| 271 base::Bind(&CupsPrintersHandler::ResolveManufacturersDone, | 271 base::Bind(&CupsPrintersHandler::ResolveManufacturersDone, |
| 272 weak_factory_.GetWeakPtr(), js_callback)); | 272 weak_factory_.GetWeakPtr(), js_callback)); |
| 273 } | 273 } |
| 274 | 274 |
| 275 void CupsPrintersHandler::HandleGetCupsPrinterModels( | 275 void CupsPrintersHandler::HandleGetCupsPrinterModels( |
| 276 const base::ListValue* args) { | 276 const base::ListValue* args) { |
| 277 AllowJavascript(); | 277 AllowJavascript(); |
| 278 std::string js_callback; | 278 std::string js_callback; |
| 279 std::string manufacturer; | 279 std::string manufacturer; |
| 280 CHECK_EQ(2U, args->GetSize()); | 280 CHECK_EQ(2U, args->GetSize()); |
| 281 CHECK(args->GetString(0, &js_callback)); | 281 CHECK(args->GetString(0, &js_callback)); |
| 282 CHECK(args->GetString(1, &manufacturer)); | 282 CHECK(args->GetString(1, &manufacturer)); |
| 283 | 283 |
| 284 // Empty manufacturer queries may be triggered as a part of the ui | 284 // Empty manufacturer queries may be triggered as a part of the ui |
| 285 // initialization, and should just return empty results. | 285 // initialization, and should just return empty results. |
| 286 if (manufacturer.empty()) { | 286 if (manufacturer.empty()) { |
| 287 base::DictionaryValue response; | 287 base::DictionaryValue response; |
| 288 response.SetBoolean("success", true); | 288 response.SetBoolean("success", true); |
| 289 response.Set("models", base::MakeUnique<base::ListValue>()); | 289 response.Set("models", base::MakeUnique<base::ListValue>()); |
| 290 ResolveJavascriptCallback(base::StringValue(js_callback), response); | 290 ResolveJavascriptCallback(base::Value(js_callback), response); |
| 291 return; | 291 return; |
| 292 } | 292 } |
| 293 | 293 |
| 294 ppd_provider_->ResolvePrinters( | 294 ppd_provider_->ResolvePrinters( |
| 295 manufacturer, base::Bind(&CupsPrintersHandler::ResolvePrintersDone, | 295 manufacturer, base::Bind(&CupsPrintersHandler::ResolvePrintersDone, |
| 296 weak_factory_.GetWeakPtr(), js_callback)); | 296 weak_factory_.GetWeakPtr(), js_callback)); |
| 297 } | 297 } |
| 298 | 298 |
| 299 void CupsPrintersHandler::HandleSelectPPDFile(const base::ListValue* args) { | 299 void CupsPrintersHandler::HandleSelectPPDFile(const base::ListValue* args) { |
| 300 CHECK_EQ(1U, args->GetSize()); | 300 CHECK_EQ(1U, args->GetSize()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 321 chromeos::printing::PpdProvider::CallbackResultCode result_code, | 321 chromeos::printing::PpdProvider::CallbackResultCode result_code, |
| 322 const std::vector<std::string>& manufacturers) { | 322 const std::vector<std::string>& manufacturers) { |
| 323 auto manufacturers_value = base::MakeUnique<base::ListValue>(); | 323 auto manufacturers_value = base::MakeUnique<base::ListValue>(); |
| 324 if (result_code == chromeos::printing::PpdProvider::SUCCESS) { | 324 if (result_code == chromeos::printing::PpdProvider::SUCCESS) { |
| 325 manufacturers_value->AppendStrings(manufacturers); | 325 manufacturers_value->AppendStrings(manufacturers); |
| 326 } | 326 } |
| 327 base::DictionaryValue response; | 327 base::DictionaryValue response; |
| 328 response.SetBoolean("success", | 328 response.SetBoolean("success", |
| 329 result_code == chromeos::printing::PpdProvider::SUCCESS); | 329 result_code == chromeos::printing::PpdProvider::SUCCESS); |
| 330 response.Set("manufacturers", std::move(manufacturers_value)); | 330 response.Set("manufacturers", std::move(manufacturers_value)); |
| 331 ResolveJavascriptCallback(base::StringValue(js_callback), response); | 331 ResolveJavascriptCallback(base::Value(js_callback), response); |
| 332 } | 332 } |
| 333 | 333 |
| 334 void CupsPrintersHandler::ResolvePrintersDone( | 334 void CupsPrintersHandler::ResolvePrintersDone( |
| 335 const std::string& js_callback, | 335 const std::string& js_callback, |
| 336 chromeos::printing::PpdProvider::CallbackResultCode result_code, | 336 chromeos::printing::PpdProvider::CallbackResultCode result_code, |
| 337 const std::vector<std::string>& printers) { | 337 const std::vector<std::string>& printers) { |
| 338 auto printers_value = base::MakeUnique<base::ListValue>(); | 338 auto printers_value = base::MakeUnique<base::ListValue>(); |
| 339 if (result_code == chromeos::printing::PpdProvider::SUCCESS) { | 339 if (result_code == chromeos::printing::PpdProvider::SUCCESS) { |
| 340 printers_value->AppendStrings(printers); | 340 printers_value->AppendStrings(printers); |
| 341 } | 341 } |
| 342 base::DictionaryValue response; | 342 base::DictionaryValue response; |
| 343 response.SetBoolean("success", | 343 response.SetBoolean("success", |
| 344 result_code == chromeos::printing::PpdProvider::SUCCESS); | 344 result_code == chromeos::printing::PpdProvider::SUCCESS); |
| 345 response.Set("models", std::move(printers_value)); | 345 response.Set("models", std::move(printers_value)); |
| 346 ResolveJavascriptCallback(base::StringValue(js_callback), response); | 346 ResolveJavascriptCallback(base::Value(js_callback), response); |
| 347 } | 347 } |
| 348 | 348 |
| 349 void CupsPrintersHandler::FileSelected(const base::FilePath& path, | 349 void CupsPrintersHandler::FileSelected(const base::FilePath& path, |
| 350 int index, | 350 int index, |
| 351 void* params) { | 351 void* params) { |
| 352 DCHECK(!webui_callback_id_.empty()); | 352 DCHECK(!webui_callback_id_.empty()); |
| 353 ResolveJavascriptCallback(base::StringValue(webui_callback_id_), | 353 ResolveJavascriptCallback(base::Value(webui_callback_id_), |
| 354 base::StringValue(path.value())); | 354 base::Value(path.value())); |
| 355 webui_callback_id_.clear(); | 355 webui_callback_id_.clear(); |
| 356 } | 356 } |
| 357 | 357 |
| 358 void CupsPrintersHandler::HandleStartDiscovery(const base::ListValue* args) { | 358 void CupsPrintersHandler::HandleStartDiscovery(const base::ListValue* args) { |
| 359 if (!printer_discoverer_.get()) | 359 if (!printer_discoverer_.get()) |
| 360 printer_discoverer_ = chromeos::PrinterDiscoverer::Create(); | 360 printer_discoverer_ = chromeos::PrinterDiscoverer::Create(); |
| 361 | 361 |
| 362 printer_discoverer_->AddObserver(this); | 362 printer_discoverer_->AddObserver(this); |
| 363 if (!printer_discoverer_->StartDiscovery()) { | 363 if (!printer_discoverer_->StartDiscovery()) { |
| 364 CallJavascriptFunction("cr.webUIListenerCallback", | 364 CallJavascriptFunction("cr.webUIListenerCallback", |
| 365 base::StringValue("on-printer-discovery-failed")); | 365 base::Value("on-printer-discovery-failed")); |
| 366 printer_discoverer_->RemoveObserver(this); | 366 printer_discoverer_->RemoveObserver(this); |
| 367 } | 367 } |
| 368 } | 368 } |
| 369 | 369 |
| 370 void CupsPrintersHandler::HandleStopDiscovery(const base::ListValue* args) { | 370 void CupsPrintersHandler::HandleStopDiscovery(const base::ListValue* args) { |
| 371 if (printer_discoverer_.get()) { | 371 if (printer_discoverer_.get()) { |
| 372 printer_discoverer_->RemoveObserver(this); | 372 printer_discoverer_->RemoveObserver(this); |
| 373 printer_discoverer_->StopDiscovery(); | 373 printer_discoverer_->StopDiscovery(); |
| 374 printer_discoverer_.reset(); | 374 printer_discoverer_.reset(); |
| 375 } | 375 } |
| 376 } | 376 } |
| 377 | 377 |
| 378 void CupsPrintersHandler::OnPrintersFound( | 378 void CupsPrintersHandler::OnPrintersFound( |
| 379 const std::vector<Printer>& printers) { | 379 const std::vector<Printer>& printers) { |
| 380 std::unique_ptr<base::ListValue> printers_list = | 380 std::unique_ptr<base::ListValue> printers_list = |
| 381 base::MakeUnique<base::ListValue>(); | 381 base::MakeUnique<base::ListValue>(); |
| 382 for (const auto& printer : printers) { | 382 for (const auto& printer : printers) { |
| 383 std::unique_ptr<base::DictionaryValue> printer_info = | 383 std::unique_ptr<base::DictionaryValue> printer_info = |
| 384 GetPrinterInfo(printer); | 384 GetPrinterInfo(printer); |
| 385 printers_list->Append(std::move(printer_info)); | 385 printers_list->Append(std::move(printer_info)); |
| 386 } | 386 } |
| 387 | 387 |
| 388 CallJavascriptFunction("cr.webUIListenerCallback", | 388 CallJavascriptFunction("cr.webUIListenerCallback", |
| 389 base::StringValue("on-printer-discovered"), | 389 base::Value("on-printer-discovered"), *printers_list); |
| 390 *printers_list); | |
| 391 } | 390 } |
| 392 | 391 |
| 393 void CupsPrintersHandler::OnDiscoveryDone() { | 392 void CupsPrintersHandler::OnDiscoveryDone() { |
| 394 CallJavascriptFunction("cr.webUIListenerCallback", | 393 CallJavascriptFunction("cr.webUIListenerCallback", |
| 395 base::StringValue("on-printer-discovery-done")); | 394 base::Value("on-printer-discovery-done")); |
| 396 } | 395 } |
| 397 | 396 |
| 398 } // namespace settings | 397 } // namespace settings |
| 399 } // namespace chromeos | 398 } // namespace chromeos |
| OLD | NEW |