Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(303)

Side by Side Diff: chrome/browser/ui/webui/print_preview/printer_backend_proxy.cc

Issue 2457933004: Register and select printer on click. (Closed)
Patch Set: rebase Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/print_preview/printer_backend_proxy.h" 5 #include "chrome/browser/ui/webui/print_preview/printer_backend_proxy.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility>
8 9
9 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h"
10 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "chrome/browser/ui/webui/print_preview/printer_capabilities.h"
11 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
12 #include "printing/backend/print_backend.h" 15 #include "printing/backend/print_backend.h"
13 16
14 namespace printing { 17 namespace printing {
15 18
19 namespace {
20
21 void PostCallbackOnUIThread(const PrinterSetupCallback& cb,
22 std::unique_ptr<base::DictionaryValue> result) {
23 content::BrowserThread::PostTask(
24 content::BrowserThread::UI, FROM_HERE,
25 base::Bind(cb, base::Passed(std::move(result))));
Lei Zhang 2016/11/03 23:41:46 Also less verbose if std::move(result) is removed
skau 2016/11/04 19:28:59 Switched to PostTaskAndReply. Code has been remov
26 }
27
28 } // namespace
29
16 std::string GetDefaultPrinterOnBlockingPoolThread() { 30 std::string GetDefaultPrinterOnBlockingPoolThread() {
17 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 31 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
18 32
19 scoped_refptr<printing::PrintBackend> print_backend( 33 scoped_refptr<printing::PrintBackend> print_backend(
20 PrintBackend::CreateInstance(nullptr)); 34 PrintBackend::CreateInstance(nullptr));
21 35
22 std::string default_printer = print_backend->GetDefaultPrinterName(); 36 std::string default_printer = print_backend->GetDefaultPrinterName();
23 VLOG(1) << "Default Printer: " << default_printer; 37 VLOG(1) << "Default Printer: " << default_printer;
24 return default_printer; 38 return default_printer;
25 } 39 }
26 40
27 PrinterList EnumeratePrintersOnBlockingPoolThread(Profile* /* profile */) { 41 PrinterList EnumeratePrintersOnBlockingPoolThread(Profile* /* profile */) {
28 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 42 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
29 43
30 scoped_refptr<PrintBackend> print_backend( 44 scoped_refptr<PrintBackend> print_backend(
31 PrintBackend::CreateInstance(nullptr)); 45 PrintBackend::CreateInstance(nullptr));
32 46
33 PrinterList printer_list; 47 PrinterList printer_list;
34 print_backend->EnumeratePrinters(&printer_list); 48 print_backend->EnumeratePrinters(&printer_list);
35 49
36 return printer_list; 50 return printer_list;
37 } 51 }
38 52
53 void ConfigurePrinterAndFetchCapabilities(Profile* /* profile */,
54 const std::string& device_name,
55 const PrinterSetupCallback& cb) {
56 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
57
58 scoped_refptr<printing::PrintBackend> print_backend(
59 printing::PrintBackend::CreateInstance(nullptr));
60
61 VLOG(1) << "Get printer capabilities start for " << device_name;
62
63 std::unique_ptr<base::DictionaryValue> printer_info;
64 if (!print_backend->IsValidPrinter(device_name)) {
65 LOG(WARNING) << "Invalid printer " << device_name;
66 PostCallbackOnUIThread(cb, nullptr);
Lei Zhang 2016/11/03 23:41:46 An alternative to having PostCallbackOnUIThread()
skau 2016/11/04 19:28:59 Thanks. I've done that and it is simpler now.
67 return;
68 }
69
70 PrinterBasicInfo basic_info;
71 if (!print_backend->GetPrinterBasicInfo(device_name, &basic_info)) {
72 PostCallbackOnUIThread(cb, nullptr);
73 return;
74 }
75
76 PostCallbackOnUIThread(cb, GetSettingsDictionary(device_name, basic_info));
77 }
78
39 } // namespace printing 79 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698