| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 #include "chrome/browser/metrics/chrome_metrics_services_manager_client.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/prefs/pref_service.h" | |
| 10 #include "chrome/browser/browser_process.h" | |
| 11 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" | |
| 12 #include "chrome/browser/metrics/chrome_metrics_service_client.h" | |
| 13 #include "chrome/browser/metrics/variations/chrome_variations_service_client.h" | |
| 14 #include "chrome/browser/metrics/variations/ui_string_overrider_factory.h" | |
| 15 #include "chrome/browser/ui/browser_otr_state.h" | |
| 16 #include "chrome/common/chrome_switches.h" | |
| 17 #include "chrome/installer/util/google_update_settings.h" | |
| 18 #include "components/metrics/metrics_state_manager.h" | |
| 19 #include "components/rappor/rappor_service.h" | |
| 20 #include "components/variations/service/variations_service.h" | |
| 21 #include "content/public/browser/browser_thread.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread | |
| 26 // because it needs access to IO and cannot work from UI thread. | |
| 27 void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) { | |
| 28 content::BrowserThread::GetBlockingPool()->PostTask( | |
| 29 FROM_HERE, | |
| 30 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info)); | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 ChromeMetricsServicesManagerClient::ChromeMetricsServicesManagerClient( | |
| 36 PrefService* local_state) | |
| 37 : local_state_(local_state) { | |
| 38 DCHECK(local_state); | |
| 39 } | |
| 40 | |
| 41 ChromeMetricsServicesManagerClient::~ChromeMetricsServicesManagerClient() {} | |
| 42 | |
| 43 scoped_ptr<rappor::RapporService> | |
| 44 ChromeMetricsServicesManagerClient::CreateRapporService() { | |
| 45 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 46 return make_scoped_ptr(new rappor::RapporService( | |
| 47 local_state_, base::Bind(&chrome::IsOffTheRecordSessionActive))); | |
| 48 } | |
| 49 | |
| 50 scoped_ptr<variations::VariationsService> | |
| 51 ChromeMetricsServicesManagerClient::CreateVariationsService() { | |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 53 return variations::VariationsService::Create( | |
| 54 make_scoped_ptr(new ChromeVariationsServiceClient()), local_state_, | |
| 55 GetMetricsStateManager(), switches::kDisableBackgroundNetworking, | |
| 56 chrome_variations::CreateUIStringOverrider()); | |
| 57 } | |
| 58 | |
| 59 scoped_ptr<metrics::MetricsServiceClient> | |
| 60 ChromeMetricsServicesManagerClient::CreateMetricsServiceClient() { | |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 62 return ChromeMetricsServiceClient::Create(GetMetricsStateManager(), | |
| 63 local_state_); | |
| 64 } | |
| 65 | |
| 66 net::URLRequestContextGetter* | |
| 67 ChromeMetricsServicesManagerClient::GetURLRequestContext() { | |
| 68 return g_browser_process->system_request_context(); | |
| 69 } | |
| 70 | |
| 71 bool ChromeMetricsServicesManagerClient::IsSafeBrowsingEnabled( | |
| 72 const base::Closure& on_update_callback) { | |
| 73 // Start listening for updates to SB service state. This is done here instead | |
| 74 // of in the constructor to avoid errors from trying to instantiate SB | |
| 75 // service before the IO thread exists. | |
| 76 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); | |
| 77 if (!sb_state_subscription_ && sb_service) { | |
| 78 // It is safe to pass the callback received from the | |
| 79 // MetricsServicesManager here since the MetricsServicesManager owns | |
| 80 // this object, which owns the sb_state_subscription_, which owns the | |
| 81 // pointer to the MetricsServicesManager. | |
| 82 sb_state_subscription_ = | |
| 83 sb_service->RegisterStateCallback(on_update_callback); | |
| 84 } | |
| 85 | |
| 86 return sb_service && sb_service->enabled_by_prefs(); | |
| 87 } | |
| 88 | |
| 89 bool ChromeMetricsServicesManagerClient::IsMetricsReportingEnabled() { | |
| 90 return ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled(); | |
| 91 } | |
| 92 | |
| 93 bool ChromeMetricsServicesManagerClient::OnlyDoMetricsRecording() { | |
| 94 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | |
| 95 return cmdline->HasSwitch(switches::kMetricsRecordingOnly) || | |
| 96 cmdline->HasSwitch(switches::kEnableBenchmarking); | |
| 97 } | |
| 98 | |
| 99 metrics::MetricsStateManager* | |
| 100 ChromeMetricsServicesManagerClient::GetMetricsStateManager() { | |
| 101 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 102 if (!metrics_state_manager_) { | |
| 103 metrics_state_manager_ = metrics::MetricsStateManager::Create( | |
| 104 local_state_, | |
| 105 base::Bind( | |
| 106 &ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled), | |
| 107 base::Bind(&PostStoreMetricsClientInfo), | |
| 108 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo)); | |
| 109 } | |
| 110 return metrics_state_manager_.get(); | |
| 111 } | |
| OLD | NEW |