blob: 1cfd91cd96164c00e41cb02415103fb77f934a39 [file] [log] [blame]
glevin5dd01a72016-03-23 23:08:121// 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
dcheng82beb4f2016-04-26 00:35:028#include <memory>
glevin5dd01a72016-03-23 23:08:129#include <set>
10
11#include "base/callback.h"
12#include "base/files/file_path.h"
13#include "base/macros.h"
glevin5dd01a72016-03-23 23:08:1214#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
19class GURL;
20class PrefRegistrySimple;
21class PrefService;
22
23namespace base {
24class SequencedWorkerPool;
25}
26
27namespace net {
28class URLFetcher;
29class URLFetcherDelegate;
30class URLRequestContextGetter;
31}
32
33namespace quirks {
34
35class 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.
40using RequestFinishedCallback =
41 base::Callback<void(const base::FilePath&, bool)>;
42
43// Format int as hex string for filename.
44QUIRKS_EXPORT std::string IdToHexString(int64_t product_id);
45
46// Append ".icc" to hex string in filename.
47QUIRKS_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.
53class QUIRKS_EXPORT QuirksManager {
54 public:
55 // Passed function to create a URLFetcher for tests.
56 // Same parameters as URLFetcher::Create().
dcheng82beb4f2016-04-26 00:35:0257 using FakeQuirksFetcherCreator = base::Callback<
58 std::unique_ptr<net::URLFetcher>(const GURL&, net::URLFetcherDelegate*)>;
glevin5dd01a72016-03-23 23:08:1259
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
glevin5b821b52016-04-04 16:42:4780 // Whether downloads are allowed by enterprise device policy.
81 virtual bool DevicePolicyEnabled() const = 0;
82
glevin5dd01a72016-03-23 23:08:1283 // 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(
dcheng82beb4f2016-04-26 00:35:0291 std::unique_ptr<Delegate> delegate,
glevin5dd01a72016-03-23 23:08:1292 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.
dcheng82beb4f2016-04-26 00:35:02111 std::unique_ptr<net::URLFetcher> CreateURLFetcher(
glevin5dd01a72016-03-23 23:08:12112 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:
dcheng82beb4f2016-04-26 00:35:02130 QuirksManager(std::unique_ptr<Delegate> delegate,
glevin5dd01a72016-03-23 23:08:12131 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
glevin5b821b52016-04-04 16:42:47152 // Whether downloads allowed by cmd line flag and device policy.
153 bool QuirksEnabled();
154
glevin5dd01a72016-03-23 23:08:12155 // 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.
dcheng82beb4f2016-04-26 00:35:02159 std::set<std::unique_ptr<QuirksClient>> clients_;
glevin5dd01a72016-03-23 23:08:12160
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.
dcheng82beb4f2016-04-26 00:35:02168 std::unique_ptr<Delegate> delegate_; // Impl runs from chrome/browser.
glevin5dd01a72016-03-23 23:08:12169 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_