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 | |
| 5 #ifndef COMPONENTS_QUIRKS_QUIRKS_MANAGER_H_ | |
| 6 #define COMPONENTS_QUIRKS_QUIRKS_MANAGER_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/threading/thread_checker.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "components/quirks/quirks_export.h" | |
| 18 | |
| 19 class GURL; | |
| 20 class PrefRegistrySimple; | |
| 21 class PrefService; | |
| 22 | |
| 23 namespace base { | |
| 24 class SequencedWorkerPool; | |
| 25 } | |
| 26 | |
| 27 namespace net { | |
| 28 class URLFetcher; | |
| 29 class URLFetcherDelegate; | |
| 30 class URLRequestContextGetter; | |
| 31 } | |
| 32 | |
| 33 namespace quirks { | |
| 34 | |
| 35 class QuirksClient; | |
| 36 | |
| 37 using RequestFinishedCallback = | |
| 38 base::Callback<void(const base::FilePath&, bool)>; | |
| 39 | |
| 40 // Format int as hex string for filename. | |
| 41 QUIRKS_EXPORT std::string IdToHexString(int64_t product_id); | |
| 42 | |
| 43 // Append ".icc" to hex string in filename. | |
| 44 QUIRKS_EXPORT std::string IdToFileName(int64_t product_id); | |
| 45 | |
| 46 // Manages downloads of and requests for hardware calibration and configuration | |
| 47 // files ("Quirks"). The manager presents an external Quirks API, handles | |
| 48 // needed components from browser (local preferences, url context getter, | |
| 49 // blocking pool, etc), and owns clients and manages their life cycles. | |
| 50 class QUIRKS_EXPORT QuirksManager { | |
| 51 public: | |
| 52 using FakeQuirksFetcherCreator = | |
| 53 base::Callback<scoped_ptr<net::URLFetcher>(const GURL&, | |
| 54 net::URLFetcherDelegate*)>; | |
| 55 | |
| 56 using DaysSinceOobeCallback = base::Callback<void(int)>; | |
| 57 | |
| 58 // Delegate class, so implementation can access browser functionality. | |
| 59 class Delegate { | |
| 60 public: | |
| 61 virtual ~Delegate() = default; | |
| 62 | |
| 63 // Provides Chrome API key for quirks server. | |
| 64 virtual std::string GetApiKey() const = 0; | |
| 65 | |
| 66 // Returns the read-only directory where icc files were added before the | |
| 67 // Quirks Client provided them. | |
| 68 virtual base::FilePath GetBuiltInDisplayProfileDirectory() const = 0; | |
| 69 | |
| 70 // Returns the path to the writable display profile directory. | |
| 71 // This directory must already exist. | |
| 72 virtual base::FilePath GetDownloadDisplayProfileDirectory() const = 0; | |
| 73 | |
| 74 // Gets days since first login, returned via callback. | |
| 75 virtual void GetDaysSinceOobe(DaysSinceOobeCallback callback) const = 0; | |
| 76 | |
| 77 private: | |
| 78 DISALLOW_ASSIGN(Delegate); | |
|
oshima
2016/03/22 20:27:21
FYI: you can omit this for pure interface
Greg Levin
2016/03/23 00:16:18
Acknowledged :-)
https://codereview.chromium.org/
| |
| 79 }; | |
| 80 | |
| 81 static void Initialize( | |
| 82 scoped_ptr<Delegate> delegate, | |
| 83 scoped_refptr<base::SequencedWorkerPool> blocking_pool, | |
| 84 PrefService* local_state, | |
| 85 scoped_refptr<net::URLRequestContextGetter> url_context_getter); | |
| 86 static void Shutdown(); | |
| 87 static QuirksManager* Get(); | |
| 88 | |
| 89 static void RegisterPrefs(PrefRegistrySimple* registry); | |
| 90 | |
| 91 // Signal to start queued downloads after login. | |
| 92 void OnLoginCompleted(); | |
| 93 | |
| 94 // Entry point into manager. Finds or downloads icc file. | |
| 95 void RequestIccProfilePath( | |
| 96 int64_t product_id, | |
| 97 const RequestFinishedCallback& on_request_finished); | |
| 98 | |
| 99 void ClientFinished(QuirksClient* client); | |
| 100 | |
| 101 // Creates a real URLFetcher for OS, and a fake one for tests. | |
| 102 scoped_ptr<net::URLFetcher> CreateURLFetcher( | |
| 103 const GURL& url, | |
| 104 net::URLFetcherDelegate* delegate); | |
| 105 | |
| 106 Delegate* delegate() const { return delegate_.get(); } | |
|
oshima
2016/03/22 20:27:21
remove const, as you're returning non const intern
Greg Levin
2016/03/23 00:16:18
Done.
| |
| 107 base::SequencedWorkerPool* blocking_pool() const { | |
| 108 return blocking_pool_.get(); | |
| 109 } | |
| 110 | |
| 111 net::URLRequestContextGetter* url_context_getter() const { | |
| 112 return url_context_getter_.get(); | |
| 113 } | |
| 114 | |
| 115 protected: | |
| 116 friend class QuirksBrowserTest; | |
| 117 | |
| 118 void SetFakeQuirksFetcherCreatorForTests( | |
| 119 const FakeQuirksFetcherCreator& creator) { | |
| 120 fake_quirks_fetcher_creator_ = creator; | |
| 121 } | |
| 122 | |
| 123 private: | |
| 124 QuirksManager(scoped_ptr<Delegate> delegate, | |
| 125 scoped_refptr<base::SequencedWorkerPool> blocking_pool, | |
| 126 PrefService* local_state, | |
| 127 scoped_refptr<net::URLRequestContextGetter> url_context_getter); | |
| 128 ~QuirksManager(); | |
| 129 | |
| 130 // Callback after checking for existing icc file; proceed if not found. | |
| 131 void OnIccFilePathRequestCompleted( | |
| 132 int64_t product_id, | |
| 133 const RequestFinishedCallback& on_request_finished, | |
| 134 base::FilePath path); | |
| 135 | |
| 136 // Callback after checking OOBE date; launch client if appropriate. | |
| 137 void OnDaysSinceOobeReceived( | |
| 138 int64_t product_id, | |
| 139 const RequestFinishedCallback& on_request_finished, | |
| 140 int days_since_oobe); | |
| 141 | |
| 142 // Create and start a client to download file. | |
| 143 void CreateClient(int64_t product_id, | |
| 144 const RequestFinishedCallback& on_request_finished); | |
| 145 | |
| 146 // Records time of most recent server check. | |
| 147 void SetLastServerCheck(int64_t product_id, const base::Time& last_check); | |
| 148 | |
| 149 // Set of active clients, each created to download a different Quirks file. | |
| 150 std::set<scoped_ptr<QuirksClient>> clients_; | |
| 151 | |
| 152 // Don't start downloads before first session login. | |
| 153 bool waiting_for_login_; | |
| 154 | |
| 155 // The class is expected to run on UI thread. | |
| 156 base::ThreadChecker thread_checker_; | |
| 157 | |
| 158 // These objects provide resources from the browser. | |
| 159 scoped_ptr<Delegate> delegate_; // Impl runs from chrome/browser. | |
| 160 scoped_refptr<base::SequencedWorkerPool> blocking_pool_; | |
| 161 PrefService* local_state_; // For local prefs. | |
| 162 scoped_refptr<net::URLRequestContextGetter> url_context_getter_; | |
| 163 | |
| 164 FakeQuirksFetcherCreator fake_quirks_fetcher_creator_; // For tests. | |
| 165 | |
| 166 // Factory for callbacks. | |
| 167 base::WeakPtrFactory<QuirksManager> weak_ptr_factory_; | |
| 168 | |
| 169 DISALLOW_COPY_AND_ASSIGN(QuirksManager); | |
| 170 }; | |
| 171 | |
| 172 } // namespace quirks | |
| 173 | |
| 174 #endif // COMPONENTS_QUIRKS_QUIRKS_MANAGER_H_ | |
| OLD | NEW |