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 <algorithm> |
7 #include <memory> | 8 #include <memory> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" |
12 #include "base/values.h" | 14 #include "base/values.h" |
13 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" | 15 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" |
14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" |
| 18 #include "chromeos/dbus/debug_daemon_client.h" |
15 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/web_ui.h" | 20 #include "content/public/browser/web_ui.h" |
17 #include "printing/backend/print_backend.h" | 21 #include "printing/backend/print_backend.h" |
18 #include "url/third_party/mozilla/url_parse.h" | 22 #include "url/third_party/mozilla/url_parse.h" |
19 | 23 |
20 namespace chromeos { | 24 namespace chromeos { |
| 25 |
| 26 namespace { |
| 27 |
| 28 struct IdMatches { |
| 29 std::string id; |
| 30 |
| 31 bool operator()(const std::unique_ptr<Printer>& printer) { |
| 32 return printer->id() == id; |
| 33 } |
| 34 }; |
| 35 |
| 36 void OnPrinterAdded(bool success) { |
| 37 VLOG(1) << "Printer added " << success; |
| 38 } |
| 39 |
| 40 void OnPrinterRemoved(bool success) { |
| 41 VLOG(1) << "Printer removed " << success; |
| 42 } |
| 43 |
| 44 void OperationError(const std::string& label) { |
| 45 VLOG(1) << "Operation " << label << " encountered an error"; |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
21 namespace settings { | 50 namespace settings { |
22 | 51 |
23 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) | 52 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) |
24 : profile_(Profile::FromWebUI(webui)), weak_factory_(this) {} | 53 : profile_(Profile::FromWebUI(webui)), weak_factory_(this) {} |
25 | 54 |
26 CupsPrintersHandler::~CupsPrintersHandler() {} | 55 CupsPrintersHandler::~CupsPrintersHandler() {} |
27 | 56 |
28 void CupsPrintersHandler::RegisterMessages() { | 57 void CupsPrintersHandler::RegisterMessages() { |
29 web_ui()->RegisterMessageCallback( | 58 web_ui()->RegisterMessageCallback( |
30 "getCupsPrintersList", | 59 "getCupsPrintersList", |
31 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, | 60 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, |
32 base::Unretained(this))); | 61 base::Unretained(this))); |
33 web_ui()->RegisterMessageCallback( | 62 web_ui()->RegisterMessageCallback( |
34 "updateCupsPrinter", | 63 "updateCupsPrinter", |
35 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, | 64 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, |
36 base::Unretained(this))); | 65 base::Unretained(this))); |
37 web_ui()->RegisterMessageCallback( | 66 web_ui()->RegisterMessageCallback( |
38 "removeCupsPrinter", | 67 "removeCupsPrinter", |
39 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, | 68 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, |
40 base::Unretained(this))); | 69 base::Unretained(this))); |
| 70 web_ui()->RegisterMessageCallback( |
| 71 "addCupsPrinter", base::Bind(&CupsPrintersHandler::HandleAddCupsPrinter, |
| 72 base::Unretained(this))); |
41 } | 73 } |
42 | 74 |
43 void CupsPrintersHandler::HandleGetCupsPrintersList( | 75 void CupsPrintersHandler::HandleGetCupsPrintersList( |
44 const base::ListValue* args) { | 76 const base::ListValue* args) { |
45 AllowJavascript(); | 77 AllowJavascript(); |
46 | 78 |
47 CHECK_EQ(1U, args->GetSize()); | 79 CHECK_EQ(1U, args->GetSize()); |
48 std::string callback_id; | 80 std::string callback_id; |
49 CHECK(args->GetString(0, &callback_id)); | 81 CHECK(args->GetString(0, &callback_id)); |
50 | 82 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 118 |
87 std::unique_ptr<base::DictionaryValue> response = | 119 std::unique_ptr<base::DictionaryValue> response = |
88 base::MakeUnique<base::DictionaryValue>(); | 120 base::MakeUnique<base::DictionaryValue>(); |
89 response->Set("printerList", printers_list); | 121 response->Set("printerList", printers_list); |
90 ResolveJavascriptCallback(base::StringValue(callback_id), *response); | 122 ResolveJavascriptCallback(base::StringValue(callback_id), *response); |
91 } | 123 } |
92 | 124 |
93 void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) { | 125 void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) { |
94 std::string printer_id; | 126 std::string printer_id; |
95 std::string printer_name; | 127 std::string printer_name; |
| 128 std::string printer_address; |
| 129 CHECK(args->GetString(0, &printer_id)); |
| 130 CHECK(args->GetString(1, &printer_name)); |
| 131 CHECK(args->GetString(2, &printer_address)); |
| 132 |
| 133 chromeos::PrinterPrefManager* prefs = |
| 134 PrinterPrefManagerFactory::GetForBrowserContext(profile_); |
| 135 auto printers = prefs->GetPrinters(); |
| 136 IdMatches matcher; |
| 137 matcher.id = printer_id; |
| 138 auto found = std::find_if(printers.begin(), printers.end(), matcher); |
| 139 DCHECK(found != printers.end()) << "Printer not found"; |
| 140 |
| 141 std::unique_ptr<Printer>& printer = *found; |
| 142 printer->set_display_name(printer_name); |
| 143 prefs->RegisterPrinter(std::move(*found)); |
| 144 |
| 145 chromeos::DebugDaemonClient::AddPrinterCallback callback = |
| 146 base::Bind(&OnPrinterAdded); |
| 147 base::Closure error_callback = base::Bind(&OperationError, "PRINTER_ADDED"); |
| 148 |
| 149 chromeos::DebugDaemonClient* client = |
| 150 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 151 // TODO(skau): switch to real ppd values when file is availalble. |
| 152 client->CupsAddPrinter(printer_name, // name |
| 153 base::StringPrintf("ipp://%s/", printer_address.c_str()
), // uri |
| 154 "", // ppd name |
| 155 true, // ipp everywhere |
| 156 callback, error_callback); |
| 157 } |
| 158 |
| 159 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { |
| 160 std::string printer_id; |
| 161 std::string printer_name; |
96 CHECK(args->GetString(0, &printer_id)); | 162 CHECK(args->GetString(0, &printer_id)); |
97 CHECK(args->GetString(1, &printer_name)); | 163 CHECK(args->GetString(1, &printer_name)); |
| 164 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( |
| 165 printer_id); |
98 | 166 |
99 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); | 167 chromeos::DebugDaemonClient::RemovePrinterCallback callback = |
| 168 base::Bind(&OnPrinterRemoved); |
| 169 base::Closure error_callback = base::Bind(&OperationError, "PRINTER_REMOVED"); |
| 170 |
| 171 chromeos::DebugDaemonClient* client = |
| 172 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 173 client->CupsRemovePrinter(printer_name, callback, error_callback); |
| 174 } |
| 175 |
| 176 void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { |
| 177 const base::DictionaryValue* printer_dict = nullptr; |
| 178 CHECK(args->GetDictionary(0, &printer_dict)); |
| 179 |
| 180 std::string printer_id; |
| 181 std::string printer_name; |
| 182 std::string printer_description; |
| 183 std::string printer_manufacturer; |
| 184 std::string printer_model; |
| 185 std::string printer_address; |
| 186 std::string printer_protocol; |
| 187 std::string printer_queue; |
| 188 CHECK(printer_dict->GetString("printerId", &printer_id)); |
| 189 CHECK(printer_dict->GetString("printerName", &printer_name)); |
| 190 CHECK(printer_dict->GetString("printerDescription", &printer_description)); |
| 191 CHECK(printer_dict->GetString("printerManufacturer", &printer_manufacturer)); |
| 192 CHECK(printer_dict->GetString("printerModel", &printer_model)); |
| 193 CHECK(printer_dict->GetString("printerAddress", &printer_address)); |
| 194 CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); |
| 195 CHECK(printer_dict->GetString("printerQueue", &printer_queue)); |
| 196 |
| 197 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(); |
| 198 printer->set_id(printer_id); |
100 printer->set_display_name(printer_name); | 199 printer->set_display_name(printer_name); |
| 200 printer->set_description(printer_description); |
| 201 printer->set_manufacturer(printer_manufacturer); |
| 202 printer->set_model(printer_model); |
| 203 printer->set_uri(printer_protocol + "://" + printer_address + "/" + |
| 204 printer_queue); |
| 205 |
101 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( | 206 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( |
102 std::move(printer)); | 207 std::move(printer)); |
103 } | 208 } |
104 | 209 |
105 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { | |
106 std::string printer_id; | |
107 CHECK(args->GetString(0, &printer_id)); | |
108 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( | |
109 printer_id); | |
110 } | |
111 | |
112 } // namespace settings | 210 } // namespace settings |
113 } // namespace chromeos | 211 } // namespace chromeos |
OLD | NEW |