Chromium Code Reviews| 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..d675d12da82337812698514412daba7472136598 |
| --- /dev/null |
| +++ b/components/quirks_client/quirks_client_manager.cc |
| @@ -0,0 +1,179 @@ |
| +// 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 "content/public/browser/browser_thread.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| +#include "url/gurl.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, |
| + const 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; |
| + } |
| + |
| + 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)) { |
| + VLOG(2) << time_since.InDays() |
| + << " days since last Quirks Server check for display " |
| + << QuirksClient::IdToHexString(product_id); |
| + return; |
| + } |
| + |
| + // TODO!!! Before committing, we need to deal with ownership and lifecycle |
| + // management. Right now, it's probably possible that the client could try |
| + // to use the manager after its been destroyed. |
|
Greg Levin
2016/02/09 18:56:38
TODO in next patch
|
| + QuirksClient* quirks_client = |
| + new QuirksClient(product_id, on_download_finished); |
| + quirks_client->StartDownload(); |
| +} |
| + |
| +} // namespace |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// QuirksClientManager |
| + |
| +QuirksClientManager::QuirksClientManager( |
| + Delegate* delegate, |
| + PrefService* local_state, |
| + net::URLRequestContextGetter* url_context_getter) |
| + : delegate_(delegate), |
| + message_loop_ui_(base::MessageLoopForUI::current()), |
| + blocking_pool_(content::BrowserThread::GetBlockingPool()), |
| + local_state_(local_state), |
| + is_new_device_(false), |
| + url_context_getter_(url_context_getter) {} |
| + |
| +QuirksClientManager::~QuirksClientManager() { |
| + delete delegate_; |
| +} |
| + |
| +// static |
| +void QuirksClientManager::Initialize( |
| + Delegate* delegate, |
| + PrefService* local_state, |
| + net::URLRequestContextGetter* url_context_getter) { |
| + if (QuirksClient::IsEnabled()) |
| + g_manager = |
| + new QuirksClientManager(delegate, local_state, url_context_getter); |
| +} |
| + |
| +// 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, |
| + const 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) |
| + return base::Time::FromDoubleT(last_check); |
| + |
| + // 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; |
| +} |
| + |
| +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(); |
| +} |
| + |
| +scoped_ptr<net::URLFetcher> QuirksClientManager::CreateURLFetcher( |
| + const std::string& url, |
| + net::URLFetcherDelegate* delegate) { |
| + scoped_ptr<net::URLFetcher> url_fetcher = |
| + fake_quirks_fetcher_creator_.is_null() |
| + ? net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, delegate) |
| + : fake_quirks_fetcher_creator_.Run(GURL(url), delegate); |
| + return url_fetcher; |
| +} |
| + |
| +} // namespace quirks_client |