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_config.h"
14 #include "content/public/browser/background_tracing_manager.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 base::DictionaryValue dict
;
60 dict
.SetString("mode", "REACTIVE_TRACING_MODE");
62 scoped_ptr
<base::ListValue
> rules_list(new base::ListValue());
64 scoped_ptr
<base::DictionaryValue
> rules_dict(new base::DictionaryValue());
65 rules_dict
->SetString("rule", "TRACE_ON_NAVIGATION_UNTIL_TRIGGER_OR_FULL");
66 rules_dict
->SetString("trigger_name", kNavigationTracingConfig
);
67 rules_dict
->SetString("category", "BENCHMARK_DEEP");
68 rules_list
->Append(rules_dict
.Pass());
70 dict
.Set("configs", rules_list
.Pass());
72 scoped_ptr
<content::BackgroundTracingConfig
> config(
73 content::BackgroundTracingConfig::FromDict(&dict
));
76 content::BackgroundTracingManager::GetInstance()->SetActiveScenario(
77 config
.Pass(), base::Bind(&UploadCallback
),
78 content::BackgroundTracingManager::NO_DATA_FILTERING
);
81 NavigationTracingObserver::NavigationTracingObserver(
82 content::WebContents
* web_contents
)
83 : content::WebContentsObserver(web_contents
) {
84 if (navigation_handle
== -1) {
86 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
87 kNavigationTracingConfig
);
91 NavigationTracingObserver::~NavigationTracingObserver() {
94 void NavigationTracingObserver::DidStartProvisionalLoadForFrame(
95 content::RenderFrameHost
* render_frame_host
,
96 const GURL
& validated_url
,
98 bool is_iframe_srcdoc
) {
99 if (!render_frame_host
->GetParent() && !is_error_page
) {
100 content::BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
102 content::BackgroundTracingManager::StartedFinalizingCallback());
106 content::BackgroundTracingManager::TriggerHandle
107 NavigationTracingObserver::navigation_handle
= -1;
109 } // namespace tracing