• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that can
3 // be found in the LICENSE file.
4 
5 #ifndef CEF_LIBCEF_BROWSER_BROWSER_INFO_H_
6 #define CEF_LIBCEF_BROWSER_BROWSER_INFO_H_
7 #pragma once
8 
9 #include <set>
10 #include <unordered_map>
11 
12 #include "include/internal/cef_ptr.h"
13 #include "libcef/common/values_impl.h"
14 
15 #include "base/callback.h"
16 #include "base/containers/unique_ptr_adapters.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/synchronization/lock.h"
20 #include "base/values.h"
21 
22 namespace content {
23 class RenderFrameHost;
24 }
25 
26 class CefBrowserHostBase;
27 class CefFrameHostImpl;
28 
29 // CefBrowserInfo is used to associate a browser ID and render view/process
30 // IDs with a particular CefBrowserHostBase. Render view/process IDs may change
31 // during the lifetime of a single CefBrowserHostBase.
32 //
33 // CefBrowserInfo objects are managed by CefBrowserInfoManager and should not be
34 // created directly.
35 class CefBrowserInfo : public base::RefCountedThreadSafe<CefBrowserInfo> {
36  public:
37   CefBrowserInfo(int browser_id,
38                  bool is_popup,
39                  bool is_windowless,
40                  CefRefPtr<CefDictionaryValue> extra_info);
41 
browser_id()42   int browser_id() const { return browser_id_; }
is_popup()43   bool is_popup() const { return is_popup_; }
is_windowless()44   bool is_windowless() const { return is_windowless_; }
extra_info()45   CefRefPtr<CefDictionaryValue> extra_info() const { return extra_info_; }
46 
47   // May return NULL if the browser has not yet been created or if the browser
48   // has been destroyed.
49   CefRefPtr<CefBrowserHostBase> browser() const;
50 
51   // Set or clear the browser. Called from CefBrowserHostBase InitializeBrowser
52   // (to set) and DestroyBrowser (to clear).
53   void SetBrowser(CefRefPtr<CefBrowserHostBase> browser);
54 
55   // Ensure that a frame record exists for |host|. Called for the main frame
56   // when the RenderView is created, or for a sub-frame when the associated
57   // RenderFrame is created in the renderer process.
58   // Called from CefBrowserContentsDelegate::RenderFrameCreated (is_guest_view =
59   // false) or CefMimeHandlerViewGuestDelegate::OnGuestAttached (is_guest_view =
60   // true).
61   void MaybeCreateFrame(content::RenderFrameHost* host, bool is_guest_view);
62 
63   // Remove the frame record for |host|. Called for the main frame when the
64   // RenderView is destroyed, or for a sub-frame when the associated RenderFrame
65   // is destroyed in the renderer process.
66   // Called from CefBrowserContentsDelegate::RenderFrameDeleted or
67   // CefMimeHandlerViewGuestDelegate::OnGuestDetached.
68   void RemoveFrame(content::RenderFrameHost* host);
69 
70   // Returns the main frame object. This object will remain valid until the
71   // browser is destroyed even though the indentifier may change with cross-
72   // origin navigations. Furthermore, calling LoadURL on this object will always
73   // behave as expected because the call is routed through the browser's
74   // NavigationController.
75   CefRefPtr<CefFrameHostImpl> GetMainFrame();
76 
77   // Creates a temporary sub-frame object for situations during navigation or
78   // resource loading where a RFH does not yet exist. If |parent_frame_id|
79   // is invalid the current main frame will be specified as the parent.
80   // Temporary frame objects are not tracked but will be implicitly detached
81   // on browser destruction.
82   CefRefPtr<CefFrameHostImpl> CreateTempSubFrame(int64_t parent_frame_id);
83 
84   // Returns the frame object matching the specified host or nullptr if no match
85   // is found. Nullptr will also be returned if a guest view match is found
86   // because we don't create frame objects for guest views. If |is_guest_view|
87   // is non-nullptr it will be set to true in this case. Must be called on the
88   // UI thread.
89   CefRefPtr<CefFrameHostImpl> GetFrameForHost(
90       const content::RenderFrameHost* host,
91       bool* is_guest_view = nullptr) const;
92 
93   // Returns the frame object matching the specified IDs or nullptr if no match
94   // is found. Nullptr will also be returned if a guest view match is found
95   // because we don't create frame objects for guest views. If |is_guest_view|
96   // is non-nullptr it will be set to true in this case. Safe to call from any
97   // thread.
98   CefRefPtr<CefFrameHostImpl> GetFrameForRoute(
99       int32_t render_process_id,
100       int32_t render_routing_id,
101       bool* is_guest_view = nullptr) const;
102 
103   // Returns the frame object matching the specified ID or nullptr if no match
104   // is found. Nullptr will also be returned if a guest view match is found
105   // because we don't create frame objects for guest views. If |is_guest_view|
106   // is non-nullptr it will be set to true in this case. Safe to call from any
107   // thread.
108   CefRefPtr<CefFrameHostImpl> GetFrameForId(
109       int64_t frame_id,
110       bool* is_guest_view = nullptr) const;
111 
112   // Returns the frame object matching the specified ID or nullptr if no match
113   // is found. Nullptr will also be returned if a guest view match is found
114   // because we don't create frame objects for guest views. If |is_guest_view|
115   // is non-nullptr it will be set to true in this case. Safe to call from any
116   // thread.
117   CefRefPtr<CefFrameHostImpl> GetFrameForFrameTreeNode(
118       int frame_tree_node_id,
119       bool* is_guest_view = nullptr) const;
120 
121   // Returns all non-speculative frame objects that currently exist. Guest views
122   // will be excluded because they don't have a frame object. Safe to call from
123   // any thread.
124   typedef std::set<CefRefPtr<CefFrameHostImpl>> FrameHostList;
125   FrameHostList GetAllFrames() const;
126 
127   class NavigationLock final : public base::RefCounted<NavigationLock> {
128    private:
129     friend class CefBrowserInfo;
130     friend class base::RefCounted<NavigationLock>;
131 
132     NavigationLock();
133     ~NavigationLock();
134 
135     base::OnceClosure pending_action_;
136     base::WeakPtrFactory<NavigationLock> weak_ptr_factory_;
137   };
138 
139   // Block navigation actions on NavigationLock life span. Must be called on the
140   // UI thread.
141   scoped_refptr<NavigationLock> CreateNavigationLock();
142 
143   // Returns true if navigation actions are currently blocked. If this method
144   // returns true the most recent |pending_action| will be executed on the UI
145   // thread once the navigation lock is released. Must be called on the UI
146   // thread.
147   bool IsNavigationLocked(base::OnceClosure pending_action);
148 
149  private:
150   friend class base::RefCountedThreadSafe<CefBrowserInfo>;
151 
152   virtual ~CefBrowserInfo();
153 
154   struct FrameInfo {
155     ~FrameInfo();
156 
157     content::RenderFrameHost* host_;
158     int64_t frame_id_;  // Combination of render_process_id + render_routing_id.
159     int frame_tree_node_id_;
160     bool is_guest_view_;
161     bool is_main_frame_;
162     bool is_speculative_;
163     CefRefPtr<CefFrameHostImpl> frame_;
164   };
165 
166   void MaybeUpdateFrameTreeNodeIdMap(FrameInfo* info);
167 
168   CefRefPtr<CefFrameHostImpl> GetFrameForFrameTreeNodeInternal(
169       int frame_tree_node_id,
170       bool* is_guest_view = nullptr) const;
171 
172   void RemoveAllFrames();
173 
174   int browser_id_;
175   bool is_popup_;
176   bool is_windowless_;
177   CefRefPtr<CefDictionaryValue> extra_info_;
178 
179   // Navigation will be blocked while |navigation_lock_| exists.
180   // Only accessed on the UI thread.
181   base::WeakPtr<NavigationLock> navigation_lock_;
182 
183   mutable base::Lock lock_;
184 
185   // The below members must be protected by |lock_|.
186 
187   CefRefPtr<CefBrowserHostBase> browser_;
188 
189   // Owner of FrameInfo structs.
190   typedef std::set<std::unique_ptr<FrameInfo>, base::UniquePtrComparator>
191       FrameInfoSet;
192   FrameInfoSet frame_info_set_;
193 
194   // Map a frame ID (e.g. MakeFrameId(process_id, routing_id)) to one frame.
195   typedef std::unordered_map<int64_t, FrameInfo*> FrameIDMap;
196   FrameIDMap frame_id_map_;
197 
198   // Map a frame_tree_node_id to one frame.
199   typedef std::unordered_map<int, FrameInfo*> FrameTreeNodeIDMap;
200   FrameTreeNodeIDMap frame_tree_node_id_map_;
201 
202   // The current main frame.
203   CefRefPtr<CefFrameHostImpl> main_frame_;
204 
205   DISALLOW_COPY_AND_ASSIGN(CefBrowserInfo);
206 };
207 
208 #endif  // CEF_LIBCEF_BROWSER_BROWSER_INFO_H_
209