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/print_preview/printer_backend_proxy.h" | 5 #include "chrome/browser/ui/webui/print_preview/printer_backend_proxy.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 const PrinterSetupCallback& cb) { | 63 const PrinterSetupCallback& cb) { |
64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
65 | 65 |
66 PrinterBasicInfo basic_info = ToBasicInfo(*printer); | 66 PrinterBasicInfo basic_info = ToBasicInfo(*printer); |
67 | 67 |
68 base::PostTaskWithTraitsAndReplyWithResult( | 68 base::PostTaskWithTraitsAndReplyWithResult( |
69 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, | 69 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, |
70 base::Bind(&GetSettingsOnBlockingPool, printer->id(), basic_info), cb); | 70 base::Bind(&GetSettingsOnBlockingPool, printer->id(), basic_info), cb); |
71 } | 71 } |
72 | 72 |
73 void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer, | |
74 const PrinterSetupCallback& cb, | |
75 chromeos::PrinterSetupResult result) { | |
76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
77 | |
78 switch (result) { | |
79 case chromeos::PrinterSetupResult::SUCCESS: | |
80 VLOG(1) << "Printer setup successful for " << printer->id() | |
81 << " fetching properties"; | |
82 | |
83 // fetch settings on the blocking pool and invoke callback. | |
84 FetchCapabilities(std::move(printer), cb); | |
85 return; | |
86 case chromeos::PrinterSetupResult::PPD_NOT_FOUND: | |
87 LOG(WARNING) << "Could not find PPD. Check printer configuration."; | |
88 // Prompt user to update configuration. | |
89 // TODO(skau): Fill me in | |
90 break; | |
91 case chromeos::PrinterSetupResult::PPD_UNRETRIEVABLE: | |
92 LOG(WARNING) << "Could not download PPD. Check Internet connection."; | |
93 // Could not download PPD. Connect to Internet. | |
94 // TODO(skau): Fill me in | |
95 break; | |
96 case chromeos::PrinterSetupResult::PRINTER_UNREACHABLE: | |
97 case chromeos::PrinterSetupResult::DBUS_ERROR: | |
98 case chromeos::PrinterSetupResult::PPD_TOO_LARGE: | |
99 case chromeos::PrinterSetupResult::INVALID_PPD: | |
100 case chromeos::PrinterSetupResult::FATAL_ERROR: | |
101 LOG(ERROR) << "Unexpected error in printer setup." << result; | |
102 break; | |
103 } | |
104 | |
105 // TODO(skau): Open printer settings if this is resolvable. | |
106 cb.Run(nullptr); | |
107 } | |
108 | |
109 class PrinterBackendProxyChromeos : public PrinterBackendProxy { | 73 class PrinterBackendProxyChromeos : public PrinterBackendProxy { |
110 public: | 74 public: |
111 explicit PrinterBackendProxyChromeos(Profile* profile) | 75 explicit PrinterBackendProxyChromeos(Profile* profile) |
112 : prefs_(chromeos::PrintersManagerFactory::GetForBrowserContext(profile)), | 76 : prefs_(chromeos::PrintersManagerFactory::GetForBrowserContext(profile)), |
113 printer_configurer_(chromeos::PrinterConfigurer::Create(profile)) { | 77 printer_configurer_(chromeos::PrinterConfigurer::Create(profile)), |
| 78 weak_factory_(this) { |
114 // Construct the CupsPrintJobManager to listen for printing events. | 79 // Construct the CupsPrintJobManager to listen for printing events. |
115 chromeos::CupsPrintJobManagerFactory::GetForBrowserContext(profile); | 80 chromeos::CupsPrintJobManagerFactory::GetForBrowserContext(profile); |
116 } | 81 } |
117 | 82 |
118 ~PrinterBackendProxyChromeos() override { | 83 ~PrinterBackendProxyChromeos() override { |
119 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 84 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
120 } | 85 } |
121 | 86 |
122 void GetDefaultPrinter(const DefaultPrinterCallback& cb) override { | 87 void GetDefaultPrinter(const DefaultPrinterCallback& cb) override { |
123 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
(...skipping 27 matching lines...) Expand all Loading... |
151 | 116 |
152 std::unique_ptr<chromeos::Printer> printer = | 117 std::unique_ptr<chromeos::Printer> printer = |
153 prefs_->GetPrinter(printer_name); | 118 prefs_->GetPrinter(printer_name); |
154 if (!printer) { | 119 if (!printer) { |
155 // If the printer was removed, the lookup will fail. | 120 // If the printer was removed, the lookup will fail. |
156 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 121 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
157 base::Bind(cb, nullptr)); | 122 base::Bind(cb, nullptr)); |
158 return; | 123 return; |
159 } | 124 } |
160 | 125 |
| 126 if (prefs_->IsConfigurationCurrent(*printer)) { |
| 127 // Skip setup if the printer is already installed. |
| 128 HandlePrinterSetup(std::move(printer), cb, chromeos::SUCCESS); |
| 129 return; |
| 130 } |
| 131 |
161 const chromeos::Printer& printer_ref = *printer; | 132 const chromeos::Printer& printer_ref = *printer; |
162 printer_configurer_->SetUpPrinter( | 133 printer_configurer_->SetUpPrinter( |
163 printer_ref, | 134 printer_ref, |
164 base::Bind(&HandlePrinterSetup, base::Passed(&printer), cb)); | 135 base::Bind(&PrinterBackendProxyChromeos::HandlePrinterSetup, |
| 136 weak_factory_.GetWeakPtr(), base::Passed(&printer), cb)); |
165 }; | 137 }; |
166 | 138 |
167 private: | 139 private: |
| 140 void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer, |
| 141 const PrinterSetupCallback& cb, |
| 142 chromeos::PrinterSetupResult result) { |
| 143 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 144 |
| 145 switch (result) { |
| 146 case chromeos::PrinterSetupResult::SUCCESS: |
| 147 VLOG(1) << "Printer setup successful for " << printer->id() |
| 148 << " fetching properties"; |
| 149 prefs_->PrinterInstalled(*printer); |
| 150 |
| 151 // fetch settings on the blocking pool and invoke callback. |
| 152 FetchCapabilities(std::move(printer), cb); |
| 153 return; |
| 154 case chromeos::PrinterSetupResult::PPD_NOT_FOUND: |
| 155 LOG(WARNING) << "Could not find PPD. Check printer configuration."; |
| 156 // Prompt user to update configuration. |
| 157 // TODO(skau): Fill me in |
| 158 break; |
| 159 case chromeos::PrinterSetupResult::PPD_UNRETRIEVABLE: |
| 160 LOG(WARNING) << "Could not download PPD. Check Internet connection."; |
| 161 // Could not download PPD. Connect to Internet. |
| 162 // TODO(skau): Fill me in |
| 163 break; |
| 164 case chromeos::PrinterSetupResult::PRINTER_UNREACHABLE: |
| 165 case chromeos::PrinterSetupResult::DBUS_ERROR: |
| 166 case chromeos::PrinterSetupResult::PPD_TOO_LARGE: |
| 167 case chromeos::PrinterSetupResult::INVALID_PPD: |
| 168 case chromeos::PrinterSetupResult::FATAL_ERROR: |
| 169 LOG(ERROR) << "Unexpected error in printer setup." << result; |
| 170 break; |
| 171 } |
| 172 |
| 173 // TODO(skau): Open printer settings if this is resolvable. |
| 174 cb.Run(nullptr); |
| 175 } |
| 176 |
168 chromeos::PrintersManager* prefs_; | 177 chromeos::PrintersManager* prefs_; |
169 scoped_refptr<chromeos::printing::PpdProvider> ppd_provider_; | 178 scoped_refptr<chromeos::printing::PpdProvider> ppd_provider_; |
170 std::unique_ptr<chromeos::PrinterConfigurer> printer_configurer_; | 179 std::unique_ptr<chromeos::PrinterConfigurer> printer_configurer_; |
| 180 base::WeakPtrFactory<PrinterBackendProxyChromeos> weak_factory_; |
171 | 181 |
172 DISALLOW_COPY_AND_ASSIGN(PrinterBackendProxyChromeos); | 182 DISALLOW_COPY_AND_ASSIGN(PrinterBackendProxyChromeos); |
173 }; | 183 }; |
174 | 184 |
175 } // namespace | 185 } // namespace |
176 | 186 |
177 std::unique_ptr<PrinterBackendProxy> PrinterBackendProxy::Create( | 187 std::unique_ptr<PrinterBackendProxy> PrinterBackendProxy::Create( |
178 Profile* profile) { | 188 Profile* profile) { |
179 return base::MakeUnique<PrinterBackendProxyChromeos>(profile); | 189 return base::MakeUnique<PrinterBackendProxyChromeos>(profile); |
180 } | 190 } |
181 | 191 |
182 } // namespace printing | 192 } // namespace printing |
OLD | NEW |