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