1 // Copyright 2014 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.
5 // WebViewRendererState manages state data for WebView guest renderer processes.
7 // This class's data can be accessed via its methods from both the UI and IO
8 // threads, and uses locks to mediate this access. When making changes to this
9 // class, ensure that you avoid introducing any reentrant code in the methods,
10 // and that you always aquire the locks in the order |web_view_info_map_lock_|
11 // -> |web_view_partition_id_map_lock_| (if both are needed in one method).
13 #ifndef EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_RENDERER_STATE_H_
14 #define EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_RENDERER_STATE_H_
21 #include "base/memory/singleton.h"
23 namespace extensions
{
27 class WebViewRendererState
{
30 int embedder_process_id
;
32 int rules_registry_id
;
33 std::string partition_id
;
34 std::string owner_host
;
35 std::set
<int> content_script_ids
;
41 static WebViewRendererState
* GetInstance();
43 // Looks up the information for the embedder WebView for a RenderViewHost,
44 // given its process and view ID. Returns true and writes the information to
45 // |web_view_info| if found, otherwise returns false.
46 bool GetInfo(int guest_process_id
,
48 WebViewInfo
* web_view_info
) const;
50 // Looks up the information for the owner of a WebView guest process, given
51 // its process ID. Returns true and writes the info to |owner_process_id| and
52 // |owner_host| if found, otherwise returns false.
53 bool GetOwnerInfo(int guest_process_id
,
54 int* owner_process_id
,
55 std::string
* owner_host
) const;
57 // Looks up the partition ID for a WebView guest process, given its
58 // process ID. Returns true and writes the partition ID to |partition_id| if
59 // found, otherwise returns false.
60 bool GetPartitionID(int guest_process_id
, std::string
* partition_id
) const;
62 // Returns true if the renderer with process ID |render_process_id| is a
63 // WebView guest process.
64 bool IsGuest(int render_process_id
) const;
66 void AddContentScriptIDs(int embedder_process_id
,
68 const std::set
<int>& script_ids
);
69 void RemoveContentScriptIDs(int embedder_process_id
,
71 const std::set
<int>& script_ids
);
74 friend class WebViewGuest
;
75 friend struct base::DefaultSingletonTraits
<WebViewRendererState
>;
77 using RenderId
= std::pair
<int, int>;
78 using WebViewInfoMap
= std::map
<RenderId
, WebViewInfo
>;
80 struct WebViewPartitionInfo
{
82 std::string partition_id
;
83 WebViewPartitionInfo() {}
84 WebViewPartitionInfo(int count
, const std::string
& partition
)
85 : web_view_count(count
), partition_id(partition
) {}
88 using WebViewPartitionIDMap
= std::map
<int, WebViewPartitionInfo
>;
90 WebViewRendererState();
91 ~WebViewRendererState();
93 // Adds/removes a WebView guest render process to/from the set.
94 void AddGuest(int render_process_host_id
, int routing_id
,
95 const WebViewInfo
& web_view_info
);
96 void RemoveGuest(int render_process_host_id
, int routing_id
);
98 // Locks are used to mediate access to these maps from both the UI and IO
100 WebViewInfoMap web_view_info_map_
;
101 mutable base::Lock web_view_info_map_lock_
;
102 WebViewPartitionIDMap web_view_partition_id_map_
;
103 mutable base::Lock web_view_partition_id_map_lock_
;
105 DISALLOW_COPY_AND_ASSIGN(WebViewRendererState
);
108 } // namespace extensions
110 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_WEB_VIEW_WEB_VIEW_RENDERER_STATE_H_