| OLD | NEW |
| 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/tracked_objects.h" | 18 #include "base/tracked_objects.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "chrome/browser/metrics/tracking_synchronizer.h" | 20 #include "chrome/browser/metrics/tracking_synchronizer.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/task_profiler/task_profiler_data_serializer.h" | 22 #include "chrome/browser/task_profiler/task_profiler_data_serializer.h" |
| 23 #include "chrome/browser/ui/webui/chrome_url_data_manager.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/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/browser/web_ui.h" | 29 #include "content/public/browser/web_ui.h" |
| 29 #include "content/public/browser/web_ui_message_handler.h" | 30 #include "content/public/browser/web_ui_message_handler.h" |
| 30 #include "grit/browser_resources.h" | 31 #include "grit/browser_resources.h" |
| 31 #include "grit/generated_resources.h" | 32 #include "grit/generated_resources.h" |
| 32 | 33 |
| 33 #ifdef USE_SOURCE_FILES_DIRECTLY | 34 #ifdef USE_SOURCE_FILES_DIRECTLY |
| 34 #include "base/base_paths.h" | 35 #include "base/base_paths.h" |
| 35 #include "base/file_util.h" | 36 #include "base/file_util.h" |
| 36 #include "base/memory/ref_counted_memory.h" | 37 #include "base/memory/ref_counted_memory.h" |
| 37 #include "base/path_service.h" | 38 #include "base/path_service.h" |
| 38 #endif // USE_SOURCE_FILES_DIRECTLY | 39 #endif // USE_SOURCE_FILES_DIRECTLY |
| 39 | 40 |
| 40 using chrome_browser_metrics::TrackingSynchronizer; | 41 using chrome_browser_metrics::TrackingSynchronizer; |
| 41 using content::BrowserThread; | 42 using content::BrowserThread; |
| 42 using content::WebContents; | 43 using content::WebContents; |
| 43 using content::WebUIMessageHandler; | 44 using content::WebUIMessageHandler; |
| 44 | 45 |
| 45 namespace { | 46 namespace { |
| 46 | 47 |
| 47 #ifdef USE_SOURCE_FILES_DIRECTLY | 48 #ifdef USE_SOURCE_FILES_DIRECTLY |
| 48 | 49 |
| 49 class ProfilerWebUIDataSource : public ChromeURLDataManager::DataSource { | 50 class ProfilerWebUIDataSource : public content::URLDataSourceDelegate { |
| 50 public: | 51 public: |
| 51 ProfilerWebUIDataSource() | 52 ProfilerWebUIDataSource() { |
| 52 : DataSource(chrome::kChromeUIProfilerHost, MessageLoop::current()) { | |
| 53 } | 53 } |
| 54 | 54 |
| 55 protected: | 55 protected: |
| 56 // ChromeURLDataManager | 56 // content::URLDataSourceDelegate implementation. |
| 57 virtual std::string GetSource() OVERRIDE { |
| 58 return chrome::kChromeUIProfilerHost; |
| 59 } |
| 60 |
| 57 virtual std::string GetMimeType(const std::string& path) const OVERRIDE { | 61 virtual std::string GetMimeType(const std::string& path) const OVERRIDE { |
| 58 if (EndsWith(path, ".js", false)) | 62 if (EndsWith(path, ".js", false)) |
| 59 return "application/javascript"; | 63 return "application/javascript"; |
| 60 return "text/html"; | 64 return "text/html"; |
| 61 } | 65 } |
| 62 | 66 |
| 63 virtual void StartDataRequest(const std::string& path, | 67 virtual void StartDataRequest(const std::string& path, |
| 64 bool is_incognito, | 68 bool is_incognito, |
| 65 int request_id) OVERRIDE { | 69 int request_id) OVERRIDE { |
| 66 FilePath base_path; | 70 FilePath base_path; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 77 file_path = base_path.AppendASCII(filename); | 81 file_path = base_path.AppendASCII(filename); |
| 78 | 82 |
| 79 // Read the file synchronously and send it as the response. | 83 // Read the file synchronously and send it as the response. |
| 80 base::ThreadRestrictions::ScopedAllowIO allow; | 84 base::ThreadRestrictions::ScopedAllowIO allow; |
| 81 std::string file_contents; | 85 std::string file_contents; |
| 82 if (!file_util::ReadFileToString(file_path, &file_contents)) | 86 if (!file_util::ReadFileToString(file_path, &file_contents)) |
| 83 LOG(ERROR) << "Couldn't read file: " << file_path.value(); | 87 LOG(ERROR) << "Couldn't read file: " << file_path.value(); |
| 84 scoped_refptr<base::RefCountedString> response = | 88 scoped_refptr<base::RefCountedString> response = |
| 85 new base::RefCountedString(); | 89 new base::RefCountedString(); |
| 86 response->data() = file_contents; | 90 response->data() = file_contents; |
| 87 SendResponse(request_id, response); | 91 url_data_source()->SendResponse(request_id, response); |
| 88 } | 92 } |
| 89 | 93 |
| 90 private: | 94 private: |
| 91 DISALLOW_COPY_AND_ASSIGN(ProfilerWebUIDataSource); | 95 DISALLOW_COPY_AND_ASSIGN(ProfilerWebUIDataSource); |
| 92 }; | 96 }; |
| 93 | 97 |
| 94 ChromeURLDataManager::DataSource* CreateProfilerHTMLSource() { | 98 ChromeURLDataManager::DataSource* CreateProfilerHTMLSource() { |
| 95 return new ProfilerWebUIDataSource(); | 99 return new ProfilerWebUIDataSource(); |
| 96 } | 100 } |
| 97 | 101 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 content::ProcessType process_type) { | 175 content::ProcessType process_type) { |
| 172 // Serialize the data to JSON. | 176 // Serialize the data to JSON. |
| 173 DictionaryValue json_data; | 177 DictionaryValue json_data; |
| 174 task_profiler::TaskProfilerDataSerializer::ToValue(profiler_data, | 178 task_profiler::TaskProfilerDataSerializer::ToValue(profiler_data, |
| 175 process_type, | 179 process_type, |
| 176 &json_data); | 180 &json_data); |
| 177 | 181 |
| 178 // Send the data to the renderer. | 182 // Send the data to the renderer. |
| 179 web_ui()->CallJavascriptFunction("g_browserBridge.receivedData", json_data); | 183 web_ui()->CallJavascriptFunction("g_browserBridge.receivedData", json_data); |
| 180 } | 184 } |
| OLD | NEW |