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/navigation_tracing.h"
8 #include "base/command_line.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/tracing/crash_service_uploader.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "components/tracing/tracing_switches.h"
13 #include "content/public/browser/background_tracing_manager.h"
14 #include "content/public/browser/background_tracing_reactive_config.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/render_frame_host.h"
18 DEFINE_WEB_CONTENTS_USER_DATA_KEY(tracing::NavigationTracingObserver
);
20 using content::RenderFrameHost
;
26 const char kNavigationTracingConfig
[] = "navigation-config";
28 void OnUploadComplete(TraceCrashServiceUploader
* uploader
,
29 const base::Closure
& done_callback
,
31 const std::string
& feedback
) {
32 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
36 void UploadCallback(const scoped_refptr
<base::RefCountedString
>& file_contents
,
37 scoped_ptr
<base::DictionaryValue
> metadata
,
38 base::Closure callback
) {
39 TraceCrashServiceUploader
* uploader
= new TraceCrashServiceUploader(
40 g_browser_process
->system_request_context());
43 file_contents
->data(), content::TraceUploader::UNCOMPRESSED_UPLOAD
,
44 metadata
.Pass(), content::TraceUploader::UploadProgressCallback(),
45 base::Bind(&OnUploadComplete
, base::Owned(uploader
), callback
));
50 void SetupNavigationTracing() {
51 const base::CommandLine
& command_line
=
52 *base::CommandLine::ForCurrentProcess();
53 if (!command_line
.HasSwitch(switches::kEnableNavigationTracing
) ||
54 !command_line
.HasSwitch(switches::kTraceUploadURL
)) {
59 scoped_ptr
<content::BackgroundTracingReactiveConfig
> config
;
60 config
.reset(new content::BackgroundTracingReactiveConfig());
62 content::BackgroundTracingReactiveConfig::TracingRule rule
;
63 rule
.type
= content::BackgroundTracingReactiveConfig::
64 TRACE_FOR_10S_OR_TRIGGER_OR_FULL
;
65 rule
.trigger_name
= kNavigationTracingConfig
;
66 rule
.category_preset
=
67 content::BackgroundTracingConfig::CategoryPreset::BENCHMARK_DEEP
;
68 config
->configs
.push_back(rule
);
70 content::BackgroundTracingManager::GetInstance()->SetActiveScenario(
71 config
.Pass(), base::Bind(&UploadCallback
),
72 content::BackgroundTracingManager::NO_DATA_FILTERING
);
75 NavigationTracingObserver::NavigationTracingObserver(
76 content::WebContents
* web_contents
)
77 : content::WebContentsObserver(web_contents
) {
78 if (navigation_handle
== -1) {
80 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
81 kNavigationTracingConfig
);
85 NavigationTracingObserver::~NavigationTracingObserver() {
88 void NavigationTracingObserver::DidStartProvisionalLoadForFrame(
89 content::RenderFrameHost
* render_frame_host
,
90 const GURL
& validated_url
,
92 bool is_iframe_srcdoc
) {
93 if (!render_frame_host
->GetParent() && !is_error_page
) {
94 content::BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
96 content::BackgroundTracingManager::StartedFinalizingCallback());
100 content::BackgroundTracingManager::TriggerHandle
101 NavigationTracingObserver::navigation_handle
= -1;
103 } // namespace tracing