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

Side by Side Diff: components/doodle/doodle_fetcher.h

Issue 2707293002: [Doodle] Split DoodleFetcher into interface + Impl (Closed)
Patch Set: Created 3 years, 10 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 unified diff | Download patch
« no previous file with comments | « components/doodle/BUILD.gn ('k') | components/doodle/doodle_fetcher.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 #ifndef COMPONENTS_DOODLE_DOODLE_FETCHER_H_ 5 #ifndef COMPONENTS_DOODLE_DOODLE_FETCHER_H_
6 #define COMPONENTS_DOODLE_DOODLE_FETCHER_H_ 6 #define COMPONENTS_DOODLE_DOODLE_FETCHER_H_
7 7
8 #include <memory> 8 #include "base/callback_forward.h"
fhorschig 2017/02/22 14:57:18 Nice, didn't know that was possible!
9 #include <string>
10 #include <utility>
11 #include <vector>
12
13 #include "base/callback.h"
14 #include "base/macros.h" 9 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/optional.h" 10 #include "base/optional.h"
18 #include "components/doodle/doodle_types.h" 11 #include "components/doodle/doodle_types.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 class GoogleURLTracker;
24
25 namespace base {
26 class Clock;
27 class DictionaryValue;
28 class Value;
29 }
30 12
31 namespace doodle { 13 namespace doodle {
32 14
33 // This class provides information about any recent doodle. 15 // Interface for fetching the current doodle from the network.
34 // It works asynchronously and calls a callback when finished fetching the 16 // It asynchronously calls a callback when fetching the doodle information from
35 // information from the remote enpoint. 17 // the remote enpoint finishes.
36 class DoodleFetcher : public net::URLFetcherDelegate { 18 // DoodleFetcherImpl is the default implementation; this interface exists to
19 // make it easy to use fakes or mocks in tests.
20 class DoodleFetcher {
37 public: 21 public:
38 // Callback that is invoked when the fetching is done. 22 // Callback that is invoked when the fetching is done.
39 // |doodle_config| will only contain a value if |state| is AVAILABLE. 23 // |doodle_config| will only contain a value if |state| is AVAILABLE.
40 using FinishedCallback = base::OnceCallback<void( 24 using FinishedCallback = base::OnceCallback<void(
41 DoodleState state, 25 DoodleState state,
42 const base::Optional<DoodleConfig>& doodle_config)>; 26 const base::Optional<DoodleConfig>& doodle_config)>;
43 // Callback for JSON parsing to allow injecting platform-dependent code.
44 using ParseJSONCallback = base::Callback<void(
45 const std::string& unsafe_json,
46 const base::Callback<void(std::unique_ptr<base::Value> json)>& success,
47 const base::Callback<void(const std::string&)>& error)>;
48 27
49 DoodleFetcher(scoped_refptr<net::URLRequestContextGetter> download_context, 28 DoodleFetcher() = default;
50 GoogleURLTracker* google_url_tracker, 29 virtual ~DoodleFetcher() = default;
51 const ParseJSONCallback& json_parsing_callback);
52 ~DoodleFetcher() override;
53 30
54 // Fetches a doodle asynchronously. The |callback| is called with a 31 // Fetches a doodle asynchronously. The |callback| is called with a
55 // DoodleState indicating whether the request succeded in fetching a doodle. 32 // DoodleState indicating whether the request succeded in fetching a doodle.
56 // If a fetch is already running, the callback will be queued and invoked with 33 // If a fetch is already running, the callback will be queued and invoked with
57 // result from the next completed request. 34 // result from the next completed request.
58 void FetchDoodle(FinishedCallback callback); 35 virtual void FetchDoodle(FinishedCallback callback) = 0;
59
60 // Overrides internal clock for testing purposes.
61 void SetClockForTesting(std::unique_ptr<base::Clock> clock) {
62 clock_ = std::move(clock);
63 }
64 36
65 private: 37 private:
66 // net::URLFetcherDelegate implementation.
67 void OnURLFetchComplete(const net::URLFetcher* source) override;
68
69 // ParseJSONCallback Success callback
70 void OnJsonParsed(std::unique_ptr<base::Value> json);
71 // ParseJSONCallback Failure callback
72 void OnJsonParseFailed(const std::string& error_message);
73
74 base::Optional<DoodleConfig> ParseDoodle(
75 const base::DictionaryValue& ddljson) const;
76 bool ParseImage(const base::DictionaryValue& image_dict,
77 const std::string& image_name,
78 DoodleImage* image) const;
79 void ParseBaseInformation(const base::DictionaryValue& ddljson,
80 DoodleConfig* config) const;
81 GURL ParseRelativeUrl(const base::DictionaryValue& dict_value,
82 const std::string& key) const;
83
84 void RespondToAllCallbacks(DoodleState state,
85 const base::Optional<DoodleConfig>& config);
86 GURL GetGoogleBaseUrl() const;
87
88 // Returns whether a fetch is still in progress. A fetch begins when a
89 // callback is added and ends when the last callback was called.
90 bool IsFetchInProgress() const { return !callbacks_.empty(); }
91
92 // Parameters set from constructor.
93 scoped_refptr<net::URLRequestContextGetter> const download_context_;
94 ParseJSONCallback json_parsing_callback_;
95 GoogleURLTracker* google_url_tracker_;
96
97 // Allow for an injectable tick clock for testing.
98 std::unique_ptr<base::Clock> clock_;
99
100 std::vector<FinishedCallback> callbacks_;
101 std::unique_ptr<net::URLFetcher> fetcher_;
102
103 base::WeakPtrFactory<DoodleFetcher> weak_ptr_factory_;
104
105 DISALLOW_COPY_AND_ASSIGN(DoodleFetcher); 38 DISALLOW_COPY_AND_ASSIGN(DoodleFetcher);
sfiera 2017/02/22 17:04:55 Is this normal for abstract classes that can't be
Marc Treib 2017/02/22 17:17:21 Hm, I *think* I've seen it before, but it doesn't
106 }; 39 };
107 40
108 } // namespace doodle 41 } // namespace doodle
109 42
110 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_H_ 43 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_H_
OLDNEW
« no previous file with comments | « components/doodle/BUILD.gn ('k') | components/doodle/doodle_fetcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698