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