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/macros.h" | |
| 12 #include "base/memory/ref_counted.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_client.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 class SingleThreadTaskRunner; | |
| 26 } | |
| 27 | |
| 28 namespace net { | |
| 29 class URLFetcher; | |
| 30 class URLFetcherDelegate; | |
| 31 class URLRequestContextGetter; | |
| 32 } | |
| 33 | |
| 34 namespace quirks { | |
| 35 | |
| 36 // Format int as hex string for filename. | |
| 37 QUIRKS_EXPORT std::string IdToHexString(int64_t product_id); | |
| 38 | |
| 39 // Append ".icc" to hex string in filename. | |
| 40 QUIRKS_EXPORT std::string IdToFileName(int64_t product_id); | |
| 41 | |
| 42 // Finds icc file, if it exists; creates and starts QuirksClient if it doesn't. | |
| 43 // In all cases, sends results to provided callback. | |
| 44 QUIRKS_EXPORT void RequestIccProfilePath( | |
| 45 int64_t product_id, | |
| 46 const RequestFinishedCallback& on_request_finished); | |
| 47 | |
| 48 // Handles needed components from browser (local preferences, url context | |
| 49 // getter, blocking pool, etc). | |
| 50 class QUIRKS_EXPORT QuirksManager { | |
| 51 public: | |
| 52 using FakeQuirksFetcherCreator = | |
| 53 base::Callback<scoped_ptr<net::URLFetcher>(const GURL&, | |
| 54 net::URLFetcherDelegate*)>; | |
| 55 | |
| 56 // Delegate class, so implementation can access browser functionality. | |
| 57 class Delegate : public base::RefCounted<Delegate> { | |
|
stevenjb
2016/02/29 20:22:29
Why do we need to refcount the delegate? That's ve
Greg Levin
2016/03/02 00:00:26
CheckForIccFileAndNewDevice() needs the delegate t
Greg Levin
2016/03/07 16:34:38
UPDATE: Delegate no longer passed to CheckForIccFi
| |
| 58 public: | |
|
stevenjb
2016/02/29 20:22:29
Delegate methods should be commented here.
Greg Levin
2016/03/02 00:00:26
Done.
| |
| 59 virtual std::string GetApiKey() const = 0; | |
| 60 virtual base::FilePath GetBuiltInDisplayProfileDirectory() const = 0; | |
| 61 virtual base::FilePath GetDownloadDisplayProfileDirectory() const = 0; | |
| 62 virtual void CheckDaysSinceOobe() = 0; // Must run on file thread. | |
| 63 virtual int GetDaysSinceOobe() const = 0; // Must call after CheckDays. | |
| 64 | |
| 65 protected: | |
| 66 friend class base::RefCounted<Delegate>; | |
| 67 virtual ~Delegate() = default; | |
| 68 | |
| 69 DISALLOW_ASSIGN(Delegate); | |
| 70 }; | |
| 71 | |
| 72 static void Initialize( | |
| 73 scoped_refptr<Delegate> delegate, | |
| 74 scoped_refptr<base::SequencedWorkerPool> blocking_pool, | |
| 75 PrefService* local_state, | |
| 76 scoped_refptr<net::URLRequestContextGetter> url_context_getter); | |
| 77 static void Shutdown(); | |
| 78 static QuirksManager* Get(); | |
| 79 | |
| 80 static void RegisterPrefs(PrefRegistrySimple* registry); | |
| 81 | |
| 82 // Entry point into manager. Finds or downloads icc file. | |
| 83 void RequestIccFilePath(int64_t product_id, | |
| 84 const RequestFinishedCallback& on_request_finished); | |
| 85 | |
| 86 void ClientFinished(QuirksClient* client); | |
| 87 | |
| 88 // UMA stats for server check reason and results. | |
| 89 void RecordReasonUmaStat(QuirksClient::RequestReason reason); | |
| 90 void RecordFileFoundUmaStat(bool success); | |
| 91 | |
| 92 // Creates a real URLFetcher for OS, and a fake one for tests. | |
| 93 scoped_ptr<net::URLFetcher> CreateURLFetcher( | |
| 94 const std::string& url, | |
| 95 net::URLFetcherDelegate* delegate); | |
| 96 | |
| 97 Delegate* delegate() const { return delegate_.get(); } | |
| 98 base::SequencedWorkerPool* blocking_pool() const { | |
| 99 return blocking_pool_.get(); | |
| 100 } | |
| 101 | |
| 102 net::URLRequestContextGetter* url_context_getter() const { | |
| 103 return url_context_getter_.get(); | |
| 104 } | |
| 105 | |
| 106 protected: | |
| 107 friend class QuirksBrowserTest; | |
| 108 | |
| 109 void SetFakeQuirksFetcherCreatorForTests( | |
| 110 const FakeQuirksFetcherCreator& creator) { | |
| 111 fake_quirks_fetcher_creator_ = creator; | |
| 112 } | |
| 113 | |
| 114 private: | |
| 115 QuirksManager(scoped_refptr<Delegate> delegate, | |
| 116 scoped_refptr<base::SequencedWorkerPool> blocking_pool, | |
| 117 PrefService* local_state, | |
| 118 scoped_refptr<net::URLRequestContextGetter> url_context_getter); | |
| 119 ~QuirksManager(); | |
| 120 | |
| 121 // Callback after checking for existing icc file; launch client if not found. | |
| 122 void OnIccFilePathRequestCompleted( | |
| 123 int64_t product_id, | |
| 124 const RequestFinishedCallback& on_request_finished, | |
| 125 base::FilePath path); | |
| 126 | |
| 127 // Determines need to check based on previous check time and device age. | |
| 128 bool NeedToCheckServer(int64_t product_id); | |
| 129 | |
| 130 // Records time of most recent server check. | |
| 131 void SetLastServerCheck(int64_t product_id, const base::Time& last_check); | |
| 132 | |
| 133 std::set<scoped_ptr<QuirksClient>> clients_; | |
| 134 | |
| 135 // These objects provide resources from the browser. | |
| 136 scoped_refptr<Delegate> delegate_; // Impl runs from chrome/browser. | |
| 137 scoped_refptr<base::SequencedWorkerPool> blocking_pool_; | |
| 138 PrefService* local_state_; // For local prefs. | |
| 139 scoped_refptr<net::URLRequestContextGetter> url_context_getter_; | |
| 140 | |
| 141 FakeQuirksFetcherCreator fake_quirks_fetcher_creator_; // For tests. | |
| 142 | |
| 143 // Factory for callbacks. | |
| 144 base::WeakPtrFactory<QuirksManager> weak_ptr_factory_; | |
| 145 | |
| 146 DISALLOW_COPY_AND_ASSIGN(QuirksManager); | |
| 147 }; | |
| 148 | |
| 149 } // namespace quirks | |
| 150 | |
| 151 #endif // COMPONENTS_QUIRKS_QUIRKS_MANAGER_H_ | |
| OLD | NEW |