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_ |
| 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 loading |
| 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; |
| 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 (includes quirks_dir_). |
| 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 // Sleep between retry requests (increasing, see Retry() method for details). |
| 107 // Non-constant value for tests. |
| 108 base::TimeDelta retry_delay_; |
| 109 |
| 110 // Retry delay of the last attempt. For testing only. |
| 111 base::TimeDelta retry_current_delay_; |
| 112 |
| 113 // Factory for callbacks. |
| 114 base::WeakPtrFactory<QuirksClient> weak_ptr_factory_; |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(QuirksClient); |
| 117 }; |
| 118 |
| 119 // Handles needed components from browser (local preferences, url context |
| 120 // getter, message loops). |
| 121 class QUIRKS_CLIENT_EXPORT QuirksClientDelegate { |
| 122 public: |
| 123 QuirksClientDelegate(base::MessageLoopForUI* message_loop_ui, |
| 124 base::SequencedWorkerPool* blocking_pool, |
| 125 PrefService* local_state, |
| 126 net::URLRequestContextGetter* url_context_getter); |
| 127 virtual ~QuirksClientDelegate(); |
| 128 |
| 129 static void Initialize(QuirksClientDelegate* delegate); |
| 130 static void Shutdown(); |
| 131 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 132 |
| 133 void RunClient(int64_t product_id, |
| 134 QuirksClient::DownloadFinishedCallback* on_download_finished); |
| 135 |
| 136 base::Time GetLastServerCheck(int64_t product_id); |
| 137 void SetLastServerCheck(int64_t product_id, base::Time last_check); |
| 138 void RecordReasonUmaStat(QuirksClient::RequestReason reason); |
| 139 void RecordFileFoundUmaStat(bool success); |
| 140 |
| 141 base::MessageLoopForUI* message_loop_ui() { return message_loop_ui_; } |
| 142 base::SequencedWorkerPool* blocking_pool() { return blocking_pool_; } |
| 143 net::URLRequestContextGetter* url_context_getter() { |
| 144 return url_context_getter_.get(); |
| 145 } |
| 146 |
| 147 // Check that all browser threads and message loops are ready. |
| 148 bool Validate(); |
| 149 |
| 150 virtual std::string api_key_quirks() = 0; |
| 151 virtual base::FilePath GetDisplayProfileDirectory() = 0; |
| 152 |
| 153 private: |
| 154 // browser/ui thread components needed for client. |
| 155 base::MessageLoopForUI* message_loop_ui_; // To run QC on browser thread. |
| 156 base::SequencedWorkerPool* blocking_pool_; // for url getter. |
| 157 PrefService* local_state_; // For local prefs. |
| 158 // For URLFetcher. |
| 159 scoped_refptr<net::URLRequestContextGetter> url_context_getter_; |
| 160 |
| 161 DISALLOW_COPY_AND_ASSIGN(QuirksClientDelegate); |
| 162 }; |
| 163 |
| 164 } // namespace chromeos |
| 165 |
| 166 #endif // CHROMEOS_QUIRKS_CLIENT_QUIRKS_CLIENT_H_ |
OLD | NEW |