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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/webui/devtools_ui.h" 6 7 #include "base/string_util.h" 8 #include "chrome/browser/net/chrome_url_request_context.h" 9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h" 11 #include "chrome/common/devtools_messages.h" 12 #include "chrome/common/url_constants.h" 13 #include "content/browser/browser_thread.h" 14 #include "content/browser/renderer_host/render_view_host.h" 15 #include "content/browser/tab_contents/tab_contents.h" 16 #include "grit/devtools_resources_map.h" 17 #include "ui/base/resource/resource_bundle.h" 18 19 namespace { 20 21 std::string PathWithoutParams(const std::string& path) { 22 return GURL(std::string("chrome-devtools://devtools/") + path) 23 .path().substr(1); 24 } 25 26 } 27 28 class DevToolsDataSource : public ChromeURLDataManager::DataSource { 29 public: 30 DevToolsDataSource(); 31 32 virtual void StartDataRequest(const std::string& path, 33 bool is_incognito, 34 int request_id); 35 virtual std::string GetMimeType(const std::string& path) const; 36 37 private: 38 ~DevToolsDataSource() {} 39 DISALLOW_COPY_AND_ASSIGN(DevToolsDataSource); 40 }; 41 42 43 DevToolsDataSource::DevToolsDataSource() 44 : DataSource(chrome::kChromeUIDevToolsHost, NULL) { 45 } 46 47 void DevToolsDataSource::StartDataRequest(const std::string& path, 48 bool is_incognito, 49 int request_id) { 50 std::string filename = PathWithoutParams(path); 51 52 int resource_id = -1; 53 for (size_t i = 0; i < kDevtoolsResourcesSize; ++i) { 54 if (filename == kDevtoolsResources[i].name) { 55 resource_id = kDevtoolsResources[i].value; 56 break; 57 } 58 } 59 60 DLOG_IF(WARNING, -1 == resource_id) << "Unable to find dev tool resource: " 61 << filename << ". If you compiled with debug_devtools=1, try running" 62 " with --debug-devtools."; 63 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 64 scoped_refptr<RefCountedStaticMemory> bytes(rb.LoadDataResourceBytes( 65 resource_id)); 66 SendResponse(request_id, bytes); 67 } 68 69 std::string DevToolsDataSource::GetMimeType(const std::string& path) const { 70 std::string filename = PathWithoutParams(path); 71 if (EndsWith(filename, ".html", false)) { 72 return "text/html"; 73 } else if (EndsWith(filename, ".css", false)) { 74 return "text/css"; 75 } else if (EndsWith(filename, ".js", false)) { 76 return "application/javascript"; 77 } else if (EndsWith(filename, ".png", false)) { 78 return "image/png"; 79 } else if (EndsWith(filename, ".gif", false)) { 80 return "image/gif"; 81 } 82 NOTREACHED(); 83 return "text/plain"; 84 } 85 86 // static 87 void DevToolsUI::RegisterDevToolsDataSource() { 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 89 static bool registered = false; 90 if (!registered) { 91 DevToolsDataSource* data_source = new DevToolsDataSource(); 92 ChromeURLRequestContext* context = static_cast<ChromeURLRequestContext*>( 93 Profile::GetDefaultRequestContext()->GetURLRequestContext()); 94 context->GetChromeURLDataManagerBackend()->AddDataSource(data_source); 95 registered = true; 96 } 97 } 98 99 DevToolsUI::DevToolsUI(TabContents* contents) : WebUI(contents) { 100 DevToolsDataSource* data_source = new DevToolsDataSource(); 101 contents->profile()->GetChromeURLDataManager()->AddDataSource(data_source); 102 } 103 104 void DevToolsUI::RenderViewCreated(RenderViewHost* render_view_host) { 105 render_view_host->Send(new DevToolsMsg_SetupDevToolsClient( 106 render_view_host->routing_id())); 107 } 108