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