Home | History | Annotate | Download | only in extensions
      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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_DEVTOOLS_MANAGER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_DEVTOOLS_MANAGER_H_
      7 #pragma once
      8 
      9 #include <map>
     10 #include <set>
     11 #include <string>
     12 
     13 #include "base/memory/linked_ptr.h"
     14 #include "base/memory/ref_counted.h"
     15 
     16 class ExtensionDevToolsBridge;
     17 class MessageLoop;
     18 class Profile;
     19 
     20 // This class manages the lifetimes of ExtensionDevToolsBridge objects.
     21 // The manager is owned by the Profile.
     22 //
     23 // The lifetime of an ExtensionDevToolsBridge object is determined by:
     24 //  * the existence of registered event handlers for the bridge's tab
     25 //  * the lifetime of the inspected tab
     26 //
     27 // The manager is alerted whenever an event listener is added or removed and
     28 // keeps track of the set of renderers with event listeners registered for each
     29 // tab.  A new bridge object is created for a tab when the first event listener
     30 // is registered on that tab.  A bridge object is destroyed when all event
     31 // listeners are removed, the inspected tab closes, or when the manager itself
     32 // is destroyed.
     33 
     34 class ExtensionDevToolsManager
     35     : public base::RefCountedThreadSafe<ExtensionDevToolsManager> {
     36  public:
     37   // UI thread only:
     38   explicit ExtensionDevToolsManager(Profile* profile);
     39 
     40   void AddEventListener(const std::string& event_name,
     41                         int render_process_id);
     42 
     43   void RemoveEventListener(const std::string& event_name,
     44                            int render_process_id);
     45 
     46   void BridgeClosingForTab(int tab_id);
     47 
     48  private:
     49   friend class base::RefCountedThreadSafe<ExtensionDevToolsManager>;
     50 
     51   ~ExtensionDevToolsManager();
     52 
     53   // Map of tab IDs to the ExtensionDevToolsBridge connected to the tab
     54   std::map<int, linked_ptr<ExtensionDevToolsBridge> > tab_id_to_bridge_;
     55 
     56   // Map of tab IDs to the set of render_process_ids that have registered
     57   // event handlers for the tab.
     58   std::map<int, std::set<int> > tab_id_to_render_process_ids_;
     59 
     60   Profile* profile_;
     61   MessageLoop* ui_loop_;
     62 
     63   DISALLOW_COPY_AND_ASSIGN(ExtensionDevToolsManager);
     64 };
     65 
     66 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_DEVTOOLS_MANAGER_H_
     67 
     68