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/devtools_ui.h" | 5 #include "chrome/browser/ui/webui/devtools_ui.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "chrome/browser/net/chrome_url_request_context.h" | 12 #include "chrome/browser/net/chrome_url_request_context.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 14 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
15 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/devtools_client_host.h" | 17 #include "content/public/browser/devtools_client_host.h" |
18 #include "content/public/browser/devtools_http_handler.h" | 18 #include "content/public/browser/devtools_http_handler.h" |
19 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
| 20 #include "content/public/browser/url_data_source_delegate.h" |
20 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
21 #include "content/public/browser/web_ui.h" | 22 #include "content/public/browser/web_ui.h" |
22 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
23 | 24 |
24 using content::BrowserThread; | 25 using content::BrowserThread; |
25 using content::WebContents; | 26 using content::WebContents; |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 std::string PathWithoutParams(const std::string& path) { | 30 std::string PathWithoutParams(const std::string& path) { |
30 return GURL(std::string("chrome-devtools://devtools/") + path) | 31 return GURL(std::string("chrome-devtools://devtools/") + path) |
31 .path().substr(1); | 32 .path().substr(1); |
32 } | 33 } |
33 | 34 |
34 } // namespace | 35 } // namespace |
35 | 36 |
36 class DevToolsDataSource : public ChromeURLDataManager::DataSource { | 37 class DevToolsDataSource : public content::URLDataSourceDelegate { |
37 public: | 38 public: |
38 DevToolsDataSource(); | 39 DevToolsDataSource(); |
39 | 40 |
| 41 // content::URLDataSourceDelegate implementation. |
| 42 virtual std::string GetSource() OVERRIDE; |
40 virtual void StartDataRequest(const std::string& path, | 43 virtual void StartDataRequest(const std::string& path, |
41 bool is_incognito, | 44 bool is_incognito, |
42 int request_id); | 45 int request_id) OVERRIDE; |
43 virtual std::string GetMimeType(const std::string& path) const; | 46 virtual std::string GetMimeType(const std::string& path) const OVERRIDE; |
44 | 47 |
45 private: | 48 private: |
46 ~DevToolsDataSource() {} | 49 ~DevToolsDataSource() {} |
47 DISALLOW_COPY_AND_ASSIGN(DevToolsDataSource); | 50 DISALLOW_COPY_AND_ASSIGN(DevToolsDataSource); |
48 }; | 51 }; |
49 | 52 |
50 | 53 |
51 DevToolsDataSource::DevToolsDataSource() | 54 DevToolsDataSource::DevToolsDataSource() { |
52 : DataSource(chrome::kChromeUIDevToolsHost, NULL) { | 55 } |
| 56 |
| 57 std::string DevToolsDataSource::GetSource() { |
| 58 return chrome::kChromeUIDevToolsHost; |
53 } | 59 } |
54 | 60 |
55 void DevToolsDataSource::StartDataRequest(const std::string& path, | 61 void DevToolsDataSource::StartDataRequest(const std::string& path, |
56 bool is_incognito, | 62 bool is_incognito, |
57 int request_id) { | 63 int request_id) { |
58 std::string filename = PathWithoutParams(path); | 64 std::string filename = PathWithoutParams(path); |
59 | 65 |
60 | 66 |
61 int resource_id = | 67 int resource_id = |
62 content::DevToolsHttpHandler::GetFrontendResourceId(filename); | 68 content::DevToolsHttpHandler::GetFrontendResourceId(filename); |
63 | 69 |
64 DLOG_IF(WARNING, -1 == resource_id) << "Unable to find dev tool resource: " | 70 DLOG_IF(WARNING, -1 == resource_id) << "Unable to find dev tool resource: " |
65 << filename << ". If you compiled with debug_devtools=1, try running" | 71 << filename << ". If you compiled with debug_devtools=1, try running" |
66 " with --debug-devtools."; | 72 " with --debug-devtools."; |
67 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 73 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
68 scoped_refptr<base::RefCountedStaticMemory> bytes(rb.LoadDataResourceBytes( | 74 scoped_refptr<base::RefCountedStaticMemory> bytes(rb.LoadDataResourceBytes( |
69 resource_id)); | 75 resource_id)); |
70 SendResponse(request_id, bytes); | 76 url_data_source()->SendResponse(request_id, bytes); |
71 } | 77 } |
72 | 78 |
73 std::string DevToolsDataSource::GetMimeType(const std::string& path) const { | 79 std::string DevToolsDataSource::GetMimeType(const std::string& path) const { |
74 std::string filename = PathWithoutParams(path); | 80 std::string filename = PathWithoutParams(path); |
75 if (EndsWith(filename, ".html", false)) { | 81 if (EndsWith(filename, ".html", false)) { |
76 return "text/html"; | 82 return "text/html"; |
77 } else if (EndsWith(filename, ".css", false)) { | 83 } else if (EndsWith(filename, ".css", false)) { |
78 return "text/css"; | 84 return "text/css"; |
79 } else if (EndsWith(filename, ".js", false)) { | 85 } else if (EndsWith(filename, ".js", false)) { |
80 return "application/javascript"; | 86 return "application/javascript"; |
81 } else if (EndsWith(filename, ".png", false)) { | 87 } else if (EndsWith(filename, ".png", false)) { |
82 return "image/png"; | 88 return "image/png"; |
83 } else if (EndsWith(filename, ".gif", false)) { | 89 } else if (EndsWith(filename, ".gif", false)) { |
84 return "image/gif"; | 90 return "image/gif"; |
85 } | 91 } |
86 NOTREACHED(); | 92 NOTREACHED(); |
87 return "text/plain"; | 93 return "text/plain"; |
88 } | 94 } |
89 | 95 |
90 // static | 96 // static |
91 void DevToolsUI::RegisterDevToolsDataSource(Profile* profile) { | 97 void DevToolsUI::RegisterDevToolsDataSource(Profile* profile) { |
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
93 static bool registered = false; | 99 static bool registered = false; |
94 if (!registered) { | 100 if (!registered) { |
95 DevToolsDataSource* data_source = new DevToolsDataSource(); | 101 ChromeURLDataManager::AddDataSource(profile, new DevToolsDataSource); |
96 ChromeURLDataManager::AddDataSource(profile, data_source); | |
97 registered = true; | 102 registered = true; |
98 } | 103 } |
99 } | 104 } |
100 | 105 |
101 DevToolsUI::DevToolsUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 106 DevToolsUI::DevToolsUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
102 DevToolsDataSource* data_source = new DevToolsDataSource(); | 107 ChromeURLDataManager::AddDataSource( |
103 Profile* profile = Profile::FromWebUI(web_ui); | 108 Profile::FromWebUI(web_ui), new DevToolsDataSource); |
104 ChromeURLDataManager::AddDataSource(profile, data_source); | |
105 } | 109 } |
106 | 110 |
107 void DevToolsUI::RenderViewCreated( | 111 void DevToolsUI::RenderViewCreated( |
108 content::RenderViewHost* render_view_host) { | 112 content::RenderViewHost* render_view_host) { |
109 content::DevToolsClientHost::SetupDevToolsFrontendClient(render_view_host); | 113 content::DevToolsClientHost::SetupDevToolsFrontendClient(render_view_host); |
110 } | 114 } |
OLD | NEW |