Index: components/quirks_client/quirks_client.h |
diff --git a/components/quirks_client/quirks_client.h b/components/quirks_client/quirks_client.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..539df399b37d63ccee2da0e4a2b4ed46dd03e14d |
--- /dev/null |
+++ b/components/quirks_client/quirks_client.h |
@@ -0,0 +1,166 @@ |
+// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROMEOS_QUIRKS_CLIENT_QUIRKS_CLIENT_H_ |
+#define CHROMEOS_QUIRKS_CLIENT_QUIRKS_CLIENT_H_ |
+ |
+#include "base/files/file_path.h" |
+#include "base/macros.h" |
+#include "base/time/time.h" |
+#include "base/timer/timer.h" |
+#include "components/quirks_client/quirks_client_export.h" |
+#include "net/url_request/url_fetcher_delegate.h" |
+ |
+class PrefRegistrySimple; |
+class PrefService; |
+ |
+namespace base { |
+class SequencedWorkerPool; |
+class MessageLoopForUI; |
+} |
+ |
+namespace net { |
+class URLRequestContextGetter; |
+} |
+ |
+namespace quirks_client { |
+ |
+// Handles providing data from icc and other display data files. This may |
+// involve downloading and storing files from the Quirks Server, or loading |
+// these files from local, writable storage. |
+class QUIRKS_CLIENT_EXPORT QuirksClient : public net::URLFetcherDelegate { |
+ public: |
+ typedef base::Callback<void(base::FilePath)> DownloadFinishedCallback; |
+ |
+ QuirksClient(int64_t product_id, |
+ DownloadFinishedCallback* on_download_finished); |
+ ~QuirksClient() override; |
+ |
+ enum RequestReason { |
+ FIRST, // Device's first server request. |
+ RETRY, // Has server file been added since last check? |
+ UPDATE // Has server file been updated since last check? |
+ }; |
+ |
+ // Returns path to icc file, if it exists; creates and starts QuirksServer if |
+ // it doesn't. |
+ static base::FilePath RequestIccProfilePath( |
+ int64_t product_id, |
+ DownloadFinishedCallback* on_download_finished); |
+ |
+ // Start download. |
+ void Start(); |
+ |
+ // net::URLFetcherDelegate |
+ void OnURLFetchComplete(const net::URLFetcher* source) override; |
+ |
+ // TODO(glevin): Copied from wallpaper downloader. Are they needed? TBD. |
+ // This is called in tests to modify (lower) retry delay. |
+ void set_retry_delay_for_testing(base::TimeDelta value) { |
+ retry_delay_ = value; |
+ } |
+ |
+ base::TimeDelta retry_current_delay_for_testing() const { |
+ return retry_current_delay_; |
+ } |
+ |
+ private: |
+ // Self deletion when done |
+ void Shutdown(); |
+ |
+ // Schedules retry. |
+ void Retry(); |
+ |
+ // Write |data| to |file_path|. |
+ static bool WriteIccFile(const base::FilePath file_path, |
+ const std::string& data); |
+ |
+ // Callback after icc write is finished. |
+ void OnWriteIccFileFinished(bool success); |
+ |
+ // Translate json with base64-encoded data (|result|) into raw |data|. |
+ bool ParseResult(const std::string& result, std::string* data); |
+ |
+ // ID of display to request from Quirks Server. |
+ const int64_t product_id_; |
+ |
+ // Callback supplied by caller. |
+ const DownloadFinishedCallback* on_download_finished_; |
+ |
+ // Full path to icc file (includes quirks_dir_). |
+ const base::FilePath icc_path_; |
+ |
+ // This fetcher is used to download icc file. |
+ scoped_ptr<net::URLFetcher> url_fetcher_; |
+ |
+ // Why are we making this server call? |
+ RequestReason request_reason_; |
+ |
+ // Pending retry. |
+ base::OneShotTimer request_scheduled_; |
+ |
+ // Number of download retries (first attempt is not counted as retry). |
+ size_t retries_; |
+ |
+ // Sleep between retry requests (increasing, see Retry() method for details). |
+ // Non-constant value for tests. |
+ base::TimeDelta retry_delay_; |
+ |
+ // Retry delay of the last attempt. For testing only. |
+ base::TimeDelta retry_current_delay_; |
+ |
+ // Factory for callbacks. |
+ base::WeakPtrFactory<QuirksClient> weak_ptr_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(QuirksClient); |
+}; |
+ |
+// Handles needed components from browser (local preferences, url context |
+// getter, message loops). |
+class QUIRKS_CLIENT_EXPORT QuirksClientDelegate { |
+ public: |
+ QuirksClientDelegate(base::MessageLoopForUI* message_loop_ui, |
+ base::SequencedWorkerPool* blocking_pool, |
+ PrefService* local_state, |
+ net::URLRequestContextGetter* url_context_getter); |
+ virtual ~QuirksClientDelegate(); |
+ |
+ static void Initialize(QuirksClientDelegate* delegate); |
+ static void Shutdown(); |
+ static void RegisterPrefs(PrefRegistrySimple* registry); |
+ |
+ void RunClient(int64_t product_id, |
+ QuirksClient::DownloadFinishedCallback* on_download_finished); |
+ |
+ base::Time GetLastServerCheck(int64_t product_id); |
+ void SetLastServerCheck(int64_t product_id, base::Time last_check); |
+ void RecordReasonUmaStat(QuirksClient::RequestReason reason); |
+ void RecordFileFoundUmaStat(bool success); |
+ |
+ base::MessageLoopForUI* message_loop_ui() { return message_loop_ui_; } |
+ base::SequencedWorkerPool* blocking_pool() { return blocking_pool_; } |
+ net::URLRequestContextGetter* url_context_getter() { |
+ return url_context_getter_.get(); |
+ } |
+ |
+ // Check that all browser threads and message loops are ready. |
+ bool Validate(); |
+ |
+ virtual std::string api_key_quirks() = 0; |
+ virtual base::FilePath GetDisplayProfileDirectory() = 0; |
+ |
+ private: |
+ // browser/ui thread components needed for client. |
+ base::MessageLoopForUI* message_loop_ui_; // To run QC on browser thread. |
+ base::SequencedWorkerPool* blocking_pool_; // for url getter. |
+ PrefService* local_state_; // For local prefs. |
+ // For URLFetcher. |
+ scoped_refptr<net::URLRequestContextGetter> url_context_getter_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(QuirksClientDelegate); |
+}; |
+ |
+} // namespace chromeos |
+ |
+#endif // CHROMEOS_QUIRKS_CLIENT_QUIRKS_CLIENT_H_ |