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 class WebViewGuest;
     17 
     18 namespace content {
     19 class ResourceRequestInfo;
     20 }
     21 
     22 // This class keeps track of renderer state for use on the IO thread. All
     23 // methods should be called on the IO thread except for Init and Shutdown.
     24 class ExtensionRendererState {
     25  public:
     26   struct WebViewInfo {
     27     int embedder_process_id;
     28     int instance_id;
     29     std::string partition_id;
     30     std::string embedder_extension_id;
     31   };
     32 
     33   static ExtensionRendererState* GetInstance();
     34 
     35   // These are called on the UI thread to start and stop listening to tab
     36   // notifications.
     37   void Init();
     38   void Shutdown();
     39 
     40   // Looks up the information for the embedder <webview> for a given render
     41   // view, if one exists. Called on the IO thread.
     42   bool GetWebViewInfo(int guest_process_id, int guest_routing_id,
     43                       WebViewInfo* webview_info);
     44 
     45   // Looks up the partition info for the embedder <webview> for a given guest
     46   // process. Called on the IO thread.
     47   bool GetWebViewPartitionID(int guest_process_id, std::string* partition_id);
     48 
     49   // Looks up the tab and window ID for a given request. Returns true if we have
     50   // the IDs in our map. Called on the IO thread.
     51   bool GetTabAndWindowId(
     52       const content::ResourceRequestInfo* info, int* tab_id, int* window_id);
     53 
     54   // Returns true if the given renderer is used by webviews.
     55   bool IsWebViewRenderer(int render_process_id);
     56 
     57  private:
     58   class RenderViewHostObserver;
     59   class TabObserver;
     60   friend class TabObserver;
     61   friend class WebViewGuest;
     62   friend struct DefaultSingletonTraits<ExtensionRendererState>;
     63 
     64   typedef std::pair<int, int> RenderId;
     65   typedef std::pair<int, int> TabAndWindowId;
     66   typedef std::map<RenderId, TabAndWindowId> TabAndWindowIdMap;
     67   typedef std::map<RenderId, WebViewInfo> WebViewInfoMap;
     68 
     69   struct WebViewPartitionInfo {
     70     int web_view_count;
     71     std::string partition_id;
     72     WebViewPartitionInfo() {}
     73     WebViewPartitionInfo(int count, std::string partition):
     74       web_view_count(count),
     75       partition_id(partition) {}
     76   };
     77 
     78   typedef std::map<int, WebViewPartitionInfo> WebViewPartitionIDMap;
     79 
     80   ExtensionRendererState();
     81   ~ExtensionRendererState();
     82 
     83   // Adds or removes a render view from our map.
     84   void SetTabAndWindowId(
     85       int render_process_host_id, int routing_id, int tab_id, int window_id);
     86   void ClearTabAndWindowId(
     87       int render_process_host_id, int routing_id);
     88 
     89   // Adds or removes a <webview> guest render process from the set.
     90   void AddWebView(int render_process_host_id, int routing_id,
     91                   const WebViewInfo& webview_info);
     92   void RemoveWebView(int render_process_host_id, int routing_id);
     93 
     94   TabObserver* observer_;
     95   TabAndWindowIdMap map_;
     96   WebViewInfoMap webview_info_map_;
     97   WebViewPartitionIDMap webview_partition_id_map_;
     98 
     99   DISALLOW_COPY_AND_ASSIGN(ExtensionRendererState);
    100 };
    101 
    102 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_RENDERER_STATE_H_
    103