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 "content/public/browser/background_tracing_manager.h"
13 #include "content/public/browser/background_tracing_reactive_config.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/render_frame_host.h"
17 DEFINE_WEB_CONTENTS_USER_DATA_KEY(tracing::NavigationTracingObserver
);
19 using content::RenderFrameHost
;
25 const char kNavigationTracingConfig
[] = "navigation-config";
27 void OnUploadComplete(TraceCrashServiceUploader
* uploader
,
28 const base::Closure
& done_callback
,
30 const std::string
& feedback
) {
31 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
35 void UploadCallback(const scoped_refptr
<base::RefCountedString
>& file_contents
,
36 scoped_ptr
<base::DictionaryValue
> metadata
,
37 base::Closure callback
) {
38 TraceCrashServiceUploader
* uploader
= new TraceCrashServiceUploader(
39 g_browser_process
->system_request_context());
42 file_contents
->data(), content::TraceUploader::UNCOMPRESSED_UPLOAD
,
43 metadata
.Pass(), content::TraceUploader::UploadProgressCallback(),
44 base::Bind(&OnUploadComplete
, base::Owned(uploader
), callback
));
49 void SetupNavigationTracing() {
50 const base::CommandLine
& command_line
=
51 *base::CommandLine::ForCurrentProcess();
52 if (!command_line
.HasSwitch(switches::kEnableNavigationTracing
) ||
53 !command_line
.HasSwitch(switches::kTraceUploadURL
)) {
58 scoped_ptr
<content::BackgroundTracingReactiveConfig
> config
;
59 config
.reset(new content::BackgroundTracingReactiveConfig());
61 content::BackgroundTracingReactiveConfig::TracingRule rule
;
62 rule
.type
= content::BackgroundTracingReactiveConfig::
63 TRACE_FOR_10S_OR_TRIGGER_OR_FULL
;
64 rule
.trigger_name
= kNavigationTracingConfig
;
65 rule
.category_preset
=
66 content::BackgroundTracingConfig::CategoryPreset::BENCHMARK_DEEP
;
67 config
->configs
.push_back(rule
);
69 content::BackgroundTracingManager::GetInstance()->SetActiveScenario(
70 config
.Pass(), base::Bind(&UploadCallback
),
71 content::BackgroundTracingManager::NO_DATA_FILTERING
);
74 NavigationTracingObserver::NavigationTracingObserver(
75 content::WebContents
* web_contents
)
76 : content::WebContentsObserver(web_contents
) {
77 if (navigation_handle
== -1) {
79 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
80 kNavigationTracingConfig
);
84 NavigationTracingObserver::~NavigationTracingObserver() {
87 void NavigationTracingObserver::DidStartProvisionalLoadForFrame(
88 content::RenderFrameHost
* render_frame_host
,
89 const GURL
& validated_url
,
91 bool is_iframe_srcdoc
) {
92 if (!render_frame_host
->GetParent() && !is_error_page
) {
93 content::BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
95 content::BackgroundTracingManager::StartedFinalizingCallback());
99 content::BackgroundTracingManager::TriggerHandle
100 NavigationTracingObserver::navigation_handle
= -1;
102 } // namespace tracing