glevin | 5dd01a7 | 2016-03-23 23:08:12 | [diff] [blame] | 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 | |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 8 | #include <memory> |
glevin | 5dd01a7 | 2016-03-23 23:08:12 | [diff] [blame] | 9 | #include <set> |
| 10 | |
| 11 | #include "base/callback.h" |
| 12 | #include "base/files/file_path.h" |
| 13 | #include "base/macros.h" |
glevin | 5dd01a7 | 2016-03-23 23:08:12 | [diff] [blame] | 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 | // Callback when Quirks path request is complete. |
| 38 | // First parameter - path found, or empty if no file. |
| 39 | // Second parameter - true if file was just downloaded. |
| 40 | using RequestFinishedCallback = |
| 41 | base::Callback<void(const base::FilePath&, bool)>; |
| 42 | |
| 43 | // Format int as hex string for filename. |
| 44 | QUIRKS_EXPORT std::string IdToHexString(int64_t product_id); |
| 45 | |
| 46 | // Append ".icc" to hex string in filename. |
| 47 | QUIRKS_EXPORT std::string IdToFileName(int64_t product_id); |
| 48 | |
| 49 | // Manages downloads of and requests for hardware calibration and configuration |
| 50 | // files ("Quirks"). The manager presents an external Quirks API, handles |
| 51 | // needed components from browser (local preferences, url context getter, |
| 52 | // blocking pool, etc), and owns clients and manages their life cycles. |
| 53 | class QUIRKS_EXPORT QuirksManager { |
| 54 | public: |
| 55 | // Passed function to create a URLFetcher for tests. |
| 56 | // Same parameters as URLFetcher::Create(). |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 57 | using FakeQuirksFetcherCreator = base::Callback< |
| 58 | std::unique_ptr<net::URLFetcher>(const GURL&, net::URLFetcherDelegate*)>; |
glevin | 5dd01a7 | 2016-03-23 23:08:12 | [diff] [blame] | 59 | |
| 60 | // Callback after getting days since OOBE on blocking pool. |
| 61 | // Parameter is returned number of days. |
| 62 | using DaysSinceOobeCallback = base::Callback<void(int)>; |
| 63 | |
| 64 | // Delegate class, so implementation can access browser functionality. |
| 65 | class Delegate { |
| 66 | public: |
| 67 | virtual ~Delegate() = default; |
| 68 | |
| 69 | // Provides Chrome API key for quirks server. |
| 70 | virtual std::string GetApiKey() const = 0; |
| 71 | |
| 72 | // Returns the read-only directory where icc files were added before the |
| 73 | // Quirks Client provided them. |
| 74 | virtual base::FilePath GetBuiltInDisplayProfileDirectory() const = 0; |
| 75 | |
| 76 | // Returns the path to the writable display profile directory. |
| 77 | // This directory must already exist. |
| 78 | virtual base::FilePath GetDownloadDisplayProfileDirectory() const = 0; |
| 79 | |
glevin | 5b821b5 | 2016-04-04 16:42:47 | [diff] [blame] | 80 | // Whether downloads are allowed by enterprise device policy. |
| 81 | virtual bool DevicePolicyEnabled() const = 0; |
| 82 | |
glevin | 5dd01a7 | 2016-03-23 23:08:12 | [diff] [blame] | 83 | // Gets days since first login, returned via callback. |
| 84 | virtual void GetDaysSinceOobe(DaysSinceOobeCallback callback) const = 0; |
| 85 | |
| 86 | private: |
| 87 | DISALLOW_ASSIGN(Delegate); |
| 88 | }; |
| 89 | |
| 90 | static void Initialize( |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 91 | std::unique_ptr<Delegate> delegate, |
glevin | 5dd01a7 | 2016-03-23 23:08:12 | [diff] [blame] | 92 | scoped_refptr<base::SequencedWorkerPool> blocking_pool, |
| 93 | PrefService* local_state, |
| 94 | scoped_refptr<net::URLRequestContextGetter> url_context_getter); |
| 95 | static void Shutdown(); |
| 96 | static QuirksManager* Get(); |
| 97 | |
| 98 | static void RegisterPrefs(PrefRegistrySimple* registry); |
| 99 | |
| 100 | // Signal to start queued downloads after login. |
| 101 | void OnLoginCompleted(); |
| 102 | |
| 103 | // Entry point into manager. Finds or downloads icc file. |
| 104 | void RequestIccProfilePath( |
| 105 | int64_t product_id, |
| 106 | const RequestFinishedCallback& on_request_finished); |
| 107 | |
| 108 | void ClientFinished(QuirksClient* client); |
| 109 | |
| 110 | // Creates a real URLFetcher for OS, and a fake one for tests. |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 111 | std::unique_ptr<net::URLFetcher> CreateURLFetcher( |
glevin | 5dd01a7 | 2016-03-23 23:08:12 | [diff] [blame] | 112 | const GURL& url, |
| 113 | net::URLFetcherDelegate* delegate); |
| 114 | |
| 115 | Delegate* delegate() { return delegate_.get(); } |
| 116 | base::SequencedWorkerPool* blocking_pool() { return blocking_pool_.get(); } |
| 117 | net::URLRequestContextGetter* url_context_getter() { |
| 118 | return url_context_getter_.get(); |
| 119 | } |
| 120 | |
| 121 | protected: |
| 122 | friend class QuirksBrowserTest; |
| 123 | |
| 124 | void SetFakeQuirksFetcherCreatorForTests( |
| 125 | const FakeQuirksFetcherCreator& creator) { |
| 126 | fake_quirks_fetcher_creator_ = creator; |
| 127 | } |
| 128 | |
| 129 | private: |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 130 | QuirksManager(std::unique_ptr<Delegate> delegate, |
glevin | 5dd01a7 | 2016-03-23 23:08:12 | [diff] [blame] | 131 | scoped_refptr<base::SequencedWorkerPool> blocking_pool, |
| 132 | PrefService* local_state, |
| 133 | scoped_refptr<net::URLRequestContextGetter> url_context_getter); |
| 134 | ~QuirksManager(); |
| 135 | |
| 136 | // Callback after checking for existing icc file; proceed if not found. |
| 137 | void OnIccFilePathRequestCompleted( |
| 138 | int64_t product_id, |
| 139 | const RequestFinishedCallback& on_request_finished, |
| 140 | base::FilePath path); |
| 141 | |
| 142 | // Callback after checking OOBE date; launch client if appropriate. |
| 143 | void OnDaysSinceOobeReceived( |
| 144 | int64_t product_id, |
| 145 | const RequestFinishedCallback& on_request_finished, |
| 146 | int days_since_oobe); |
| 147 | |
| 148 | // Create and start a client to download file. |
| 149 | void CreateClient(int64_t product_id, |
| 150 | const RequestFinishedCallback& on_request_finished); |
| 151 | |
glevin | 5b821b5 | 2016-04-04 16:42:47 | [diff] [blame] | 152 | // Whether downloads allowed by cmd line flag and device policy. |
| 153 | bool QuirksEnabled(); |
| 154 | |
glevin | 5dd01a7 | 2016-03-23 23:08:12 | [diff] [blame] | 155 | // Records time of most recent server check. |
| 156 | void SetLastServerCheck(int64_t product_id, const base::Time& last_check); |
| 157 | |
| 158 | // Set of active clients, each created to download a different Quirks file. |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 159 | std::set<std::unique_ptr<QuirksClient>> clients_; |
glevin | 5dd01a7 | 2016-03-23 23:08:12 | [diff] [blame] | 160 | |
| 161 | // Don't start downloads before first session login. |
| 162 | bool waiting_for_login_; |
| 163 | |
| 164 | // Ensure this class runs on a single thread. |
| 165 | base::ThreadChecker thread_checker_; |
| 166 | |
| 167 | // These objects provide resources from the browser. |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 168 | std::unique_ptr<Delegate> delegate_; // Impl runs from chrome/browser. |
glevin | 5dd01a7 | 2016-03-23 23:08:12 | [diff] [blame] | 169 | scoped_refptr<base::SequencedWorkerPool> blocking_pool_; |
| 170 | PrefService* local_state_; // For local prefs. |
| 171 | scoped_refptr<net::URLRequestContextGetter> url_context_getter_; |
| 172 | |
| 173 | FakeQuirksFetcherCreator fake_quirks_fetcher_creator_; // For tests. |
| 174 | |
| 175 | // Factory for callbacks. |
| 176 | base::WeakPtrFactory<QuirksManager> weak_ptr_factory_; |
| 177 | |
| 178 | DISALLOW_COPY_AND_ASSIGN(QuirksManager); |
| 179 | }; |
| 180 | |
| 181 | } // namespace quirks |
| 182 | |
| 183 | #endif // COMPONENTS_QUIRKS_QUIRKS_MANAGER_H_ |