Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Unified Diff: components/quirks_client/quirks_client_manager.cc

Issue 1528963002: Quirks Client for downloading and providing display profiles (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: First round of review fixes, separate QCManager into separate file Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/quirks_client/quirks_client_manager.cc
diff --git a/components/quirks_client/quirks_client_manager.cc b/components/quirks_client/quirks_client_manager.cc
new file mode 100644
index 0000000000000000000000000000000000000000..56f33d3a6913829fbd0c2b9202353f9b5c43f45a
--- /dev/null
+++ b/components/quirks_client/quirks_client_manager.cc
@@ -0,0 +1,173 @@
+// Copyright 2016 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.
+
+#include "components/quirks_client/quirks_client_manager.h"
+
+#include "base/message_loop/message_loop.h"
+#include "base/prefs/pref_registry_simple.h"
+#include "base/prefs/scoped_user_pref_update.h"
+#include "base/rand_util.h"
+#include "base/thread_task_runner_handle.h"
+#include "components/quirks_client/pref_names.h"
+#include "net/url_request/url_request_context_getter.h"
+
+namespace quirks_client {
+
+namespace {
+
+QUIRKS_CLIENT_EXPORT QuirksClientManager* g_manager = nullptr;
+
+// How often we query Quirks Server.
+const int kDaysBetweenServerChecks = 30;
+
+// Initializes QuirksClient object on the ui thread, where most of its work
+// needs to be done.
+void RunClientOnUIThread(
+ int64_t product_id,
+ QuirksClient::DownloadFinishedCallback* on_download_finished) {
+ DCHECK(base::MessageLoopForUI::IsCurrent());
+
+ if (!g_manager || !g_manager->ValidateOnUiThread()) {
+ VLOG(1) << "Quirks Client Manager not initialized; can't start Client.";
+ return;
+ }
+
+ // TODO: Why does this work, but ::current() doesn't?
stevenjb 2016/02/02 23:45:51 ?
Greg Levin 2016/02/04 20:51:11 Remove comment
Greg Levin 2016/02/09 18:56:38 On 2016/02/04 20:51:11, Greg Levin wrote: Was note
+
+ const base::Time last_check =
+ g_manager->GetLastServerCheck(product_id);
+ const base::TimeDelta time_since = base::Time::Now() - last_check;
+
+ // If we haven't checked Quirks Server in the last 30 days, create a
+ // QuirksClient to check Quirks Server for icc file or updates.
+ if (time_since >= base::TimeDelta::FromDays(kDaysBetweenServerChecks)) {
stevenjb 2016/02/02 23:45:51 invert and early exit
Greg Levin 2016/02/09 18:56:38 Done.
+ // TODO(glevin): Do we want to keep a pointer to quirks_client, or just let
+ // it roam free and delete itself when it's done?
+ /* Might need to handle interaction with delegate more intelligently. If
+ * offline, the client might persist, and keep trying. If it tries during
+ * shutdown, while the delegate is invalidating, it might cause trouble. */
stevenjb 2016/02/02 23:45:51 (No C style comments) Generally try to avoid unow
Greg Levin 2016/02/09 18:56:38 Acknowledged... punting larger architectural chang
+ QuirksClient* quirks_client =
+ new QuirksClient(product_id, on_download_finished);
+ quirks_client->StartDownload();
+ } else {
+ VLOG(2) << time_since.InDays()
+ << " days since last Quirks Server check for display "
+ << QuirksClient::IdToHexString(product_id);
+ }
+}
+
+} // namespace
+
+////////////////////////////////////////////////////////////////////////////////
+// QuirksClientManager
+
+QuirksClientManager::QuirksClientManager(
+ Delegate* delegate,
+ base::MessageLoopForUI* message_loop_ui,
+ base::SequencedWorkerPool* blocking_pool,
+ PrefService* local_state,
+ net::URLRequestContextGetter* url_context_getter)
+ : delegate_(delegate),
+ message_loop_ui_(message_loop_ui),
+ blocking_pool_(blocking_pool),
+ local_state_(local_state),
+ is_new_device_(false),
+ url_context_getter_(url_context_getter) {}
+
+QuirksClientManager::~QuirksClientManager() {
+ delete delegate_;
+}
+
+// static
+void QuirksClientManager::Initialize(QuirksClientManager* manager) {
+ if (!QuirksClient::IsEnabled()) {
+ delete manager;
+ return;
+ }
+ g_manager = manager;
+}
+
+// static
+void QuirksClientManager::Shutdown() {
+ delete g_manager;
+ g_manager = nullptr;
+}
+
+// static
+QuirksClientManager* QuirksClientManager::Get() {
+ return g_manager;
+}
+
+// static
+void QuirksClientManager::RegisterPrefs(PrefRegistrySimple* registry) {
+ registry->RegisterDictionaryPref(prefs::kQuirksClientLastServerCheck);
+}
+
+void QuirksClientManager::RunClient(
+ int64_t product_id,
+ QuirksClient::DownloadFinishedCallback* on_download_finished) {
+ DCHECK(blocking_pool()->RunsTasksOnCurrentThread());
+
+ // Record this on io thread for future use on ui thread.
+ is_new_device_ = (delegate_->GetDaysSinceOobe() <= kDaysBetweenServerChecks);
+
+ message_loop_ui_->PostTask(
+ FROM_HERE,
+ base::Bind(&RunClientOnUIThread, product_id, on_download_finished));
+}
+
+base::Time QuirksClientManager::GetLastServerCheck(int64_t product_id) {
+ DCHECK(base::MessageLoopForUI::IsCurrent());
+ double last_check = 0.0;
+ const base::DictionaryValue* dict =
+ local_state_->GetDictionary(prefs::kQuirksClientLastServerCheck);
+ if (dict)
+ dict->GetDouble(QuirksClient::IdToHexString(product_id), &last_check);
+
+ if (last_check == 0.0) {
stevenjb 2016/02/02 23:45:51 invert and early exit
Greg Levin 2016/02/09 18:56:38 Done.
+ // On new devices, we want to check server immediately, so pretend its been
+ // too long since last check.
+ if (is_new_device_) {
+ return base::Time::Now() -
+ base::TimeDelta::FromDays(kDaysBetweenServerChecks + 1);
+ }
+
+ // Otherwise, for the first check on an older device, we want to stagger
+ // it over 30 days, so artificially set last check accordingly.
+ const int rand_days = base::RandInt(0, kDaysBetweenServerChecks);
+ const base::Time last_check_time =
+ base::Time::Now() - base::TimeDelta::FromDays(rand_days);
+ SetLastServerCheck(product_id, last_check_time);
+ VLOG(2) << "Delaying first Quirks Server check by "
+ << kDaysBetweenServerChecks - rand_days << " days.";
+ return last_check_time;
+ }
+
+ return base::Time::FromDoubleT(last_check);
+}
+
+void QuirksClientManager::SetLastServerCheck(int64_t product_id,
+ const base::Time& last_check) {
+ DCHECK(base::MessageLoopForUI::IsCurrent());
+ DictionaryPrefUpdate dict(local_state_, prefs::kQuirksClientLastServerCheck);
+ dict->SetDouble(QuirksClient::IdToHexString(product_id),
+ last_check.ToDoubleT());
+}
+
+// TODO(glevin): Add code to record UMA stats here. Also need to set
+// request_reason_ in QuirksClient.
+void QuirksClientManager::RecordReasonUmaStat(
+ QuirksClient::RequestReason reason) {}
+
+void QuirksClientManager::RecordFileFoundUmaStat(bool success) {}
+
+bool QuirksClientManager::ValidateOnUiThread() {
+ DCHECK(base::MessageLoopForUI::IsCurrent());
+ return base::MessageLoopForUI::IsCurrent() &&
+ base::MessageLoopForUI::current()->is_running() &&
+ local_state_->CalledOnValidThread() &&
+ base::ThreadTaskRunnerHandle::IsSet();
+}
+
+} // namespace quirks_client

Powered by Google App Engine
This is Rietveld 408576698