OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/string_util.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
7 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
| 11 #include "chrome/common/url_constants.h" |
8 #include "content/browser/renderer_host/render_view_host.h" | 12 #include "content/browser/renderer_host/render_view_host.h" |
| 13 #include "content/browser/tab_contents/tab_contents.h" |
| 14 #include "grit/devtools_resources_map.h" |
| 15 #include "ui/base/resource/resource_bundle.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 std::string PathWithoutParams(const std::string& path) { |
| 20 return GURL(std::string("chrome-devtools://devtools/") + path) |
| 21 .path().substr(1); |
| 22 } |
| 23 |
| 24 } |
| 25 |
| 26 class DevToolsDataSource : public ChromeURLDataManager::DataSource { |
| 27 public: |
| 28 DevToolsDataSource(); |
| 29 |
| 30 virtual void StartDataRequest(const std::string& path, |
| 31 bool is_off_the_record, |
| 32 int request_id); |
| 33 virtual std::string GetMimeType(const std::string& path) const; |
| 34 |
| 35 private: |
| 36 ~DevToolsDataSource() {} |
| 37 DISALLOW_COPY_AND_ASSIGN(DevToolsDataSource); |
| 38 }; |
| 39 |
| 40 |
| 41 DevToolsDataSource::DevToolsDataSource() |
| 42 : DataSource(chrome::kChromeUIDevToolsHost, MessageLoop::current()) { |
| 43 } |
| 44 |
| 45 void DevToolsDataSource::StartDataRequest(const std::string& path, |
| 46 bool is_off_the_record, |
| 47 int request_id) { |
| 48 std::string filename = PathWithoutParams(path); |
| 49 |
| 50 int resource_id = -1; |
| 51 for (size_t i = 0; i < kDevtoolsResourcesSize; ++i) { |
| 52 if (filename == kDevtoolsResources[i].name) { |
| 53 resource_id = kDevtoolsResources[i].value; |
| 54 break; |
| 55 } |
| 56 } |
| 57 |
| 58 DLOG_IF(WARNING, -1 == resource_id) << "Unable to find dev tool resource: " |
| 59 << filename << ". If you compiled with debug_devtools=1, try running" |
| 60 " with --debug-devtools."; |
| 61 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 62 scoped_refptr<RefCountedStaticMemory> bytes(rb.LoadDataResourceBytes( |
| 63 resource_id)); |
| 64 SendResponse(request_id, bytes); |
| 65 } |
| 66 |
| 67 std::string DevToolsDataSource::GetMimeType(const std::string& path) const { |
| 68 std::string filename = PathWithoutParams(path); |
| 69 if (EndsWith(filename, ".html", false)) { |
| 70 return "text/html"; |
| 71 } else if (EndsWith(filename, ".css", false)) { |
| 72 return "text/css"; |
| 73 } else if (EndsWith(filename, ".js", false)) { |
| 74 return "application/javascript"; |
| 75 } else if (EndsWith(filename, ".png", false)) { |
| 76 return "image/png"; |
| 77 } else if (EndsWith(filename, ".gif", false)) { |
| 78 return "image/gif"; |
| 79 } |
| 80 NOTREACHED(); |
| 81 return "text/plain"; |
| 82 } |
| 83 |
9 | 84 |
10 DevToolsUI::DevToolsUI(TabContents* contents) : WebUI(contents) { | 85 DevToolsUI::DevToolsUI(TabContents* contents) : WebUI(contents) { |
| 86 DevToolsDataSource* data_source = new DevToolsDataSource(); |
| 87 contents->profile()->GetChromeURLDataManager()->AddDataSource(data_source); |
11 } | 88 } |
12 | 89 |
13 void DevToolsUI::RenderViewCreated(RenderViewHost* render_view_host) { | 90 void DevToolsUI::RenderViewCreated(RenderViewHost* render_view_host) { |
14 render_view_host->Send(new ViewMsg_SetupDevToolsClient( | 91 render_view_host->Send(new ViewMsg_SetupDevToolsClient( |
15 render_view_host->routing_id())); | 92 render_view_host->routing_id())); |
16 } | 93 } |
OLD | NEW |