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/extensions/extension_devtools_bridge.h" 6 7 #include "base/json/json_writer.h" 8 #include "base/message_loop.h" 9 #include "base/string_util.h" 10 #include "base/stringprintf.h" 11 #include "base/values.h" 12 #include "chrome/browser/debugger/devtools_manager.h" 13 #include "chrome/browser/extensions/extension_devtools_events.h" 14 #include "chrome/browser/extensions/extension_devtools_manager.h" 15 #include "chrome/browser/extensions/extension_event_router.h" 16 #include "chrome/browser/extensions/extension_tabs_module.h" 17 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 19 #include "chrome/common/devtools_messages.h" 20 #include "content/browser/tab_contents/tab_contents.h" 21 22 ExtensionDevToolsBridge::ExtensionDevToolsBridge(int tab_id, 23 Profile* profile) 24 : tab_id_(tab_id), 25 profile_(profile), 26 on_page_event_name_( 27 ExtensionDevToolsEvents::OnPageEventNameForTab(tab_id)), 28 on_tab_close_event_name_( 29 ExtensionDevToolsEvents::OnTabCloseEventNameForTab(tab_id)) { 30 extension_devtools_manager_ = profile_->GetExtensionDevToolsManager(); 31 DCHECK(extension_devtools_manager_.get()); 32 } 33 34 ExtensionDevToolsBridge::~ExtensionDevToolsBridge() { 35 } 36 37 static std::string FormatDevToolsMessage(int id, const std::string& method) { 38 DictionaryValue message; 39 message.SetInteger("id", id); 40 message.SetString("method", method); 41 42 std::string json; 43 base::JSONWriter::Write(&message, false, &json); 44 return json; 45 } 46 47 bool ExtensionDevToolsBridge::RegisterAsDevToolsClientHost() { 48 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); 49 50 Browser* browser; 51 TabStripModel* tab_strip; 52 TabContentsWrapper* contents; 53 int tab_index; 54 if (ExtensionTabUtil::GetTabById(tab_id_, profile_, true, 55 &browser, &tab_strip, 56 &contents, &tab_index)) { 57 DevToolsManager* devtools_manager = DevToolsManager::GetInstance(); 58 if (devtools_manager->GetDevToolsClientHostFor(contents-> 59 render_view_host()) != NULL) 60 return false; 61 62 devtools_manager->RegisterDevToolsClientHostFor( 63 contents->render_view_host(), this); 64 65 // Following messages depend on inspector protocol that is not yet 66 // finalized. 67 68 // 1. Report front-end is loaded. 69 devtools_manager->ForwardToDevToolsAgent( 70 this, 71 DevToolsAgentMsg_FrontendLoaded()); 72 73 // 2. Start timeline profiler. 74 devtools_manager->ForwardToDevToolsAgent( 75 this, 76 DevToolsAgentMsg_DispatchOnInspectorBackend( 77 FormatDevToolsMessage(2, "Timeline.start"))); 78 79 // 3. Enable network resource tracking. 80 devtools_manager->ForwardToDevToolsAgent( 81 this, 82 DevToolsAgentMsg_DispatchOnInspectorBackend( 83 FormatDevToolsMessage(3, "Network.enable"))); 84 85 return true; 86 } 87 return false; 88 } 89 90 void ExtensionDevToolsBridge::UnregisterAsDevToolsClientHost() { 91 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); 92 93 NotifyCloseListener(); 94 } 95 96 // If the tab we are looking at is going away then we fire a closing event at 97 // the extension. 98 void ExtensionDevToolsBridge::InspectedTabClosing() { 99 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); 100 101 // TODO(knorton): Remove this event in favor of the standard tabs.onRemoved 102 // event in extensions. 103 std::string json("[{}]"); 104 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( 105 on_tab_close_event_name_, json, profile_, GURL()); 106 107 // This may result in this object being destroyed. 108 extension_devtools_manager_->BridgeClosingForTab(tab_id_); 109 } 110 111 void ExtensionDevToolsBridge::SendMessageToClient(const IPC::Message& msg) { 112 IPC_BEGIN_MESSAGE_MAP(ExtensionDevToolsBridge, msg) 113 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, 114 OnDispatchOnInspectorFrontend); 115 IPC_MESSAGE_UNHANDLED_ERROR() 116 IPC_END_MESSAGE_MAP() 117 } 118 119 void ExtensionDevToolsBridge::TabReplaced(TabContentsWrapper* new_tab) { 120 DCHECK_EQ(profile_, new_tab->profile()); 121 // We don't update the tab id as it needs to remain the same so that we can 122 // properly unregister. 123 } 124 125 void ExtensionDevToolsBridge::OnDispatchOnInspectorFrontend( 126 const std::string& data) { 127 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); 128 129 std::string json = base::StringPrintf("[%s]", data.c_str()); 130 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( 131 on_page_event_name_, json, profile_, GURL()); 132 } 133