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