Chromium Code Reviews| 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 { | |
| 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, base::FilePath())); | |
| 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, | |
| 118 const base::FilePath& ppd_path) override { | |
| 119 string buf; | |
| 120 if (!base::ReadFileToStringWithMaxSize(ppd_path, &buf, | |
| 121 options_.max_ppd_contents_size_)) { | |
| 122 return false; | |
| 123 } | |
| 124 return static_cast<bool>(cache_->Store(ppd_reference, buf)); | |
| 125 } | |
| 126 | |
| 127 // Called on the network thread when the fetcher completes its fetch. | |
| 128 void OnURLFetchComplete() { | |
| 129 // Scope the allocated fetcher_ into this function so we clean it up when | |
| 130 // we're done here instead of leaving it around until the next Resolve call. | |
| 131 auto fetcher = std::move(fetcher_); | |
| 132 string contents; | |
| 133 if ((fetcher->GetStatus().status() != net::URLRequestStatus::SUCCESS) || | |
| 134 (fetcher->GetResponseCode() != net::HTTP_OK) || | |
| 135 !fetcher->GetResponseAsString(&contents)) { | |
| 136 // Something went wrong with the fetch. | |
| 137 done_callback_.Run(PpdProvider::SERVER_ERROR, FilePath()); | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 auto parsed_json = ::base::JSONReader::Read(contents); | |
| 142 DictionaryValue* dict; | |
| 143 if (parsed_json == nullptr || // | |
| 144 !parsed_json->GetAsDictionary(&dict)) { | |
| 145 // Malformed response. TODO(justincarlson) - LOG something here? | |
| 146 done_callback_.Run(PpdProvider::SERVER_ERROR, FilePath()); | |
| 147 return; | |
| 148 } | |
| 149 string ppd_contents; | |
| 150 string last_updated_time_string; | |
| 151 uint64_t last_updated_time; | |
| 152 if (!(dict->GetString(kJSONPPDKey, &ppd_contents) && | |
| 153 dict->GetString(kJSONLastUpdatedKey, &last_updated_time_string) && | |
| 154 ::base::StringToUint64(last_updated_time_string, | |
| 155 &last_updated_time))) { | |
| 156 // Malformed response. TODO(justincarlson) - LOG something here? | |
| 157 done_callback_.Run(PpdProvider::SERVER_ERROR, FilePath()); | |
| 158 return; | |
| 159 } | |
| 160 | |
| 161 if (ppd_contents.size() > options_.max_ppd_contents_size_) { | |
| 162 // PPD is too big. | |
| 163 // | |
| 164 // Note -- if we ever add shared-ppd-sourcing, e.g. we may serve a ppd to | |
| 165 // a user that's not from an explicitly trusted source, we should also | |
| 166 // check *uncompressed* size here to head off zip-bombs (e.g. let's | |
| 167 // compress 1GBs of zeros into a 900kb file and see what cups does when it | |
| 168 // tries to expand that...) | |
| 169 done_callback_.Run(PpdProvider::SERVER_ERROR, FilePath()); | |
| 170 return; | |
| 171 } | |
| 172 | |
| 173 auto ppd_file = cache_->Store(active_reference_, ppd_contents); | |
| 174 if (!ppd_file) { | |
| 175 // Failed to store. | |
| 176 done_callback_.Run(PpdProvider::INTERNAL_ERROR, FilePath()); | |
| 177 return; | |
| 178 } | |
| 179 done_callback_.Run(PpdProvider::SUCCESS, ppd_file.value()); | |
| 180 } | |
| 181 | |
| 182 private: | |
| 183 // Generate a url to look up a manufacturer/model from the quirks server | |
| 184 GURL GetQuirksServerLookupURL( | |
| 185 const Printer::PpdReference& ppd_reference) const { | |
| 186 return GURL(::base::JoinString( | |
| 187 { | |
| 188 "https://", // | |
|
Lei Zhang
2016/10/17 17:54:07
Unneeded comments?
Carlson
2016/10/18 19:05:01
clang-format line breaks.
| |
| 189 options_.quirks_server, // | |
| 190 "/v2/printer/manufacturers/", // | |
| 191 ppd_reference.effective_manufacturer, // | |
| 192 "/models/", // | |
| 193 ppd_reference.effective_model, // | |
| 194 "?key=", // | |
| 195 api_key_ // | |
| 196 }, | |
| 197 "")); | |
| 198 } | |
| 199 | |
| 200 // API key for accessing quirks server. | |
| 201 const string api_key_; | |
| 202 | |
| 203 // Reference we're currently trying to resolve. | |
| 204 Printer::PpdReference active_reference_; | |
| 205 | |
| 206 ForwardingURLFetcherDelegate forwarding_delegate_; | |
| 207 scoped_refptr<net::URLRequestContextGetter> url_context_getter_; | |
| 208 unique_ptr<PpdCache> cache_; | |
| 209 | |
| 210 PpdProvider::ResolveCallback done_callback_; | |
| 211 | |
| 212 // Fetcher for the current resolve call, if any. | |
| 213 unique_ptr<::net::URLFetcher> fetcher_; | |
| 214 | |
| 215 // Construction-time options, immutable. | |
| 216 const PpdProvider::Options options_; | |
| 217 }; | |
| 218 | |
| 219 void ForwardingURLFetcherDelegate::OnURLFetchComplete( | |
| 220 const URLFetcher* source) { | |
| 221 owner_->OnURLFetchComplete(); | |
| 222 } | |
| 223 | |
| 224 } // namespace | |
| 225 | |
| 226 // static | |
| 227 unique_ptr<PpdProvider> PpdProvider::Create( | |
| 228 const string& api_key, | |
| 229 scoped_refptr<net::URLRequestContextGetter> url_context_getter, | |
| 230 unique_ptr<PpdCache> cache, | |
| 231 const PpdProvider::Options& options) { | |
| 232 return ::base::MakeUnique<PpdProviderImpl>(api_key, url_context_getter, | |
|
Lei Zhang
2016/10/17 17:54:07
Most code just write base:Foo. Is there another ba
Carlson
2016/10/18 19:05:01
Done.
| |
| 233 std::move(cache), options); | |
| 234 } | |
| 235 | |
| 236 } // namespace printing | |
| 237 } // namespace chromeos | |
| OLD | NEW |