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..07d004df2a2a9d1fb32b9e7e9811ad4f7df5e303 |
| --- /dev/null |
| +++ b/components/quirks_client/quirks_client_manager.cc |
| @@ -0,0 +1,181 @@ |
| +// 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/rand_util.h" |
| +#include "base/thread_task_runner_handle.h" |
| +#include "components/prefs/pref_registry_simple.h" |
| +#include "components/prefs/scoped_user_pref_update.h" |
| +#include "components/quirks_client/pref_names.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::ThreadTaskRunnerHandle::IsSet()); |
| + 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 |
|
oshima
2016/02/11 20:05:51
TODO(glevin)
Greg Levin
2016/02/12 04:48:04
I was using the TODO!!! to denote things I wanted
|
| + // management. Right now, it's probably possible that the client could try |
| + // to use the manager after its been destroyed. |
| + QuirksClient* quirks_client = |
| + new QuirksClient(product_id, on_download_finished); |
| + quirks_client->StartDownload(); |
| +} |
| + |
| +} // namespace |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// QuirksClientManager |
| + |
| +QuirksClientManager::QuirksClientManager( |
| + Delegate* delegate, |
| + base::SequencedWorkerPool* blocking_pool, |
| + PrefService* local_state, |
| + net::URLRequestContextGetter* url_context_getter) |
| + : delegate_(delegate), |
| + blocking_pool_(blocking_pool), |
| + local_state_(local_state), |
| + url_context_getter_(url_context_getter), |
| + task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| + is_new_device_(false) {} |
| + |
| +QuirksClientManager::~QuirksClientManager() { |
| + delete delegate_; |
| +} |
| + |
| +// static |
| +void QuirksClientManager::Initialize( |
| + Delegate* delegate, |
| + base::SequencedWorkerPool* blocking_pool, |
| + PrefService* local_state, |
| + net::URLRequestContextGetter* url_context_getter) { |
| + if (QuirksClient::IsEnabled()) |
| + g_manager = new QuirksClientManager(delegate, blocking_pool, local_state, |
| + url_context_getter); |
| +} |
| + |
| +// static |
| +void QuirksClientManager::Shutdown() { |
| + delete g_manager; |
| + g_manager = nullptr; |
| +} |
| + |
| +// static |
| +QuirksClientManager* QuirksClientManager::Get() { |
|
oshima
2016/02/11 20:05:51
DCHECK(g_manager) ?
Greg Levin
2016/02/12 04:48:04
Done.
|
| + 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); |
|
oshima
2016/02/11 20:05:51
isn't it better to check if the data exist to chec
Greg Levin
2016/02/12 04:48:04
Discussed off-line
|
| + |
| + task_runner_->PostTask(FROM_HERE, base::Bind(&RunClientOnUIThread, product_id, |
| + on_download_finished)); |
| +} |
| + |
| +base::Time QuirksClientManager::GetLastServerCheck(int64_t product_id) { |
| + DCHECK(base::ThreadTaskRunnerHandle::IsSet()); |
| + 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::ThreadTaskRunnerHandle::IsSet()); |
| + 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) {} |
|
oshima
2016/02/11 20:05:51
In general, I'd recommend to add these methods whe
Greg Levin
2016/02/12 04:48:04
Acknowledged. In this case, I wanted placeholders
|
| + |
| +bool QuirksClientManager::ValidateOnUiThread() { |
| + DCHECK(base::ThreadTaskRunnerHandle::IsSet()); |
| + return local_state_->CalledOnValidThread() && |
| + base::ThreadTaskRunnerHandle::IsSet() && task_runner_ && |
| + task_runner_->RunsTasksOnCurrentThread(); |
| +} |
| + |
| +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; |
| +} |
| + |
| +base::SingleThreadTaskRunner* QuirksClientManager::TaskRunner() { |
| + return task_runner_.get(); |
| +} |
| + |
| +} // namespace quirks_client |