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

Unified Diff: components/doodle/doodle_fetcher.h

Issue 2660883002: Introduce a Doodle Fetcher for NTP (Closed)
Patch Set: Thread-safe multiple callbacks. Weak tests. Created 3 years, 11 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: components/doodle/doodle_fetcher.h
diff --git a/components/doodle/doodle_fetcher.h b/components/doodle/doodle_fetcher.h
new file mode 100644
index 0000000000000000000000000000000000000000..c3125e2f2fb3ee03e446b45d29d46818d2fec491
--- /dev/null
+++ b/components/doodle/doodle_fetcher.h
@@ -0,0 +1,160 @@
+// Copyright 2017 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.
+
+#ifndef COMPONENTS_DOODLE_DOODLE_FETCHER_H_
+#define COMPONENTS_DOODLE_DOODLE_FETCHER_H_
+
+#include <list>
+#include <memory>
+#include <string>
+#include <utility>
+
+#include "base/callback.h"
+#include "base/macros.h"
+#include "base/memory/weak_ptr.h"
+#include "base/synchronization/lock.h"
+#include "net/url_request/url_fetcher_delegate.h"
+#include "url/gurl.h"
+
+namespace base {
+class Clock;
+class DictionaryValue;
+class Value;
+}
+
+namespace net {
+class URLRequestContextGetter;
+}
+
+namespace doodle {
+
+enum class DoodleState {
+ AVAILABLE,
+ NO_DOODLE,
+ DOWNLOAD_ERROR,
+ PARSING_ERROR,
+};
+
+enum class DoodleType {
+ SIMPLE,
+ RANDOM,
+ VIDEO,
+ INTERACTIVE,
+ INLINE_INTERACTIVE,
+ SLIDESHOW,
+};
+
+// Information about a Doodle image. If the image is invalid, the |url| will be
+// empty and invalid. By default, the background color is #ffffff and dimensions
+// are 0.
+struct DoodleImage {
+ DoodleImage();
+ ~DoodleImage();
+ explicit DoodleImage(GURL url);
+ DoodleImage(const DoodleImage& config);
+
+ GURL url;
+ int height;
+ int width;
+ std::string background_color;
+ bool is_animated_gif;
+ bool is_cta;
+};
+
+// All information about a current doodle that can be fetched from the remote
+// end. If no information could be fetched, the reason is stored in |state|.
+// By default, all URLs are empty and therefore invalid.
+struct DoodleConfig {
+ DoodleConfig() = delete;
+ ~DoodleConfig();
+ explicit DoodleConfig(DoodleState doodle_state);
+ DoodleConfig(const DoodleConfig& config);
+
+ DoodleState state;
+ DoodleType doodle_type;
+ std::string alt_text;
+
+ base::Time expiry_date;
+ GURL search_url;
+ GURL target_url;
+ GURL fullpage_interactive_url;
+
+ DoodleImage small_image;
+ DoodleImage medium_image;
+ DoodleImage hires_image;
+ DoodleImage large_image;
+ DoodleImage transparent_large_image;
+};
+
+// This class provides information about any recent doodle, based on the
+// base_url of a user's account.
+// It works asynchronously and calls a callback when finished fetching the
+// information from the remote enpoint. Every instance can only handle one fetch
+// call at a time. Not thread safe.
+class DoodleFetcher : public net::URLFetcherDelegate {
+ public:
+ using FinishedCallback =
+ base::OnceCallback<void(const DoodleConfig& doodle_config)>;
+ using ParseJSONCallback = base::Callback<void(
+ const std::string& unsafe_json,
+ const base::Callback<void(std::unique_ptr<base::Value> json)>& success,
+ const base::Callback<void(const std::string&)>& error)>;
+
+ DoodleFetcher(net::URLRequestContextGetter* download_context, GURL base_url);
+ DoodleFetcher(net::URLRequestContextGetter* download_context,
+ GURL base_url,
+ ParseJSONCallback json_parsing_callback);
+ DoodleFetcher(DoodleFetcher&&);
+ ~DoodleFetcher() override;
+
+ // Fetches a doodle asynchronously. The |callback| is always called and
+ // provides a valid DoodleConfig object.
+ void FetchDoodle(FinishedCallback callback);
+
+ // Overrides internal clock for testing purposes.
+ void SetClockForTesting(std::unique_ptr<base::Clock> clock) {
+ clock_ = std::move(clock);
+ }
+
+ private:
+ // net::URLFetcherDelegate implementation.
+ void OnURLFetchComplete(const net::URLFetcher* source) override;
+
+ void OnJsonParsed(std::unique_ptr<base::Value> json);
+ void OnJsonParseFailed(const std::string& error_message);
+
+ void ParseDoodle(std::unique_ptr<base::DictionaryValue> config);
+ bool ParseImage(const base::DictionaryValue* image_dict,
+ const std::string& image_name,
+ DoodleImage* image) const;
+ void ParseBaseInformation(const base::DictionaryValue* ddljson,
+ DoodleConfig* config) const;
+ void ParseRelativeUrl(const base::DictionaryValue* dict_value,
+ const std::string& key,
+ GURL* url) const;
+
+ void RespondToAllCallbacks(const DoodleConfig& response);
+
+ // Parameter set from constructor.
+ net::URLRequestContextGetter* const download_context_;
+ ParseJSONCallback json_parsing_callback_;
+ GURL base_url_;
+
+ // Allow for an injectable tick clock for testing.
+ std::unique_ptr<base::Clock> clock_;
+
+ std::list<FinishedCallback> callbacks_;
+ base::Lock callback_lock_; // Used to synchronize adding/removing callbacks.
+ std::unique_ptr<net::URLFetcher> fetcher_;
+
+ base::WeakPtrFactory<DoodleFetcher> weak_ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(DoodleFetcher);
+};
+
+extern const char kDoodleConfigUrl[];
+
+} // namespace doodle
+
+#endif // COMPONENTS_DOODLE_DOODLE_FETCHER_H_

Powered by Google App Engine
This is Rietveld 408576698