OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 #include "chromeos/printing/ppd_provider.h" | |
5 | |
6 #include <utility> | |
7 | |
8 #include "base/files/file_util.h" | |
9 #include "base/json/json_parser.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "base/time/time.h" | |
14 #include "base/values.h" | |
15 #include "chromeos/printing/ppd_cache.h" | |
16 #include "net/base/load_flags.h" | |
17 #include "net/http/http_status_code.h" | |
18 #include "net/url_request/url_fetcher.h" | |
19 #include "net/url_request/url_fetcher_delegate.h" | |
20 #include "net/url_request/url_request_context_getter.h" | |
21 #include "url/gurl.h" | |
22 | |
23 using ::base::DictionaryValue; | |
24 using ::base::FilePath; | |
25 using ::base::Optional; | |
26 using ::base::Value; | |
27 using ::net::URLFetcher; | |
28 using ::net::URLFetcherDelegate; | |
29 using ::std::string; | |
30 using ::std::unique_ptr; | |
31 | |
32 namespace chromeos { | |
33 namespace printing { | |
34 namespace { | |
35 | |
36 // Expected fields from the quirks server. | |
37 const char kJSONPPDKey[] = "compressedPpd"; | |
38 const char kJSONLastUpdatedKey[] = "lastUpdatedTime"; | |
39 | |
40 class PpdProviderImpl; | |
41 | |
42 // URLFetcherDelegate that just forwards the complete callback back to | |
43 // the PpdProvider that owns the delegate. | |
44 class ForwardingURLFetcherDelegate : public URLFetcherDelegate { | |
45 public: | |
46 explicit ForwardingURLFetcherDelegate(PpdProviderImpl* owner) | |
47 : owner_(owner) {} | |
48 ~ForwardingURLFetcherDelegate() override {} | |
49 | |
50 // URLFetcherDelegate API method. Defined below since we need the | |
51 // PpdProviderImpl definition first. | |
52 void OnURLFetchComplete(const URLFetcher* source) override; | |
53 | |
54 private: | |
55 // owner of this delegate. | |
56 PpdProviderImpl* owner_; | |
57 }; | |
58 | |
59 class PpdProviderImpl : public PpdProvider { | |
60 public: | |
61 PpdProviderImpl( | |
62 const string& api_key, | |
63 scoped_refptr<net::URLRequestContextGetter> url_context_getter, | |
64 unique_ptr<PpdCache> cache, | |
65 const PpdProvider::Options& options) | |
66 : api_key_(api_key), | |
67 forwarding_delegate_(this), | |
68 url_context_getter_(url_context_getter), | |
69 cache_(std::move(cache)), | |
70 options_(options) { | |
71 CHECK_GT(options_.max_ppd_contents_size_, static_cast<size_t>(0)); | |
72 } | |
73 ~PpdProviderImpl() override {} | |
74 | |
75 void Resolve(const Printer::PpdReference& ppd_reference, | |
76 PpdProvider::ResolveCallback cb) override { | |
skau
2016/10/14 22:10:16
Given what we're doing with fetcher_ we need a gua
Carlson
2016/10/14 23:05:56
That's something I've been unclear on. There will
skau
2016/10/14 23:51:49
Right now, the only use is serial. But if we ever
| |
77 Optional<FilePath> tmp = cache_->Find(ppd_reference); | |
78 if (tmp) { | |
79 // Cache hit. Schedule the callback right now. | |
80 url_context_getter_->GetNetworkTaskRunner()->PostTask( | |
81 FROM_HERE, base::Bind(cb, PpdProvider::SUCCESS, tmp.value())); | |
82 return; | |
83 } | |
84 | |
85 // We don't have a way to automatically resolve user-supplied ppds yet. So | |
86 // if we have one specified, and it's not cached, we fail out rather than | |
87 // fall back to quirks-server based resolution. The reasoning here is that | |
88 // if the user has specified a ppd when a quirks-server one exists, it | |
89 // probably means the quirks server one doesn't work for some reason, so we | |
90 // shouldn't silently use it. | |
91 if (!ppd_reference.user_supplied_ppd_url.empty()) { | |
92 // Cache hit. Schedule the callback right now. | |
93 url_context_getter_->GetNetworkTaskRunner()->PostTask( | |
94 FROM_HERE, base::Bind(cb, PpdProvider::NOT_FOUND, tmp.value())); | |
95 return; | |
96 } | |
97 | |
98 active_reference_ = ppd_reference; | |
99 done_callback_ = cb; | |
100 | |
101 fetcher_ = net::URLFetcher::Create(GetQuirksServerLookupURL(ppd_reference), | |
102 URLFetcher::GET, &forwarding_delegate_); | |
103 fetcher_->SetRequestContext(url_context_getter_.get()); | |
104 fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | | |
105 net::LOAD_DO_NOT_SAVE_COOKIES | | |
106 net::LOAD_DO_NOT_SEND_COOKIES | | |
107 net::LOAD_DO_NOT_SEND_AUTH_DATA); | |
108 fetcher_->Start(); | |
109 }; | |
110 | |
111 void AbortResolve() override { | |
112 // UrlFetcher guarantees that when the object has been destroyed, no further | |
113 // callbacks will occur. | |
114 fetcher_.reset(); | |
115 } | |
116 | |
117 bool CachePpd(const Printer::PpdReference& ppd_reference, | |
skau
2016/10/14 22:10:16
I liked having separate Store functions as it was
Carlson
2016/10/14 23:05:56
The issue is *normally* storing is invisible to th
| |
118 const base::FilePath& ppd_path) override { | |
119 string buf; | |
120 return base::ReadFileToStringWithMaxSize(ppd_path, &buf, | |
121 options_.max_ppd_contents_size_) && | |
122 (base::WriteFile(ppd_path, buf.data(), buf.size()) == | |
123 static_cast<int>(buf.size())); | |
124 } | |
125 | |
126 // Called on the network thread when the fetcher completes its fetch. | |
127 void OnURLFetchComplete() { | |
128 // Scope the allocated fetcher_ into this function so we clean it up when | |
129 // we're done here instead of leaving it around until the next Resolve call. | |
130 auto fetcher = std::move(fetcher_); | |
131 string contents; | |
132 if ((fetcher->GetStatus().status() != net::URLRequestStatus::SUCCESS) || | |
133 (fetcher->GetResponseCode() != net::HTTP_OK) || | |
134 !fetcher->GetResponseAsString(&contents)) { | |
135 // Something went wrong with the fetch. | |
136 done_callback_.Run(PpdProvider::SERVER_ERROR, FilePath()); | |
137 return; | |
138 } | |
139 | |
140 auto parsed_json = ::base::JSONReader::Read(contents); | |
141 DictionaryValue* dict; | |
142 if (parsed_json == nullptr || // | |
143 !parsed_json->GetAsDictionary(&dict)) { | |
144 // Malformed response. TODO(justincarlson) - LOG something here? | |
145 done_callback_.Run(PpdProvider::SERVER_ERROR, FilePath()); | |
146 return; | |
147 } | |
148 string ppd_contents; | |
149 string last_updated_time_string; | |
150 uint64_t last_updated_time; | |
151 if (!(dict->GetString(kJSONPPDKey, &ppd_contents) && | |
152 dict->GetString(kJSONLastUpdatedKey, &last_updated_time_string) && | |
153 ::base::StringToUint64(last_updated_time_string, | |
154 &last_updated_time))) { | |
155 // Malformed response. TODO(justincarlson) - LOG something here? | |
156 done_callback_.Run(PpdProvider::SERVER_ERROR, FilePath()); | |
157 return; | |
158 } | |
159 | |
160 if (ppd_contents.size() > options_.max_ppd_contents_size_) { | |
161 // PPD is too big. | |
162 // | |
163 // Note -- if we ever add shared-ppd-sourcing, e.g. we may serve a ppd to | |
164 // a user that's not from an explicitly trusted source, we should also | |
165 // check *uncompressed* size here to head off zip-bombs (e.g. let's | |
166 // compress 1GBs of zeros into a 900kb file and see what cups does when it | |
167 // tries to expand that...) | |
168 done_callback_.Run(PpdProvider::SERVER_ERROR, FilePath()); | |
169 return; | |
170 } | |
171 | |
172 auto ppd_file = cache_->Store(active_reference_, ppd_contents); | |
173 if (!ppd_file) { | |
174 // Failed to store. | |
175 done_callback_.Run(PpdProvider::INTERNAL_ERROR, FilePath()); | |
176 return; | |
177 } | |
178 done_callback_.Run(PpdProvider::SUCCESS, ppd_file.value()); | |
179 } | |
180 | |
181 private: | |
182 // Generate a url to look up a manufacturer/model from the quirks server | |
183 GURL GetQuirksServerLookupURL( | |
184 const Printer::PpdReference& ppd_reference) const { | |
185 return GURL(::base::JoinString( | |
186 { | |
187 "https://", // | |
188 options_.quirks_server, // | |
189 "/v2/printer/manufacturers/", // | |
190 ppd_reference.effective_manufacturer, // | |
191 "/models/", // | |
192 ppd_reference.effective_model, // | |
193 "?key=", // | |
194 api_key_ // | |
195 }, | |
196 "")); | |
197 } | |
198 | |
199 // API key for accessing quirks server. | |
200 const string api_key_; | |
201 | |
202 // Reference we're currently trying to resolve. | |
203 Printer::PpdReference active_reference_; | |
204 | |
205 ForwardingURLFetcherDelegate forwarding_delegate_; | |
206 scoped_refptr<net::URLRequestContextGetter> url_context_getter_; | |
207 unique_ptr<PpdCache> cache_; | |
208 | |
209 PpdProvider::ResolveCallback done_callback_; | |
210 | |
211 // Fetcher for the current resolve call, if any. | |
212 unique_ptr<::net::URLFetcher> fetcher_; | |
213 | |
214 // Construction-time options, immutable. | |
215 const PpdProvider::Options options_; | |
216 }; | |
217 | |
218 void ForwardingURLFetcherDelegate::OnURLFetchComplete( | |
219 const URLFetcher* source) { | |
220 owner_->OnURLFetchComplete(); | |
221 } | |
222 | |
223 } // namespace | |
224 | |
225 // static | |
226 PpdProvider::Options PpdProvider::Defaults() { | |
227 PpdProvider::Options ret; | |
228 ret.quirks_server = "chromeosquirksserver-pa.googleapis.com"; | |
229 ret.max_ppd_contents_size_ = 1024 * 1024; | |
230 return ret; | |
231 } | |
232 | |
233 // static | |
234 unique_ptr<PpdProvider> PpdProvider::Create( | |
235 const string& api_key, | |
236 scoped_refptr<net::URLRequestContextGetter> url_context_getter, | |
237 unique_ptr<PpdCache> cache, | |
238 const PpdProvider::Options& options) { | |
239 return ::base::MakeUnique<PpdProviderImpl>(api_key, url_context_getter, | |
240 std::move(cache), options); | |
241 } | |
242 | |
243 } // namespace printing | |
244 } // namespace chromeos | |
OLD | NEW |