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/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" | 13 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" |
| 14 #include "chrome/browser/download/download_prefs.h" |
14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/browser_finder.h" |
| 17 #include "chrome/browser/ui/browser_window.h" |
| 18 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 19 #include "content/public/browser/browser_context.h" |
15 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/web_ui.h" | 21 #include "content/public/browser/web_ui.h" |
17 #include "printing/backend/print_backend.h" | 22 #include "printing/backend/print_backend.h" |
18 #include "url/third_party/mozilla/url_parse.h" | 23 #include "url/third_party/mozilla/url_parse.h" |
19 | 24 |
20 namespace chromeos { | 25 namespace chromeos { |
21 namespace settings { | 26 namespace settings { |
22 | 27 |
23 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) | 28 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) |
24 : profile_(Profile::FromWebUI(webui)), weak_factory_(this) {} | 29 : profile_(Profile::FromWebUI(webui)), weak_factory_(this) {} |
25 | 30 |
26 CupsPrintersHandler::~CupsPrintersHandler() {} | 31 CupsPrintersHandler::~CupsPrintersHandler() {} |
27 | 32 |
28 void CupsPrintersHandler::RegisterMessages() { | 33 void CupsPrintersHandler::RegisterMessages() { |
29 web_ui()->RegisterMessageCallback( | 34 web_ui()->RegisterMessageCallback( |
30 "getCupsPrintersList", | 35 "getCupsPrintersList", |
31 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, | 36 base::Bind(&CupsPrintersHandler::HandleGetCupsPrintersList, |
32 base::Unretained(this))); | 37 base::Unretained(this))); |
33 web_ui()->RegisterMessageCallback( | 38 web_ui()->RegisterMessageCallback( |
34 "updateCupsPrinter", | 39 "updateCupsPrinter", |
35 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, | 40 base::Bind(&CupsPrintersHandler::HandleUpdateCupsPrinter, |
36 base::Unretained(this))); | 41 base::Unretained(this))); |
37 web_ui()->RegisterMessageCallback( | 42 web_ui()->RegisterMessageCallback( |
38 "removeCupsPrinter", | 43 "removeCupsPrinter", |
39 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, | 44 base::Bind(&CupsPrintersHandler::HandleRemoveCupsPrinter, |
40 base::Unretained(this))); | 45 base::Unretained(this))); |
| 46 web_ui()->RegisterMessageCallback( |
| 47 "selectPPDFile", base::Bind(&CupsPrintersHandler::HandleSelectPPDFile, |
| 48 base::Unretained(this))); |
41 } | 49 } |
42 | 50 |
43 void CupsPrintersHandler::HandleGetCupsPrintersList( | 51 void CupsPrintersHandler::HandleGetCupsPrintersList( |
44 const base::ListValue* args) { | 52 const base::ListValue* args) { |
45 AllowJavascript(); | 53 AllowJavascript(); |
46 | 54 |
47 CHECK_EQ(1U, args->GetSize()); | 55 CHECK_EQ(1U, args->GetSize()); |
48 std::string callback_id; | 56 std::string callback_id; |
49 CHECK(args->GetString(0, &callback_id)); | 57 CHECK(args->GetString(0, &callback_id)); |
50 | 58 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 std::move(printer)); | 110 std::move(printer)); |
103 } | 111 } |
104 | 112 |
105 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { | 113 void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { |
106 std::string printer_id; | 114 std::string printer_id; |
107 CHECK(args->GetString(0, &printer_id)); | 115 CHECK(args->GetString(0, &printer_id)); |
108 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( | 116 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RemovePrinter( |
109 printer_id); | 117 printer_id); |
110 } | 118 } |
111 | 119 |
| 120 void CupsPrintersHandler::HandleSelectPPDFile(const base::ListValue* args) { |
| 121 CHECK_EQ(1U, args->GetSize()); |
| 122 CHECK(args->GetString(0, &webui_callback_id_)); |
| 123 |
| 124 base::FilePath downloads_path = DownloadPrefs::FromDownloadManager( |
| 125 content::BrowserContext::GetDownloadManager(profile_))->DownloadPath(); |
| 126 |
| 127 select_file_dialog_ = ui::SelectFileDialog::Create( |
| 128 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents())); |
| 129 gfx::NativeWindow owning_window = |
| 130 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents()) |
| 131 ->window() |
| 132 ->GetNativeWindow(); |
| 133 select_file_dialog_->SelectFile( |
| 134 ui::SelectFileDialog::SELECT_OPEN_FILE, base::string16(), downloads_path, |
| 135 nullptr, 0, FILE_PATH_LITERAL(""), owning_window, nullptr); |
| 136 } |
| 137 |
| 138 void CupsPrintersHandler::FileSelected(const base::FilePath& path, |
| 139 int index, |
| 140 void* params) { |
| 141 DCHECK(!webui_callback_id_.empty()); |
| 142 ResolveJavascriptCallback(base::StringValue(webui_callback_id_), |
| 143 base::StringValue(path.value())); |
| 144 webui_callback_id_.clear(); |
| 145 } |
| 146 |
112 } // namespace settings | 147 } // namespace settings |
113 } // namespace chromeos | 148 } // namespace chromeos |
OLD | NEW |