Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / tracing / chrome_tracing_delegate.cc
blobbc3338213f7b38bafbb22785fc065d4b6cd687d9
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.
5 #include "chrome/browser/tracing/chrome_tracing_delegate.h"
7 #include "base/prefs/pref_registry_simple.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/time/time.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/tracing/crash_service_uploader.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/browser/ui/browser_otr_state.h"
17 #include "chrome/common/pref_names.h"
18 #include "content/public/browser/background_tracing_config.h"
19 #include "content/public/browser/browser_thread.h"
21 namespace {
23 const int kMinDaysUntilNextUpload = 7;
25 } // namespace
27 void ChromeTracingDelegate::RegisterPrefs(PrefRegistrySimple* registry) {
28 registry->RegisterInt64Pref(prefs::kBackgroundTracingLastUpload, 0);
31 ChromeTracingDelegate::ChromeTracingDelegate() : incognito_launched_(false) {
32 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
33 BrowserList::AddObserver(this);
36 ChromeTracingDelegate::~ChromeTracingDelegate() {
37 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
38 BrowserList::RemoveObserver(this);
41 void ChromeTracingDelegate::OnBrowserAdded(Browser* browser) {
42 if (browser->profile()->IsOffTheRecord())
43 incognito_launched_ = true;
46 scoped_ptr<content::TraceUploader> ChromeTracingDelegate::GetTraceUploader(
47 net::URLRequestContextGetter* request_context) {
48 return scoped_ptr<content::TraceUploader>(
49 new TraceCrashServiceUploader(request_context));
52 namespace {
54 enum PermitMissingProfile { PROFILE_REQUIRED, PROFILE_NOT_REQUIRED };
56 bool ProfileAllowsScenario(const content::BackgroundTracingConfig& config,
57 PermitMissingProfile profile_permission) {
58 ProfileManager* profile_manager = g_browser_process->profile_manager();
59 if (!profile_manager)
60 return false;
62 Profile* profile = profile_manager->GetProfileByPath(
63 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir()));
65 // If the profile hasn't loaded or been created yet, we allow the scenario
66 // to start up, but not be finalized.
67 if (!profile) {
68 if (profile_permission == PROFILE_REQUIRED)
69 return false;
70 else
71 return true;
74 // Safeguard, in case background tracing is responsible for a crash on
75 // startup.
76 if (profile->GetLastSessionExitType() == Profile::EXIT_CRASHED)
77 return false;
79 if (config.tracing_mode() == content::BackgroundTracingConfig::PREEMPTIVE) {
80 PrefService* local_state = g_browser_process->local_state();
81 DCHECK(local_state);
82 const base::Time last_upload_time = base::Time::FromInternalValue(
83 local_state->GetInt64(prefs::kBackgroundTracingLastUpload));
84 if (!last_upload_time.is_null()) {
85 base::Time computed_next_allowed_time =
86 last_upload_time + base::TimeDelta::FromDays(kMinDaysUntilNextUpload);
87 if (computed_next_allowed_time > base::Time::Now()) {
88 return false;
93 return true;
96 } // namespace
98 bool ChromeTracingDelegate::IsAllowedToBeginBackgroundScenario(
99 const content::BackgroundTracingConfig& config,
100 bool requires_anonymized_data) {
101 if (!ProfileAllowsScenario(config, PROFILE_NOT_REQUIRED))
102 return false;
104 if (requires_anonymized_data && chrome::IsOffTheRecordSessionActive())
105 return false;
107 return true;
110 bool ChromeTracingDelegate::IsAllowedToEndBackgroundScenario(
111 const content::BackgroundTracingConfig& config,
112 bool requires_anonymized_data) {
113 if (requires_anonymized_data && incognito_launched_)
114 return false;
116 if (!ProfileAllowsScenario(config, PROFILE_REQUIRED))
117 return false;
119 if (config.tracing_mode() == content::BackgroundTracingConfig::PREEMPTIVE) {
120 PrefService* local_state = g_browser_process->local_state();
121 DCHECK(local_state);
122 local_state->SetInt64(prefs::kBackgroundTracingLastUpload,
123 base::Time::Now().ToInternalValue());
125 // We make sure we've persisted the value in case finalizing the tracing
126 // causes a crash.
127 local_state->CommitPendingWrite();
130 return true;