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/download/download_prefs.h" | 15 #include "chrome/browser/download/download_prefs.h" |
15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
17 #include "chrome/browser/ui/browser_window.h" | 18 #include "chrome/browser/ui/browser_window.h" |
18 #include "chrome/browser/ui/chrome_select_file_policy.h" | 19 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 20 #include "chromeos/dbus/dbus_thread_manager.h" |
| 21 #include "chromeos/dbus/debug_daemon_client.h" |
19 #include "content/public/browser/browser_context.h" | 22 #include "content/public/browser/browser_context.h" |
20 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/web_ui.h" | 24 #include "content/public/browser/web_ui.h" |
22 #include "printing/backend/print_backend.h" | 25 #include "printing/backend/print_backend.h" |
23 #include "url/third_party/mozilla/url_parse.h" | 26 #include "url/third_party/mozilla/url_parse.h" |
24 | 27 |
25 namespace chromeos { | 28 namespace chromeos { |
26 namespace settings { | 29 namespace settings { |
27 | 30 |
| 31 namespace { |
| 32 |
| 33 void onRemovedPrinter(bool success) {} |
| 34 |
| 35 } // namespace |
| 36 |
28 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) | 37 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) |
29 : profile_(Profile::FromWebUI(webui)), weak_factory_(this) {} | 38 : profile_(Profile::FromWebUI(webui)), weak_factory_(this) {} |
30 | 39 |
31 CupsPrintersHandler::~CupsPrintersHandler() {} | 40 CupsPrintersHandler::~CupsPrintersHandler() {} |
32 | 41 |
33 void CupsPrintersHandler::RegisterMessages() { | 42 void CupsPrintersHandler::RegisterMessages() { |
34 web_ui()->RegisterMessageCallback( | 43 web_ui()->RegisterMessageCallback( |
35 "getCupsPrintersList", | 44 "getCupsPrintersList", |
36 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, | 45 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, |
37 base::Unretained(this))); | 46 base::Unretained(this))); |
38 web_ui()->RegisterMessageCallback( | 47 web_ui()->RegisterMessageCallback( |
39 "updateCupsPrinter", | 48 "updateCupsPrinter", |
40 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, | 49 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, |
41 base::Unretained(this))); | 50 base::Unretained(this))); |
42 web_ui()->RegisterMessageCallback( | 51 web_ui()->RegisterMessageCallback( |
43 "removeCupsPrinter", | 52 "removeCupsPrinter", |
44 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, | 53 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, |
45 base::Unretained(this))); | 54 base::Unretained(this))); |
46 web_ui()->RegisterMessageCallback( | 55 web_ui()->RegisterMessageCallback( |
| 56 "addCupsPrinter", base::Bind(&CupsPrintersHandler::HandleAddCupsPrinter, |
| 57 base::Unretained(this))); |
| 58 web_ui()->RegisterMessageCallback( |
47 "selectPPDFile", base::Bind(&CupsPrintersHandler::HandleSelectPPDFile, | 59 "selectPPDFile", base::Bind(&CupsPrintersHandler::HandleSelectPPDFile, |
48 base::Unretained(this))); | 60 base::Unretained(this))); |
49 } | 61 } |
50 | 62 |
51 void CupsPrintersHandler::HandleGetCupsPrintersList( | 63 void CupsPrintersHandler::HandleGetCupsPrintersList( |
52 const base::ListValue* args) { | 64 const base::ListValue* args) { |
53 AllowJavascript(); | 65 AllowJavascript(); |
54 | 66 |
55 CHECK_EQ(1U, args->GetSize()); | 67 CHECK_EQ(1U, args->GetSize()); |
56 std::string callback_id; | 68 std::string callback_id; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 CHECK(args->GetString(1, &printer_name)); | 117 CHECK(args->GetString(1, &printer_name)); |
106 | 118 |
107 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); | 119 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); |
108 printer->set_display_name(printer_name); | 120 printer->set_display_name(printer_name); |
109 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( | 121 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( |
110 std::move(printer)); | 122 std::move(printer)); |
111 } | 123 } |
112 | 124 |
113 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { | 125 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { |
114 std::string printer_id; | 126 std::string printer_id; |
| 127 std::string printer_name; |
115 CHECK(args->GetString(0, &printer_id)); | 128 CHECK(args->GetString(0, &printer_id)); |
| 129 CHECK(args->GetString(1, &printer_name)); |
116 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( | 130 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( |
117 printer_id); | 131 printer_id); |
| 132 |
| 133 chromeos::DebugDaemonClient* client = |
| 134 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 135 client->CupsRemovePrinter(printer_name, base::Bind(&onRemovedPrinter), |
| 136 base::Bind(&base::DoNothing)); |
| 137 } |
| 138 |
| 139 void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { |
| 140 AllowJavascript(); |
| 141 |
| 142 const base::DictionaryValue* printer_dict = nullptr; |
| 143 CHECK(args->GetDictionary(0, &printer_dict)); |
| 144 |
| 145 std::string printer_id; |
| 146 std::string printer_name; |
| 147 std::string printer_description; |
| 148 std::string printer_manufacturer; |
| 149 std::string printer_model; |
| 150 std::string printer_address; |
| 151 std::string printer_protocol; |
| 152 std::string printer_queue; |
| 153 std::string printer_ppd_path; |
| 154 CHECK(printer_dict->GetString("printerId", &printer_id)); |
| 155 CHECK(printer_dict->GetString("printerName", &printer_name)); |
| 156 CHECK(printer_dict->GetString("printerDescription", &printer_description)); |
| 157 CHECK(printer_dict->GetString("printerManufacturer", &printer_manufacturer)); |
| 158 CHECK(printer_dict->GetString("printerModel", &printer_model)); |
| 159 CHECK(printer_dict->GetString("printerAddress", &printer_address)); |
| 160 CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); |
| 161 CHECK(printer_dict->GetString("printerQueue", &printer_queue)); |
| 162 CHECK(printer_dict->GetString("printerPPDPath", &printer_ppd_path)); |
| 163 std::string printer_uri = |
| 164 printer_protocol + "://" + printer_address + "/" + printer_queue; |
| 165 |
| 166 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(); |
| 167 printer->set_id(printer_id); |
| 168 printer->set_display_name(printer_name); |
| 169 printer->set_description(printer_description); |
| 170 printer->set_manufacturer(printer_manufacturer); |
| 171 printer->set_model(printer_model); |
| 172 printer->set_uri(printer_uri); |
| 173 |
| 174 chromeos::DebugDaemonClient* client = |
| 175 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 176 client->CupsAddPrinter( |
| 177 printer_name, printer_uri, |
| 178 printer_ppd_path, |
| 179 printer_ppd_path.empty() ? true : false, // ipp everywhere |
| 180 base::Bind(&CupsPrintersHandler::OnAddedPrinter, |
| 181 weak_factory_.GetWeakPtr(), base::Passed(std::move(printer))), |
| 182 base::Bind(&CupsPrintersHandler::OnAddPrinterError, |
| 183 weak_factory_.GetWeakPtr())); |
| 184 } |
| 185 |
| 186 void CupsPrintersHandler::OnAddedPrinter(std::unique_ptr<Printer> printer, |
| 187 bool success) { |
| 188 std::string printer_name = printer->display_name(); |
| 189 if (success) { |
| 190 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( |
| 191 std::move(printer)); |
| 192 } |
| 193 CallJavascriptFunction( |
| 194 "cr.webUIListenerCallback", base::StringValue("on-add-cups-printer"), |
| 195 base::FundamentalValue(success), base::StringValue(printer_name)); |
| 196 } |
| 197 |
| 198 void CupsPrintersHandler::OnAddPrinterError() { |
| 199 CallJavascriptFunction("cr.webUIListenerCallback", |
| 200 base::StringValue("on-add-cups-printer"), |
| 201 base::FundamentalValue(false), base::StringValue("")); |
118 } | 202 } |
119 | 203 |
120 void CupsPrintersHandler::HandleSelectPPDFile(const base::ListValue* args) { | 204 void CupsPrintersHandler::HandleSelectPPDFile(const base::ListValue* args) { |
121 CHECK_EQ(1U, args->GetSize()); | 205 CHECK_EQ(1U, args->GetSize()); |
122 CHECK(args->GetString(0, &webui_callback_id_)); | 206 CHECK(args->GetString(0, &webui_callback_id_)); |
123 | 207 |
124 base::FilePath downloads_path = DownloadPrefs::FromDownloadManager( | 208 base::FilePath downloads_path = DownloadPrefs::FromDownloadManager( |
125 content::BrowserContext::GetDownloadManager(profile_))->DownloadPath(); | 209 content::BrowserContext::GetDownloadManager(profile_))->DownloadPath(); |
126 | 210 |
127 select_file_dialog_ = ui::SelectFileDialog::Create( | 211 select_file_dialog_ = ui::SelectFileDialog::Create( |
(...skipping 11 matching lines...) Expand all Loading... |
139 int index, | 223 int index, |
140 void* params) { | 224 void* params) { |
141 DCHECK(!webui_callback_id_.empty()); | 225 DCHECK(!webui_callback_id_.empty()); |
142 ResolveJavascriptCallback(base::StringValue(webui_callback_id_), | 226 ResolveJavascriptCallback(base::StringValue(webui_callback_id_), |
143 base::StringValue(path.value())); | 227 base::StringValue(path.value())); |
144 webui_callback_id_.clear(); | 228 webui_callback_id_.clear(); |
145 } | 229 } |
146 | 230 |
147 } // namespace settings | 231 } // namespace settings |
148 } // namespace chromeos | 232 } // namespace chromeos |
OLD | NEW |