1 // Copyright (c) 2009 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_manager.h" 6 7 #include "base/message_loop.h" 8 #include "base/string_util.h" 9 #include "base/task.h" 10 #include "chrome/browser/extensions/extension_devtools_bridge.h" 11 #include "chrome/browser/extensions/extension_devtools_events.h" 12 13 ExtensionDevToolsManager::ExtensionDevToolsManager(Profile* profile) 14 : profile_(profile), 15 ui_loop_(NULL) { 16 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); 17 ui_loop_ = MessageLoop::current(); 18 } 19 20 ExtensionDevToolsManager::~ExtensionDevToolsManager() { 21 } 22 23 void ExtensionDevToolsManager::AddEventListener(const std::string& event_name, 24 int render_process_id) { 25 int tab_id; 26 if (ExtensionDevToolsEvents::IsDevToolsEventName(event_name, &tab_id)) { 27 // Add the renderer process ID to the set of processes interested 28 // in this tab. 29 tab_id_to_render_process_ids_[tab_id].insert(render_process_id); 30 if (tab_id_to_bridge_.count(tab_id) == 0) { 31 // Create a new bridge for this tab if there isn't one already. 32 linked_ptr<ExtensionDevToolsBridge> bridge( 33 new ExtensionDevToolsBridge(tab_id, profile_)); 34 if (bridge->RegisterAsDevToolsClientHost()) { 35 tab_id_to_bridge_[tab_id] = bridge; 36 } 37 } 38 } 39 } 40 41 void ExtensionDevToolsManager::RemoveEventListener( 42 const std::string& event_name, 43 int render_process_id) { 44 int tab_id; 45 if (ExtensionDevToolsEvents::IsDevToolsEventName(event_name, &tab_id)) { 46 std::map<int, std::set<int> >::iterator it = 47 tab_id_to_render_process_ids_.find(tab_id); 48 if (it != tab_id_to_render_process_ids_.end()) { 49 // Remove the process from the set of processes interested in this tab. 50 it->second.erase(render_process_id); 51 if (it->second.empty()) { 52 // No renderers have registered listeners for this tab, so kill the 53 // bridge if there is one. 54 if (tab_id_to_bridge_.count(tab_id) != 0) { 55 linked_ptr<ExtensionDevToolsBridge> bridge(tab_id_to_bridge_[tab_id]); 56 bridge->UnregisterAsDevToolsClientHost(); 57 tab_id_to_bridge_.erase(tab_id); 58 } 59 } 60 } 61 } 62 } 63 64 void ExtensionDevToolsManager::BridgeClosingForTab(int tab_id) { 65 if (tab_id_to_bridge_.count(tab_id) != 0) { 66 linked_ptr<ExtensionDevToolsBridge> bridge(tab_id_to_bridge_[tab_id]); 67 bridge->UnregisterAsDevToolsClientHost(); 68 tab_id_to_bridge_.erase(tab_id); 69 } 70 tab_id_to_render_process_ids_.erase(tab_id); 71 } 72 73