Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / guest_view / web_view / web_view_renderer_state.h
blob7ee5ace4ab08dbf0bed020a29e71f3f1051adda8
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.
6 //
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_
16 #include <map>
17 #include <set>
18 #include <string>
19 #include <utility>
21 #include "base/memory/singleton.h"
23 namespace extensions {
25 class WebViewGuest;
27 class WebViewRendererState {
28 public:
29 struct WebViewInfo {
30 int embedder_process_id;
31 int instance_id;
32 int rules_registry_id;
33 std::string partition_id;
34 std::string owner_host;
35 std::set<int> content_script_ids;
37 WebViewInfo();
38 ~WebViewInfo();
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,
47 int guest_routing_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,
67 int view_instance_id,
68 const std::set<int>& script_ids);
69 void RemoveContentScriptIDs(int embedder_process_id,
70 int view_instance_id,
71 const std::set<int>& script_ids);
73 private:
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 {
81 int web_view_count;
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
99 // threads.
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_