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