| 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/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/time/time.h" |
| 15 #include "components/quirks/quirks_client.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 class SingleThreadTaskRunner; |
| 25 } |
| 26 |
| 27 namespace net { |
| 28 class URLFetcher; |
| 29 class URLFetcherDelegate; |
| 30 class URLRequestContextGetter; |
| 31 } |
| 32 |
| 33 namespace quirks { |
| 34 |
| 35 // Format int as hex string for filename. |
| 36 QUIRKS_EXPORT std::string IdToHexString(int64_t product_id); |
| 37 |
| 38 // Append ".icc" to hex string in filename. |
| 39 QUIRKS_EXPORT std::string IdToFileName(int64_t product_id); |
| 40 |
| 41 // Finds icc file, if it exists; creates and starts QuirksClient if it doesn't. |
| 42 // In all cases, sends results to provided callback. |
| 43 QUIRKS_EXPORT void RequestIccProfilePath( |
| 44 int64_t product_id, |
| 45 const RequestFinishedCallback& on_request_finished); |
| 46 |
| 47 // Handles needed components from browser (local preferences, url context |
| 48 // getter, blocking pool, etc). |
| 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, returns via callback. |
| 74 virtual void GetDaysSinceOobe( |
| 75 scoped_refptr<base::SequencedWorkerPool> blocking_pool, |
| 76 DaysSinceOobeCallback callback) const = 0; |
| 77 |
| 78 private: |
| 79 DISALLOW_ASSIGN(Delegate); |
| 80 }; |
| 81 |
| 82 static void Initialize( |
| 83 scoped_ptr<Delegate> delegate, |
| 84 scoped_refptr<base::SequencedWorkerPool> blocking_pool, |
| 85 PrefService* local_state, |
| 86 scoped_refptr<net::URLRequestContextGetter> url_context_getter); |
| 87 static void Shutdown(); |
| 88 static QuirksManager* Get(); |
| 89 |
| 90 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 91 |
| 92 // Entry point into manager. Finds or downloads icc file. |
| 93 void RequestIccFilePath(int64_t product_id, |
| 94 const RequestFinishedCallback& on_request_finished); |
| 95 |
| 96 void ClientFinished(QuirksClient* client); |
| 97 |
| 98 // UMA stats for server check reason and results. |
| 99 void RecordReasonUmaStat(QuirksClient::RequestReason reason); |
| 100 void RecordFileFoundUmaStat(bool success); |
| 101 |
| 102 // Creates a real URLFetcher for OS, and a fake one for tests. |
| 103 scoped_ptr<net::URLFetcher> CreateURLFetcher( |
| 104 const std::string& url, |
| 105 net::URLFetcherDelegate* delegate); |
| 106 |
| 107 Delegate* delegate() const { return delegate_.get(); } |
| 108 base::SequencedWorkerPool* blocking_pool() const { |
| 109 return blocking_pool_.get(); |
| 110 } |
| 111 |
| 112 net::URLRequestContextGetter* url_context_getter() const { |
| 113 return url_context_getter_.get(); |
| 114 } |
| 115 |
| 116 protected: |
| 117 friend class QuirksBrowserTest; |
| 118 |
| 119 void SetFakeQuirksFetcherCreatorForTests( |
| 120 const FakeQuirksFetcherCreator& creator) { |
| 121 fake_quirks_fetcher_creator_ = creator; |
| 122 } |
| 123 |
| 124 private: |
| 125 QuirksManager(Delegate* delegate, |
| 126 scoped_refptr<base::SequencedWorkerPool> blocking_pool, |
| 127 PrefService* local_state, |
| 128 scoped_refptr<net::URLRequestContextGetter> url_context_getter); |
| 129 ~QuirksManager(); |
| 130 |
| 131 // Callback after checking for existing icc file; proceed if not found. |
| 132 void OnIccFilePathRequestCompleted( |
| 133 int64_t product_id, |
| 134 const RequestFinishedCallback& on_request_finished, |
| 135 base::FilePath path); |
| 136 |
| 137 // Callback after checking OOBE date; launch client if appropriate. |
| 138 void OnDaysSinceOobeGotten(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 RunClient(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 std::set<scoped_ptr<QuirksClient>> clients_; |
| 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 |