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
));
52 bool ChromeTracingDelegate::IsAllowedToBeginBackgroundScenario(
53 const content::BackgroundTracingConfig
& config
,
54 bool requires_anonymized_data
) {
55 Profile
* profile
= g_browser_process
->profile_manager()
56 ->GetLastUsedProfile()
57 ->GetOriginalProfile();
59 // Safeguard, in case background tracing is responsible for a crash on
61 if (profile
->GetLastSessionExitType() == Profile::EXIT_CRASHED
)
64 if (requires_anonymized_data
&& chrome::IsOffTheRecordSessionActive())
68 content::BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE
) {
69 PrefService
* local_state
= g_browser_process
->local_state();
71 const base::Time last_upload_time
= base::Time::FromInternalValue(
72 local_state
->GetInt64(prefs::kBackgroundTracingLastUpload
));
73 if (!last_upload_time
.is_null()) {
74 base::Time computed_next_allowed_time
=
75 last_upload_time
+ base::TimeDelta::FromDays(kMinDaysUntilNextUpload
);
76 if (computed_next_allowed_time
> base::Time::Now()) {
85 bool ChromeTracingDelegate::IsAllowedToEndBackgroundScenario(
86 const content::BackgroundTracingConfig
& config
,
87 bool requires_anonymized_data
) {
88 if (requires_anonymized_data
&& incognito_launched_
)
92 content::BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE
) {
93 PrefService
* local_state
= g_browser_process
->local_state();
95 local_state
->SetInt64(prefs::kBackgroundTracingLastUpload
,
96 base::Time::Now().ToInternalValue());
98 // We make sure we've persisted the value in case finalizing the tracing
100 local_state
->CommitPendingWrite();