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

Unified Diff: chromeos/printing/ppd_provider.cc

Issue 2343983004: Add PPDProvider barebones implementation and associated cache skeleton. (Closed)
Patch Set: Initial PPDProvider/PPDCache implementation. Also, add associated unittests. This doesn't plumb th… Created 4 years, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: chromeos/printing/ppd_provider.cc
diff --git a/chromeos/printing/ppd_provider.cc b/chromeos/printing/ppd_provider.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a3fae9ec8cb90da4e4bcc34bf31f7acf26d85653
--- /dev/null
+++ b/chromeos/printing/ppd_provider.cc
@@ -0,0 +1,232 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chromeos/printing/ppd_provider.h"
+
+#include "base/files/file_util.h"
+#include "base/json/json_parser.h"
+#include "base/memory/ptr_util.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_util.h"
+#include "base/time/time.h"
+#include "base/values.h"
+#include "chromeos/printing/ppd_cache.h"
+#include "net/base/load_flags.h"
+#include "net/http/http_status_code.h"
+#include "net/url_request/url_fetcher.h"
+#include "net/url_request/url_fetcher_delegate.h"
+#include "net/url_request/url_request_context_getter.h"
+#include "url/gurl.h"
+
+using ::base::DictionaryValue;
+using ::base::FilePath;
+using ::base::Optional;
+using ::base::Value;
+using ::net::URLFetcher;
+using ::net::URLFetcherDelegate;
+using ::std::string;
+using ::std::unique_ptr;
+
+namespace chromeos {
+namespace printing {
+namespace {
+
+// Expected fields from the quirks server.
+const char* kJSONPPDKey = "compressedPpd";
+const char* kJSONLastUpdatedKey = "lastUpdatedTime";
+
+class PPDProviderImpl;
+
+// URLFetcherDelegate that just forwards the complete callback back to
+// the PPDProvider that owns the delegate.
+class ForwardingURLFetcherDelegate : public URLFetcherDelegate {
+ public:
+ ForwardingURLFetcherDelegate(PPDProviderImpl* owner) : owner_(owner) {}
+ ~ForwardingURLFetcherDelegate() override {}
+
+ // URLFetcherDelegate API method. Defined below since we need the
+ // PPDProviderImpl definition first.
+ void OnURLFetchComplete(const URLFetcher* source) override;
+
+ private:
+ // owner of this delegate.
+ PPDProviderImpl* owner_;
+};
+
+class PPDProviderImpl : public PPDProvider {
+ public:
+ PPDProviderImpl(
+ const string& api_key,
+ scoped_refptr<net::URLRequestContextGetter> url_context_getter,
+ unique_ptr<PPDCache> cache,
+ const PPDProvider::Options& options)
+ : api_key_(api_key),
+ forwarding_delegate_(this),
+ url_context_getter_(url_context_getter),
+ cache_(std::move(cache)),
+ options_(options) {}
+ ~PPDProviderImpl() override {}
+
+ void Resolve(const string& manufacturer,
+ const string& model,
+ PPDProvider::ResolveCallback cb) override {
+ auto tmp = cache_->Find(manufacturer, model);
+ if (tmp) {
+ // Cache hit. Schedule the callback right now.
+ url_context_getter_->GetNetworkTaskRunner()->PostTask(
+ FROM_HERE, base::Bind(cb, PPDProvider::SUCCESS, tmp.value()));
+ return;
+ }
+
+ manufacturer_ = manufacturer;
+ model_ = model;
+ done_callback_ = cb;
+ fetcher_ = net::URLFetcher::Create(GetLookupURL(manufacturer, model),
+ URLFetcher::GET, &forwarding_delegate_);
+ fetcher_->SetRequestContext(url_context_getter_.get());
+ fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE |
+ net::LOAD_DO_NOT_SAVE_COOKIES |
+ net::LOAD_DO_NOT_SEND_COOKIES |
+ net::LOAD_DO_NOT_SEND_AUTH_DATA);
+ fetcher_->Start();
+ };
+
+ bool StoreLocal(const std::string& printer_id,
+ base::FilePath ppd_file) override {
+ string contents;
+ // TODO(justincarlson) - Resolve what we're doing around compression. We
+ // may need to compress the file here.
+ if (!ReadFileToString(ppd_file, &contents)) {
+ LOG(ERROR) << "Failed to read ppd file " << ppd_file.LossyDisplayName();
+ return false;
+ }
+ return static_cast<bool>(cache_->StoreLocal(printer_id, contents));
+ }
+
+ void ResolveLocal(const string& printer_id,
+ PPDProvider::ResolveCallback cb) override {
+ auto tmp = cache_->FindLocal(printer_id);
skau 2016/10/07 16:29:06 Can you write out this type? It's not clear that
Carlson 2016/10/14 19:28:56 Done.
+ if (tmp) {
+ // Found it.
+ url_context_getter_->GetNetworkTaskRunner()->PostTask(
+ FROM_HERE, base::Bind(cb, PPDProvider::SUCCESS, tmp.value()));
+ } else {
+ // Doesn't exist. No further resolution should be attempted since
+ // it was originally a local addition.
+ url_context_getter_->GetNetworkTaskRunner()->PostTask(
+ FROM_HERE, base::Bind(cb, PPDProvider::NOT_FOUND, FilePath()));
+ }
+ }
+
+ // Called on the network thread when the fetcher completes its fetch.
+ void OnURLFetchComplete() {
+ // TODO(justincarlson) -- What does URLFetcher do with redirects? Does it
+ // automatically resolve them, or do we need to do something else?
+ string contents;
+ if ((fetcher_->GetStatus().status() != net::URLRequestStatus::SUCCESS) ||
+ (fetcher_->GetResponseCode() != net::HTTP_OK) ||
+ !fetcher_->GetResponseAsString(&contents)) {
+ // Something went wrong with the fetch.
+ done_callback_.Run(PPDProvider::SERVER_ERROR, FilePath());
+ return;
+ }
+ auto parsed_json = ::base::JSONReader::Read(contents);
+ DictionaryValue* dict;
+ if (parsed_json == nullptr || //
skau 2016/10/07 16:29:06 are you forcing it to wrap with the comment //?
Carlson 2016/10/14 19:28:56 Yes. I think it's easier to read this way, and AF
+ !parsed_json->GetAsDictionary(&dict)) {
+ // Malformed response. TODO(justincarlson) - LOG something here?
+ done_callback_.Run(PPDProvider::SERVER_ERROR, FilePath());
+ return;
+ }
+ string compressed_ppd_contents;
+ string last_updated_time_string;
+ uint64_t last_updated_time;
+ if (!(dict->GetString(kJSONPPDKey, &compressed_ppd_contents) &&
+ dict->GetString(kJSONLastUpdatedKey, &last_updated_time_string) &&
+ ::base::StringToUint64(last_updated_time_string,
+ &last_updated_time))) {
+ // Malformed response. TODO(justincarlson) - LOG something here?
+ done_callback_.Run(PPDProvider::SERVER_ERROR, FilePath());
+ return;
+ }
+ auto ppd_file =
+ cache_->Store(manufacturer_, model_, compressed_ppd_contents);
+ if (!ppd_file) {
+ // Failed to store.
+ done_callback_.Run(PPDProvider::INTERNAL_ERROR, FilePath());
+ return;
+ }
+ done_callback_.Run(PPDProvider::SUCCESS, ppd_file.value());
+ }
+
+ private:
+ // Generate a url to look up a manufacturer/model from the quirks server
+ GURL GetLookupURL(const string& manufacturer, const string& model) const {
+ return GURL(::base::JoinString(
+ {
+ "https://", //
+ options_.quirks_server, //
+ "/v2/printer/manufacturers/", //
+ manufacturer, //
+ "/models/", //
+ model, //
+ "?key=", //
+ api_key_ //
+ },
+ ""));
+ }
+
+ // API key for accessing quirks server.
+ const string api_key_;
+
+ // Manufacturer/model strings for the current lookup.
+ string manufacturer_;
+ string model_;
+
+ ForwardingURLFetcherDelegate forwarding_delegate_;
+ scoped_refptr<net::URLRequestContextGetter> url_context_getter_;
+ unique_ptr<PPDCache> cache_;
+
+ PPDProvider::ResolveCallback done_callback_;
+
+ // Construction-time options, immutable.
+ const PPDProvider::Options options_;
+ unique_ptr<::net::URLFetcher> fetcher_;
+};
+
+void ForwardingURLFetcherDelegate::OnURLFetchComplete(
+ const URLFetcher* source) {
+ owner_->OnURLFetchComplete();
+}
+
+} // namespace
+
+// static
+PPDProvider::Options PPDProvider::Defaults() {
+ PPDProvider::Options ret;
+ ret.quirks_server = "chromeosquirksserver-pa.googleapis.com";
+ return ret;
+}
+
+// static
+unique_ptr<PPDProvider> PPDProvider::Create(
+ const string& api_key,
+ scoped_refptr<net::URLRequestContextGetter> url_context_getter,
+ unique_ptr<PPDCache> cache,
+ const PPDProvider::Options& options) {
+ return ::base::MakeUnique<PPDProviderImpl>(api_key, url_context_getter,
+ std::move(cache), options);
+}
+
+// static
+unique_ptr<PPDProvider> PPDProvider::Create(
+ const string& api_key,
+ scoped_refptr<net::URLRequestContextGetter> url_context_getter,
+ unique_ptr<PPDCache> cache) {
+ return ::base::MakeUnique<PPDProviderImpl>(api_key, url_context_getter,
+ std::move(cache), Defaults());
+}
+
+} // namespace printing
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698