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"
23 const int kMinDaysUntilNextUpload
= 7;
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
));
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();
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.
68 if (profile_permission
== PROFILE_REQUIRED
)
74 // Safeguard, in case background tracing is responsible for a crash on
76 if (profile
->GetLastSessionExitType() == Profile::EXIT_CRASHED
)
79 if (config
.tracing_mode() == content::BackgroundTracingConfig::PREEMPTIVE
) {
80 PrefService
* local_state
= g_browser_process
->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()) {
98 bool ChromeTracingDelegate::IsAllowedToBeginBackgroundScenario(
99 const content::BackgroundTracingConfig
& config
,
100 bool requires_anonymized_data
) {
101 if (!ProfileAllowsScenario(config
, PROFILE_NOT_REQUIRED
))
104 if (requires_anonymized_data
&& chrome::IsOffTheRecordSessionActive())
110 bool ChromeTracingDelegate::IsAllowedToEndBackgroundScenario(
111 const content::BackgroundTracingConfig
& config
,
112 bool requires_anonymized_data
) {
113 if (requires_anonymized_data
&& incognito_launched_
)
116 if (!ProfileAllowsScenario(config
, PROFILE_REQUIRED
))
119 if (config
.tracing_mode() == content::BackgroundTracingConfig::PREEMPTIVE
) {
120 PrefService
* local_state
= g_browser_process
->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
127 local_state
->CommitPendingWrite();