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_TAB_ID_MAP_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_ID_MAP_H_
7#pragma once
8
9#include <map>
10#include <utility>
11
12#include "base/basictypes.h"
13#include "base/memory/singleton.h"
14
15// This class keeps track of a map between renderer IDs and tab/window IDs, for
16// use on the IO thread. All methods should be called on the IO thread except
17// for Init and Shutdown.
18class ExtensionTabIdMap {
19 public:
20  static ExtensionTabIdMap* GetInstance();
21
22  // These are called on the UI thread to start and stop listening to tab
23  // notifications.
24  void Init();
25  void Shutdown();
26
27  // Looks up the tab and window ID for a given render view. Returns true
28  // if we have the IDs in our map. Called on the IO thread.
29  bool GetTabAndWindowId(
30      int render_process_host_id, int routing_id, int* tab_id, int* window_id);
31
32 private:
33  class TabObserver;
34  friend class TabObserver;
35  friend struct DefaultSingletonTraits<ExtensionTabIdMap>;
36
37  typedef std::pair<int, int> RenderId;
38  typedef std::pair<int, int> TabAndWindowId;
39  typedef std::map<RenderId, TabAndWindowId> TabAndWindowIdMap;
40
41  ExtensionTabIdMap();
42  ~ExtensionTabIdMap();
43
44  // Adds or removes a render view from our map.
45  void SetTabAndWindowId(
46      int render_process_host_id, int routing_id, int tab_id, int window_id);
47  void ClearTabAndWindowId(
48      int render_process_host_id, int routing_id);
49
50  TabObserver* observer_;
51  TabAndWindowIdMap map_;
52
53  DISALLOW_COPY_AND_ASSIGN(ExtensionTabIdMap);
54};
55
56#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_ID_MAP_H_
57