Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / webui / profiler_ui.cc
bloba16c05f1e08aa201d5978c023dc86a058a555592
1 // Copyright (c) 2012 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/ui/webui/profiler_ui.h"
7 #include <string>
9 // When testing the javacript code, it is cumbersome to have to keep
10 // re-building the resouces package and reloading the browser. To solve
11 // this, enable the following flag to read the webapp's source files
12 // directly off disk, so all you have to do is refresh the page to
13 // test the modifications.
14 // #define USE_SOURCE_FILES_DIRECTLY
16 #include "base/bind.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/strings/string_util.h"
19 #include "base/tracked_objects.h"
20 #include "base/values.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/task_profiler/task_profiler_data_serializer.h"
23 #include "chrome/common/url_constants.h"
24 #include "components/metrics/profiler/tracking_synchronizer.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/url_data_source.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_ui.h"
29 #include "content/public/browser/web_ui_data_source.h"
30 #include "content/public/browser/web_ui_message_handler.h"
31 #include "grit/browser_resources.h"
33 #ifdef USE_SOURCE_FILES_DIRECTLY
34 #include "base/base_paths.h"
35 #include "base/files/file_util.h"
36 #include "base/memory/ref_counted_memory.h"
37 #include "base/path_service.h"
38 #endif // USE_SOURCE_FILES_DIRECTLY
40 using content::BrowserThread;
41 using content::WebContents;
42 using content::WebUIMessageHandler;
43 using metrics::TrackingSynchronizer;
45 namespace {
47 #ifdef USE_SOURCE_FILES_DIRECTLY
49 class ProfilerWebUIDataSource : public content::URLDataSource {
50 public:
51 ProfilerWebUIDataSource() {
54 protected:
55 // content::URLDataSource implementation.
56 virtual std::string GetSource() override {
57 return chrome::kChromeUIProfilerHost;
60 virtual std::string GetMimeType(const std::string& path) const override {
61 if (EndsWith(path, ".js", false))
62 return "application/javascript";
63 return "text/html";
66 virtual void StartDataRequest(
67 const std::string& path,
68 bool is_incognito,
69 const content::URLDataSource::GotDataCallback& callback) override {
70 base::FilePath base_path;
71 PathService::Get(base::DIR_SOURCE_ROOT, &base_path);
72 base_path = base_path.AppendASCII("chrome");
73 base_path = base_path.AppendASCII("browser");
74 base_path = base_path.AppendASCII("resources");
75 base_path = base_path.AppendASCII("profiler");
77 // If no resource was specified, default to profiler.html.
78 std::string filename = path.empty() ? "profiler.html" : path;
80 base::FilePath file_path;
81 file_path = base_path.AppendASCII(filename);
83 // Read the file synchronously and send it as the response.
84 base::ThreadRestrictions::ScopedAllowIO allow;
85 std::string file_contents;
86 if (!base::ReadFileToString(file_path, &file_contents))
87 LOG(ERROR) << "Couldn't read file: " << file_path.value();
88 scoped_refptr<base::RefCountedString> response =
89 new base::RefCountedString();
90 response->data() = file_contents;
91 callback.Run(response);
94 private:
95 DISALLOW_COPY_AND_ASSIGN(ProfilerWebUIDataSource);
98 #else // USE_SOURCE_FILES_DIRECTLY
100 content::WebUIDataSource* CreateProfilerHTMLSource() {
101 content::WebUIDataSource* source =
102 content::WebUIDataSource::Create(chrome::kChromeUIProfilerHost);
104 source->SetJsonPath("strings.js");
105 source->AddResourcePath("profiler.js", IDR_PROFILER_JS);
106 source->SetDefaultResource(IDR_PROFILER_HTML);
107 return source;
110 #endif
112 // This class receives javascript messages from the renderer.
113 // Note that the WebUI infrastructure runs on the UI thread, therefore all of
114 // this class's methods are expected to run on the UI thread.
115 class ProfilerMessageHandler : public WebUIMessageHandler {
116 public:
117 ProfilerMessageHandler() {}
119 // WebUIMessageHandler implementation.
120 void RegisterMessages() override;
122 // Messages.
123 void OnGetData(const base::ListValue* list);
125 private:
126 DISALLOW_COPY_AND_ASSIGN(ProfilerMessageHandler);
129 void ProfilerMessageHandler::RegisterMessages() {
130 DCHECK_CURRENTLY_ON(BrowserThread::UI);
132 web_ui()->RegisterMessageCallback("getData",
133 base::Bind(&ProfilerMessageHandler::OnGetData, base::Unretained(this)));
136 void ProfilerMessageHandler::OnGetData(const base::ListValue* list) {
137 ProfilerUI* profiler_ui = static_cast<ProfilerUI*>(web_ui()->GetController());
138 profiler_ui->GetData();
141 } // namespace
143 ProfilerUI::ProfilerUI(content::WebUI* web_ui)
144 : WebUIController(web_ui),
145 weak_ptr_factory_(this) {
146 web_ui->AddMessageHandler(new ProfilerMessageHandler());
148 // Set up the chrome://profiler/ source.
149 Profile* profile = Profile::FromWebUI(web_ui);
150 #if defined(USE_SOURCE_FILES_DIRECTLY)
151 content::URLDataSource::Add(profile, new ProfilerWebUIDataSource);
152 #else
153 content::WebUIDataSource::Add(profile, CreateProfilerHTMLSource());
154 #endif
157 ProfilerUI::~ProfilerUI() {
160 void ProfilerUI::GetData() {
161 TrackingSynchronizer::FetchProfilerDataAsynchronously(
162 weak_ptr_factory_.GetWeakPtr());
165 void ProfilerUI::ReceivedProfilerData(
166 const tracked_objects::ProcessDataPhaseSnapshot& process_data_phase,
167 base::ProcessId process_id,
168 content::ProcessType process_type,
169 int profiling_phase,
170 base::TimeDelta phase_start,
171 base::TimeDelta phase_finish,
172 const metrics::ProfilerEvents& past_events) {
173 // Serialize the data to JSON.
174 base::DictionaryValue json_data;
175 task_profiler::TaskProfilerDataSerializer::ToValue(
176 process_data_phase, process_id, process_type, &json_data);
178 // Send the data to the renderer.
179 web_ui()->CallJavascriptFunction("g_browserBridge.receivedData", json_data);