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/time/time.h" | |
14 #include "components/quirks/quirks_client.h" | |
15 #include "components/quirks/quirks_export.h" | |
16 | |
17 class GURL; | |
18 class PrefRegistrySimple; | |
19 class PrefService; | |
20 | |
21 namespace base { | |
22 class SequencedWorkerPool; | |
23 class SingleThreadTaskRunner; | |
24 } | |
25 | |
26 namespace net { | |
27 class URLFetcher; | |
28 class URLFetcherDelegate; | |
29 class URLRequestContextGetter; | |
30 } | |
31 | |
32 namespace quirks { | |
33 | |
34 // Format int as hex string for filename. | |
35 QUIRKS_EXPORT std::string IdToHexString(int64_t product_id); | |
36 | |
37 // Append ".icc" to hex string in filename. | |
38 QUIRKS_EXPORT std::string IdToFileName(int64_t product_id); | |
39 | |
40 // Returns path to icc file, if it exists; creates and starts QuirksClient if | |
41 // it doesn't. | |
42 QUIRKS_EXPORT base::FilePath RequestIccProfilePath( | |
43 int64_t product_id, | |
44 const DownloadFinishedCallback& on_download_finished); | |
45 | |
46 // Handles needed components from browser (local preferences, url context | |
47 // getter, message loops). | |
48 class QUIRKS_EXPORT QuirksManager { | |
49 public: | |
50 using FakeQuirksFetcherCreator = | |
51 base::Callback<scoped_ptr<net::URLFetcher>(const GURL&, | |
52 net::URLFetcherDelegate*)>; | |
53 | |
54 // Delegate class, so implementation can access browser functionality. | |
55 class Delegate { | |
56 public: | |
57 virtual ~Delegate() = default; | |
58 virtual std::string GetApiKey() const = 0; | |
59 virtual base::FilePath GetBuiltInDisplayProfileDirectory() const = 0; | |
60 virtual base::FilePath GetDownloadDisplayProfileDirectory() const = 0; | |
61 virtual int GetDaysSinceOobe() const = 0; // Must run on file thread. | |
62 | |
63 private: | |
64 DISALLOW_ASSIGN(Delegate); | |
65 }; | |
66 | |
67 static void Initialize( | |
68 scoped_ptr<Delegate> delegate, | |
69 scoped_refptr<base::SequencedWorkerPool> blocking_pool, | |
70 PrefService* local_state, | |
71 scoped_refptr<net::URLRequestContextGetter> url_context_getter); | |
72 static void Shutdown(); | |
73 static QuirksManager* Get(); | |
74 | |
75 static void RegisterPrefs(PrefRegistrySimple* registry); | |
76 | |
77 // Returns path to existing icc file, or starts client to download one. | |
78 base::FilePath RequestIccFilePath( | |
79 int64_t product_id, | |
80 const DownloadFinishedCallback& on_download_finished); | |
81 | |
82 void ClientFinished(QuirksClient* client); | |
83 | |
84 // UMA stats for server check reason and results. | |
85 void RecordReasonUmaStat(QuirksClient::RequestReason reason); | |
86 void RecordFileFoundUmaStat(bool success); | |
87 | |
88 // Switch to fake URLFetcher creator for tests. | |
stevenjb
2016/02/17 23:38:55
Clarify comment - this sounds like it is only for
Greg Levin
2016/03/02 00:00:26
Done.
| |
89 scoped_ptr<net::URLFetcher> CreateURLFetcher( | |
90 const std::string& url, | |
91 net::URLFetcherDelegate* delegate); | |
92 | |
93 Delegate* delegate() const { return delegate_.get(); } | |
94 base::SequencedWorkerPool* blocking_pool() const { | |
95 return blocking_pool_.get(); | |
96 } | |
97 | |
98 net::URLRequestContextGetter* url_context_getter() const { | |
99 return url_context_getter_.get(); | |
100 } | |
101 | |
102 base::SingleThreadTaskRunner* client_task_runner() const { | |
103 return client_task_runner_.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(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 void RunClientOnTaskRunner( | |
122 int64_t product_id, | |
123 const DownloadFinishedCallback& on_download_finished); | |
124 | |
125 // Determines need to check based on previous check time and device age. | |
126 bool NeedToCheckServer(int64_t product_id); | |
127 | |
128 // Records time of most recent server check. | |
129 void SetLastServerCheck(int64_t product_id, const base::Time& last_check); | |
130 | |
131 // Check that client task runner and local_state_ are ready. | |
132 bool ValidateOnTaskRunner(); | |
133 | |
134 std::set<scoped_ptr<QuirksClient>> clients_; | |
135 | |
136 // These objects provide resources from the browser. | |
137 scoped_ptr<Delegate> delegate_; // Impl runs from browser. | |
138 // For url getter and file io. | |
139 scoped_refptr<base::SequencedWorkerPool> blocking_pool_; | |
140 PrefService* local_state_; // For local prefs. | |
141 scoped_refptr<net::URLRequestContextGetter> url_context_getter_; | |
142 scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_; | |
143 | |
144 bool is_new_device_; // Is within 30 days of OOBE? | |
145 FakeQuirksFetcherCreator fake_quirks_fetcher_creator_; // For tests. | |
146 | |
147 // Factory for callbacks. | |
148 base::WeakPtrFactory<QuirksManager> weak_ptr_factory_; | |
149 | |
150 DISALLOW_COPY_AND_ASSIGN(QuirksManager); | |
151 }; | |
152 | |
153 } // namespace quirks | |
154 | |
155 #endif // COMPONENTS_QUIRKS_QUIRKS_MANAGER_H_ | |
OLD | NEW |