OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015 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 CHROMEOS_QUIRKS_CLIENT_QUIRKS_CLIENT_H_ | |
6 #define CHROMEOS_QUIRKS_CLIENT_QUIRKS_CLIENT_H_ | |
Greg Levin
2016/01/27 21:26:09
COMPONENTS_QUIRKS_CLIENT_QUIRKS_CLIENT_H_
| |
7 | |
8 #include "base/files/file_path.h" | |
9 #include "base/macros.h" | |
10 #include "base/time/time.h" | |
11 #include "base/timer/timer.h" | |
12 #include "components/quirks_client/quirks_client_export.h" | |
13 #include "net/url_request/url_fetcher_delegate.h" | |
14 | |
15 class PrefRegistrySimple; | |
16 class PrefService; | |
17 | |
18 namespace base { | |
19 class SequencedWorkerPool; | |
20 class MessageLoopForUI; | |
21 } | |
22 | |
23 namespace net { | |
24 class URLRequestContextGetter; | |
25 } | |
26 | |
27 namespace quirks_client { | |
28 | |
29 // Handles providing data from icc and other display data files. This may | |
30 // involve downloading and storing files from the Quirks Server, or serving | |
31 // these files from local, writable storage. | |
32 class QUIRKS_CLIENT_EXPORT QuirksClient : public net::URLFetcherDelegate { | |
33 public: | |
34 typedef base::Callback<void(base::FilePath)> DownloadFinishedCallback; | |
35 | |
36 QuirksClient(int64_t product_id, | |
37 DownloadFinishedCallback* on_download_finished); | |
38 ~QuirksClient() override; | |
39 | |
40 enum RequestReason { | |
41 FIRST, // Device's first server request. | |
42 RETRY, // Has server file been added since last check? | |
43 UPDATE // Has server file been updated since last check? | |
44 }; | |
45 | |
46 // Returns path to icc file, if it exists; creates and starts QuirksServer if | |
47 // it doesn't. | |
48 static base::FilePath RequestIccProfilePath( | |
49 int64_t product_id, | |
50 DownloadFinishedCallback* on_download_finished); | |
51 | |
52 // Start download. | |
53 void Start(); | |
54 | |
55 // net::URLFetcherDelegate | |
56 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
Greg Levin
2016/01/27 21:26:09
Make private
| |
57 | |
58 // TODO(glevin): Copied from wallpaper downloader. Are they needed? TBD. | |
59 // This is called in tests to modify (lower) retry delay. | |
60 void set_retry_delay_for_testing(base::TimeDelta value) { | |
61 retry_delay_ = value; | |
62 } | |
63 | |
64 base::TimeDelta retry_current_delay_for_testing() const { | |
65 return retry_current_delay_; | |
66 } | |
67 | |
68 private: | |
69 // Self deletion when done | |
70 void Shutdown(); | |
71 | |
72 // Schedules retry. | |
73 void Retry(); | |
74 | |
75 // Write |data| to |file_path|. | |
76 static bool WriteIccFile(const base::FilePath file_path, | |
77 const std::string& data); | |
78 | |
79 // Callback after icc write is finished. | |
80 void OnWriteIccFileFinished(bool success); | |
81 | |
82 // Translate json with base64-encoded data (|result|) into raw |data|. | |
83 bool ParseResult(const std::string& result, std::string* data); | |
84 | |
85 // ID of display to request from Quirks Server. | |
86 const int64_t product_id_; | |
87 | |
88 // Callback supplied by caller. | |
89 const DownloadFinishedCallback* on_download_finished_; | |
90 | |
91 // Full path to icc file. | |
92 const base::FilePath icc_path_; | |
93 | |
94 // This fetcher is used to download icc file. | |
95 scoped_ptr<net::URLFetcher> url_fetcher_; | |
96 | |
97 // Why are we making this server call? | |
98 RequestReason request_reason_; | |
99 | |
100 // Pending retry. | |
101 base::OneShotTimer request_scheduled_; | |
102 | |
103 // Number of download retries (first attempt is not counted as retry). | |
104 size_t retries_; | |
105 | |
106 // TODO(glevin): Copied from wallpaper downloader. Are they needed? TBD. | |
107 // Sleep between retry requests (increasing, see Retry() method for details). | |
108 // Non-constant value for tests. | |
109 base::TimeDelta retry_delay_; | |
110 | |
111 // Retry delay of the last attempt. For testing only. | |
112 base::TimeDelta retry_current_delay_; | |
113 | |
114 // Factory for callbacks. | |
115 base::WeakPtrFactory<QuirksClient> weak_ptr_factory_; | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(QuirksClient); | |
118 }; | |
119 | |
120 // Handles needed components from browser (local preferences, url context | |
121 // getter, message loops). | |
122 class QUIRKS_CLIENT_EXPORT QuirksClientDelegate { | |
123 public: | |
124 QuirksClientDelegate(base::MessageLoopForUI* message_loop_ui, | |
125 base::SequencedWorkerPool* blocking_pool, | |
126 PrefService* local_state, | |
127 net::URLRequestContextGetter* url_context_getter); | |
128 virtual ~QuirksClientDelegate(); | |
129 | |
130 static void Initialize(QuirksClientDelegate* delegate); | |
131 static void Shutdown(); | |
132 static void RegisterPrefs(PrefRegistrySimple* registry); | |
133 | |
134 void RunClient(int64_t product_id, | |
135 QuirksClient::DownloadFinishedCallback* on_download_finished); | |
136 | |
137 base::Time GetLastServerCheck(int64_t product_id); | |
138 void SetLastServerCheck(int64_t product_id, base::Time last_check); | |
139 void RecordReasonUmaStat(QuirksClient::RequestReason reason); | |
140 void RecordFileFoundUmaStat(bool success); | |
141 | |
142 base::MessageLoopForUI* message_loop_ui() { return message_loop_ui_; } | |
143 base::SequencedWorkerPool* blocking_pool() { return blocking_pool_; } | |
144 net::URLRequestContextGetter* url_context_getter() { | |
145 return url_context_getter_.get(); | |
146 } | |
147 | |
148 // Check that ui thread and message loop are ready. | |
149 bool Validate(); | |
150 | |
151 virtual std::string api_key_quirks() = 0; | |
152 virtual base::FilePath GetDisplayProfileDirectory() = 0; | |
153 virtual int GetDaysSinceOobe() = 0; | |
154 | |
155 private: | |
156 // browser/ui thread components needed for client. | |
157 base::MessageLoopForUI* message_loop_ui_; // To run QC on ui thread. | |
158 base::SequencedWorkerPool* blocking_pool_; // for url getter. | |
159 PrefService* local_state_; // For local prefs. | |
160 bool is_new_device_; // Is within 30 days of OOBE? | |
161 // For URLFetcher. | |
162 scoped_refptr<net::URLRequestContextGetter> url_context_getter_; | |
163 | |
164 DISALLOW_COPY_AND_ASSIGN(QuirksClientDelegate); | |
165 }; | |
166 | |
167 } // namespace chromeos | |
168 | |
169 #endif // CHROMEOS_QUIRKS_CLIENT_QUIRKS_CLIENT_H_ | |
OLD | NEW |