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 <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.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" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" | 14 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" |
14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chromeos/dbus/dbus_thread_manager.h" | |
17 #include "chromeos/dbus/debug_daemon_client.h" | |
15 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/web_ui.h" | 19 #include "content/public/browser/web_ui.h" |
17 #include "printing/backend/print_backend.h" | 20 #include "printing/backend/print_backend.h" |
18 #include "url/third_party/mozilla/url_parse.h" | 21 #include "url/third_party/mozilla/url_parse.h" |
19 | 22 |
20 namespace chromeos { | 23 namespace chromeos { |
21 namespace settings { | 24 namespace settings { |
22 | 25 |
26 namespace { | |
27 | |
28 void onRemovedPrinter(bool success) {} | |
29 | |
30 } // namespace | |
31 | |
23 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) | 32 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) |
24 : profile_(Profile::FromWebUI(webui)), weak_factory_(this) {} | 33 : profile_(Profile::FromWebUI(webui)), weak_factory_(this) {} |
25 | 34 |
26 CupsPrintersHandler::~CupsPrintersHandler() {} | 35 CupsPrintersHandler::~CupsPrintersHandler() {} |
27 | 36 |
28 void CupsPrintersHandler::RegisterMessages() { | 37 void CupsPrintersHandler::RegisterMessages() { |
29 web_ui()->RegisterMessageCallback( | 38 web_ui()->RegisterMessageCallback( |
30 "getCupsPrintersList", | 39 "getCupsPrintersList", |
31 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, | 40 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, |
32 base::Unretained(this))); | 41 base::Unretained(this))); |
33 web_ui()->RegisterMessageCallback( | 42 web_ui()->RegisterMessageCallback( |
34 "updateCupsPrinter", | 43 "updateCupsPrinter", |
35 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, | 44 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, |
36 base::Unretained(this))); | 45 base::Unretained(this))); |
37 web_ui()->RegisterMessageCallback( | 46 web_ui()->RegisterMessageCallback( |
38 "removeCupsPrinter", | 47 "removeCupsPrinter", |
39 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, | 48 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, |
40 base::Unretained(this))); | 49 base::Unretained(this))); |
50 web_ui()->RegisterMessageCallback( | |
51 "addCupsPrinter", base::Bind(&CupsPrintersHandler::HandleAddCupsPrinter, | |
52 base::Unretained(this))); | |
41 } | 53 } |
42 | 54 |
43 void CupsPrintersHandler::HandleGetCupsPrintersList( | 55 void CupsPrintersHandler::HandleGetCupsPrintersList( |
44 const base::ListValue* args) { | 56 const base::ListValue* args) { |
45 AllowJavascript(); | 57 AllowJavascript(); |
46 | 58 |
47 CHECK_EQ(1U, args->GetSize()); | 59 CHECK_EQ(1U, args->GetSize()); |
48 std::string callback_id; | 60 std::string callback_id; |
49 CHECK(args->GetString(0, &callback_id)); | 61 CHECK(args->GetString(0, &callback_id)); |
50 | 62 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 CHECK(args->GetString(1, &printer_name)); | 109 CHECK(args->GetString(1, &printer_name)); |
98 | 110 |
99 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); | 111 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); |
100 printer->set_display_name(printer_name); | 112 printer->set_display_name(printer_name); |
101 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( | 113 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( |
102 std::move(printer)); | 114 std::move(printer)); |
103 } | 115 } |
104 | 116 |
105 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { | 117 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { |
106 std::string printer_id; | 118 std::string printer_id; |
119 std::string printer_name; | |
107 CHECK(args->GetString(0, &printer_id)); | 120 CHECK(args->GetString(0, &printer_id)); |
121 CHECK(args->GetString(1, &printer_name)); | |
108 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( | 122 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( |
109 printer_id); | 123 printer_id); |
124 | |
125 chromeos::DebugDaemonClient* client = | |
126 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | |
127 client->CupsRemovePrinter(printer_name, base::Bind(&onRemovedPrinter), | |
128 base::Bind(&base::DoNothing)); | |
129 } | |
130 | |
131 void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { | |
132 AllowJavascript(); | |
133 | |
134 const base::DictionaryValue* printer_dict = nullptr; | |
135 CHECK(args->GetDictionary(0, &printer_dict)); | |
136 | |
137 std::string printer_id; | |
138 std::string printer_name; | |
139 std::string printer_description; | |
140 std::string printer_manufacturer; | |
141 std::string printer_model; | |
142 std::string printer_address; | |
143 std::string printer_protocol; | |
144 std::string printer_queue; | |
145 CHECK(printer_dict->GetString("printerId", &printer_id)); | |
146 CHECK(printer_dict->GetString("printerName", &printer_name)); | |
147 CHECK(printer_dict->GetString("printerDescription", &printer_description)); | |
148 CHECK(printer_dict->GetString("printerManufacturer", &printer_manufacturer)); | |
149 CHECK(printer_dict->GetString("printerModel", &printer_model)); | |
150 CHECK(printer_dict->GetString("printerAddress", &printer_address)); | |
151 CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); | |
152 CHECK(printer_dict->GetString("printerQueue", &printer_queue)); | |
153 std::string printer_uri = | |
154 printer_protocol + "://" + printer_address + "/" + printer_queue; | |
155 | |
156 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(); | |
157 printer->set_id(printer_id); | |
158 printer->set_display_name(printer_name); | |
159 printer->set_description(printer_description); | |
160 printer->set_manufacturer(printer_manufacturer); | |
161 printer->set_model(printer_model); | |
162 printer->set_uri(printer_uri); | |
163 | |
164 chromeos::DebugDaemonClient* client = | |
165 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | |
166 client->CupsAddPrinter( | |
167 printer_name, printer_uri, | |
168 "", // ppd path, will update it later | |
169 true, // ipp everywhere | |
170 base::Bind(&CupsPrintersHandler::OnAddedPrinter, | |
171 weak_factory_.GetWeakPtr(), base::Passed(std::move(printer))), | |
172 base::Bind(&CupsPrintersHandler::OnAddPrinterError, | |
173 weak_factory_.GetWeakPtr())); | |
174 } | |
175 | |
176 void CupsPrintersHandler::OnAddedPrinter(std::unique_ptr<Printer> printer, | |
177 bool success) { | |
178 std::string printer_name = printer->display_name(); | |
179 if (success) { | |
180 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( | |
181 std::move(printer)); | |
182 } | |
183 CallJavascriptFunction( | |
184 "cr.webUIListenerCallback", base::StringValue("on-add-cups-printer"), | |
185 base::FundamentalValue(success), base::StringValue(printer_name)); | |
186 } | |
187 | |
188 void CupsPrintersHandler::OnAddPrinterError() { | |
189 CallJavascriptFunction("cr.webUIListenerCallback", | |
michaelpg
2016/09/20 20:58:42
nit: indent this and above consistently?
xdai1
2016/09/21 17:40:20
This is what "git cl format" gives me...
michaelpg
2016/09/21 19:13:23
Acknowledged.
| |
190 base::StringValue("on-add-cups-printer"), | |
191 base::FundamentalValue(false), base::StringValue("")); | |
110 } | 192 } |
111 | 193 |
112 } // namespace settings | 194 } // namespace settings |
113 } // namespace chromeos | 195 } // namespace chromeos |
OLD | NEW |