Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: chrome/browser/ui/webui/profiler_ui.cc

Issue 11881055: Simplify WebUI data sources. Currently WebUI data sources implement a URLDataSourceDelegate interfa… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix issue in about_ui exposed by cros tests Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/profiler_ui.h" 5 #include "chrome/browser/ui/webui/profiler_ui.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 // When testing the javacript code, it is cumbersome to have to keep 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 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 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 12 // directly off disk, so all you have to do is refresh the page to
13 // test the modifications. 13 // test the modifications.
14 // #define USE_SOURCE_FILES_DIRECTLY 14 // #define USE_SOURCE_FILES_DIRECTLY
15 15
16 #include "base/bind.h" 16 #include "base/bind.h"
17 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
18 #include "base/string_util.h"
18 #include "base/tracked_objects.h" 19 #include "base/tracked_objects.h"
19 #include "base/values.h" 20 #include "base/values.h"
20 #include "chrome/browser/metrics/tracking_synchronizer.h" 21 #include "chrome/browser/metrics/tracking_synchronizer.h"
21 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/task_profiler/task_profiler_data_serializer.h" 23 #include "chrome/browser/task_profiler/task_profiler_data_serializer.h"
23 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
24 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" 24 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h"
25 #include "chrome/common/url_constants.h" 25 #include "chrome/common/url_constants.h"
26 #include "content/public/browser/browser_thread.h" 26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/url_data_source_delegate.h" 27 #include "content/public/browser/url_data_source.h"
28 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
29 #include "content/public/browser/web_ui.h" 29 #include "content/public/browser/web_ui.h"
30 #include "content/public/browser/web_ui_message_handler.h" 30 #include "content/public/browser/web_ui_message_handler.h"
31 #include "grit/browser_resources.h" 31 #include "grit/browser_resources.h"
32 #include "grit/generated_resources.h" 32 #include "grit/generated_resources.h"
33 33
34 #ifdef USE_SOURCE_FILES_DIRECTLY 34 #ifdef USE_SOURCE_FILES_DIRECTLY
35 #include "base/base_paths.h" 35 #include "base/base_paths.h"
36 #include "base/file_util.h" 36 #include "base/file_util.h"
37 #include "base/memory/ref_counted_memory.h" 37 #include "base/memory/ref_counted_memory.h"
38 #include "base/path_service.h" 38 #include "base/path_service.h"
39 #endif // USE_SOURCE_FILES_DIRECTLY 39 #endif // USE_SOURCE_FILES_DIRECTLY
40 40
41 using chrome_browser_metrics::TrackingSynchronizer; 41 using chrome_browser_metrics::TrackingSynchronizer;
42 using content::BrowserThread; 42 using content::BrowserThread;
43 using content::WebContents; 43 using content::WebContents;
44 using content::WebUIMessageHandler; 44 using content::WebUIMessageHandler;
45 45
46 namespace { 46 namespace {
47 47
48 #ifdef USE_SOURCE_FILES_DIRECTLY 48 #ifdef USE_SOURCE_FILES_DIRECTLY
49 49
50 class ProfilerWebUIDataSource : public content::URLDataSourceDelegate { 50 class ProfilerWebUIDataSource : public content::URLDataSource {
51 public: 51 public:
52 ProfilerWebUIDataSource() { 52 ProfilerWebUIDataSource() {
53 } 53 }
54 54
55 protected: 55 protected:
56 // content::URLDataSourceDelegate implementation. 56 // content::URLDataSource implementation.
57 virtual std::string GetSource() OVERRIDE { 57 virtual std::string GetSource() OVERRIDE {
58 return chrome::kChromeUIProfilerHost; 58 return chrome::kChromeUIProfilerHost;
59 } 59 }
60 60
61 virtual std::string GetMimeType(const std::string& path) const OVERRIDE { 61 virtual std::string GetMimeType(const std::string& path) const OVERRIDE {
62 if (EndsWith(path, ".js", false)) 62 if (EndsWith(path, ".js", false))
63 return "application/javascript"; 63 return "application/javascript";
64 return "text/html"; 64 return "text/html";
65 } 65 }
66 66
67 virtual void StartDataRequest(const std::string& path, 67 virtual void StartDataRequest(
68 bool is_incognito, 68 const std::string& path,
69 int request_id) OVERRIDE { 69 bool is_incognito,
70 const content::URLDataSource::GotDataCallback& callback) OVERRIDE {
70 FilePath base_path; 71 FilePath base_path;
71 PathService::Get(base::DIR_SOURCE_ROOT, &base_path); 72 PathService::Get(base::DIR_SOURCE_ROOT, &base_path);
72 base_path = base_path.AppendASCII("chrome"); 73 base_path = base_path.AppendASCII("chrome");
73 base_path = base_path.AppendASCII("browser"); 74 base_path = base_path.AppendASCII("browser");
74 base_path = base_path.AppendASCII("resources"); 75 base_path = base_path.AppendASCII("resources");
75 base_path = base_path.AppendASCII("profiler"); 76 base_path = base_path.AppendASCII("profiler");
76 77
77 // If no resource was specified, default to profiler.html. 78 // If no resource was specified, default to profiler.html.
78 std::string filename = path.empty() ? "profiler.html" : path; 79 std::string filename = path.empty() ? "profiler.html" : path;
79 80
80 FilePath file_path; 81 FilePath file_path;
81 file_path = base_path.AppendASCII(filename); 82 file_path = base_path.AppendASCII(filename);
82 83
83 // Read the file synchronously and send it as the response. 84 // Read the file synchronously and send it as the response.
84 base::ThreadRestrictions::ScopedAllowIO allow; 85 base::ThreadRestrictions::ScopedAllowIO allow;
85 std::string file_contents; 86 std::string file_contents;
86 if (!file_util::ReadFileToString(file_path, &file_contents)) 87 if (!file_util::ReadFileToString(file_path, &file_contents))
87 LOG(ERROR) << "Couldn't read file: " << file_path.value(); 88 LOG(ERROR) << "Couldn't read file: " << file_path.value();
88 scoped_refptr<base::RefCountedString> response = 89 scoped_refptr<base::RefCountedString> response =
89 new base::RefCountedString(); 90 new base::RefCountedString();
90 response->data() = file_contents; 91 response->data() = file_contents;
91 url_data_source()->SendResponse(request_id, response); 92 callback.Run(response);
92 } 93 }
93 94
94 private: 95 private:
95 DISALLOW_COPY_AND_ASSIGN(ProfilerWebUIDataSource); 96 DISALLOW_COPY_AND_ASSIGN(ProfilerWebUIDataSource);
96 }; 97 };
97 98
98 ChromeURLDataManager::DataSource* CreateProfilerHTMLSource() {
99 return new ProfilerWebUIDataSource();
100 }
101
102 #else // USE_SOURCE_FILES_DIRECTLY 99 #else // USE_SOURCE_FILES_DIRECTLY
103 100
104 ChromeWebUIDataSource* CreateProfilerHTMLSource() { 101 ChromeWebUIDataSource* CreateProfilerHTMLSource() {
105 ChromeWebUIDataSource* source = 102 ChromeWebUIDataSource* source =
106 new ChromeWebUIDataSource(chrome::kChromeUIProfilerHost); 103 new ChromeWebUIDataSource(chrome::kChromeUIProfilerHost);
107 104
108 source->set_json_path("strings.js"); 105 source->set_json_path("strings.js");
109 source->add_resource_path("profiler.js", IDR_PROFILER_JS); 106 source->add_resource_path("profiler.js", IDR_PROFILER_JS);
110 source->set_default_resource(IDR_PROFILER_HTML); 107 source->set_default_resource(IDR_PROFILER_HTML);
111 return source; 108 return source;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 149
153 } // namespace 150 } // namespace
154 151
155 ProfilerUI::ProfilerUI(content::WebUI* web_ui) 152 ProfilerUI::ProfilerUI(content::WebUI* web_ui)
156 : WebUIController(web_ui), 153 : WebUIController(web_ui),
157 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { 154 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
158 web_ui->AddMessageHandler(new ProfilerMessageHandler()); 155 web_ui->AddMessageHandler(new ProfilerMessageHandler());
159 156
160 // Set up the chrome://profiler/ source. 157 // Set up the chrome://profiler/ source.
161 Profile* profile = Profile::FromWebUI(web_ui); 158 Profile* profile = Profile::FromWebUI(web_ui);
162 ChromeURLDataManager::AddDataSource(profile, CreateProfilerHTMLSource()); 159 #if defined(USE_SOURCE_FILES_DIRECTLY)
160 ChromeURLDataManager::AddDataSource(profile, new ProfilerWebUIDataSource);
161 #else
162 ChromeURLDataManager::AddDataSourceImpl(profile, CreateProfilerHTMLSource());
163 #endif
163 } 164 }
164 165
165 ProfilerUI::~ProfilerUI() { 166 ProfilerUI::~ProfilerUI() {
166 } 167 }
167 168
168 void ProfilerUI::GetData() { 169 void ProfilerUI::GetData() {
169 TrackingSynchronizer::FetchProfilerDataAsynchronously( 170 TrackingSynchronizer::FetchProfilerDataAsynchronously(
170 weak_ptr_factory_.GetWeakPtr()); 171 weak_ptr_factory_.GetWeakPtr());
171 } 172 }
172 173
173 void ProfilerUI::ReceivedProfilerData( 174 void ProfilerUI::ReceivedProfilerData(
174 const tracked_objects::ProcessDataSnapshot& profiler_data, 175 const tracked_objects::ProcessDataSnapshot& profiler_data,
175 content::ProcessType process_type) { 176 content::ProcessType process_type) {
176 // Serialize the data to JSON. 177 // Serialize the data to JSON.
177 DictionaryValue json_data; 178 DictionaryValue json_data;
178 task_profiler::TaskProfilerDataSerializer::ToValue(profiler_data, 179 task_profiler::TaskProfilerDataSerializer::ToValue(profiler_data,
179 process_type, 180 process_type,
180 &json_data); 181 &json_data);
181 182
182 // Send the data to the renderer. 183 // Send the data to the renderer.
183 web_ui()->CallJavascriptFunction("g_browserBridge.receivedData", json_data); 184 web_ui()->CallJavascriptFunction("g_browserBridge.receivedData", json_data);
184 } 185 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698