• 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_FRAME_HOST_IMPL_H_
6 #define CEF_LIBCEF_BROWSER_FRAME_HOST_IMPL_H_
7 #pragma once
8 
9 #include <memory>
10 #include <queue>
11 #include <string>
12 
13 #include "include/cef_frame.h"
14 
15 #include "base/synchronization/lock.h"
16 #include "cef/libcef/common/mojom/cef.mojom.h"
17 #include "mojo/public/cpp/bindings/receiver_set.h"
18 #include "mojo/public/cpp/bindings/remote.h"
19 #include "ui/base/page_transition_types.h"
20 
21 namespace content {
22 class RenderFrameHost;
23 struct Referrer;
24 }  // namespace content
25 
26 class GURL;
27 
28 class CefBrowserInfo;
29 class CefBrowserHostBase;
30 
31 // Implementation of CefFrame. CefFrameHostImpl objects should always be created
32 // or retrieved via CefBrowerInfo.
33 class CefFrameHostImpl : public CefFrame, public cef::mojom::BrowserFrame {
34  public:
35   // Create a temporary sub-frame.
36   CefFrameHostImpl(scoped_refptr<CefBrowserInfo> browser_info,
37                    int64_t parent_frame_id);
38 
39   // Create a frame backed by a RFH and owned by CefBrowserInfo.
40   CefFrameHostImpl(scoped_refptr<CefBrowserInfo> browser_info,
41                    content::RenderFrameHost* render_frame_host);
42 
43   CefFrameHostImpl(const CefFrameHostImpl&) = delete;
44   CefFrameHostImpl& operator=(const CefFrameHostImpl&) = delete;
45 
46   ~CefFrameHostImpl() override;
47 
48   // CefFrame methods
49   bool IsValid() override;
50   void Undo() override;
51   void Redo() override;
52   void Cut() override;
53   void Copy() override;
54   void Paste() override;
55   void Delete() override;
56   void SelectAll() override;
57   void ViewSource() override;
58   void GetSource(CefRefPtr<CefStringVisitor> visitor) override;
59   void GetText(CefRefPtr<CefStringVisitor> visitor) override;
60   void LoadRequest(CefRefPtr<CefRequest> request) override;
61   void LoadURL(const CefString& url) override;
62   void ExecuteJavaScript(const CefString& jsCode,
63                          const CefString& scriptUrl,
64                          int startLine) override;
65   bool IsMain() override;
66   bool IsFocused() override;
67   CefString GetName() override;
68   int64 GetIdentifier() override;
69   CefRefPtr<CefFrame> GetParent() override;
70   CefString GetURL() override;
71   CefRefPtr<CefBrowser> GetBrowser() override;
72   CefRefPtr<CefV8Context> GetV8Context() override;
73   void VisitDOM(CefRefPtr<CefDOMVisitor> visitor) override;
74   CefRefPtr<CefURLRequest> CreateURLRequest(
75       CefRefPtr<CefRequest> request,
76       CefRefPtr<CefURLRequestClient> client) override;
77   void SendProcessMessage(CefProcessId target_process,
78                           CefRefPtr<CefProcessMessage> message) override;
79 
is_temporary()80   bool is_temporary() const { return frame_id_ == kInvalidFrameId; }
81 
82   void SetFocused(bool focused);
83   void RefreshAttributes();
84 
85   // Notification that a move or resize of the renderer's containing window has
86   // started. Used on Windows and Linux with the Alloy runtime.
87   void NotifyMoveOrResizeStarted();
88 
89   // Load the specified request.
90   void LoadRequest(cef::mojom::RequestParamsPtr params);
91 
92   // Load the specified URL.
93   void LoadURLWithExtras(const std::string& url,
94                          const content::Referrer& referrer,
95                          ui::PageTransition transition,
96                          const std::string& extra_headers);
97 
98   // Send a command to the renderer for execution.
99   void SendCommand(const std::string& command);
100   void SendCommandWithResponse(
101       const std::string& command,
102       cef::mojom::RenderFrame::SendCommandWithResponseCallback
103           response_callback);
104 
105   // Send JavaScript to the renderer for execution.
106   void SendJavaScript(const std::u16string& jsCode,
107                       const std::string& scriptUrl,
108                       int startLine);
109 
110   // Called from CefBrowserHostBase::DidStopLoading.
111   void MaybeSendDidStopLoading();
112 
113   void ExecuteJavaScriptWithUserGestureForTests(const CefString& javascript);
114 
115   // Returns the RFH associated with this frame. Must be called on the UI
116   // thread.
117   content::RenderFrameHost* GetRenderFrameHost() const;
118 
119   // Owned frame objects will be detached explicitly when the associated
120   // RenderFrame is deleted. Temporary frame objects will be detached
121   // implicitly via CefBrowserInfo::browser() returning nullptr. Returns true
122   // if this was the first call to Detach() for the frame.
123   bool Detach();
124 
125   // A frame has swapped to active status from prerendering or the back-forward
126   // cache. We may need to re-attach if the RFH has changed. See
127   // https://crbug.com/1179502#c8 for additional background.
128   void MaybeReAttach(scoped_refptr<CefBrowserInfo> browser_info,
129                      content::RenderFrameHost* render_frame_host);
130 
131   // cef::mojom::BrowserFrame methods forwarded from CefBrowserFrame.
132   void SendMessage(const std::string& name, base::Value arguments) override;
133   void FrameAttached(mojo::PendingRemote<cef::mojom::RenderFrame> render_frame,
134                      bool reattached) override;
135   void DidFinishFrameLoad(const GURL& validated_url,
136                           int32_t http_status_code) override;
137   void UpdateDraggableRegions(
138       absl::optional<std::vector<cef::mojom::DraggableRegionEntryPtr>> regions)
139       override;
140 
141   static const int64_t kMainFrameId;
142   static const int64_t kFocusedFrameId;
143   static const int64_t kUnspecifiedFrameId;
144   static const int64_t kInvalidFrameId;
145 
146   // PageTransition type for explicit navigations. This must pass the check in
147   // ContentBrowserClient::IsExplicitNavigation for debug URLs (HandleDebugURL)
148   // to work as expected.
149   static const ui::PageTransition kPageTransitionExplicit;
150 
151  private:
152   int64 GetFrameId() const;
153   scoped_refptr<CefBrowserInfo> GetBrowserInfo() const;
154   CefRefPtr<CefBrowserHostBase> GetBrowserHostBase() const;
155 
156   // Send an action to the remote RenderFrame. This will queue the action if the
157   // remote frame is not yet attached.
158   using RenderFrameType = mojo::Remote<cef::mojom::RenderFrame>;
159   using RenderFrameAction = base::OnceCallback<void(const RenderFrameType&)>;
160   void SendToRenderFrame(const std::string& function_name,
161                          RenderFrameAction action);
162 
163   void OnRenderFrameDisconnect();
164 
165   const bool is_main_frame_;
166 
167   // The following members may be read/modified from any thread. All access must
168   // be protected by |state_lock_|.
169   mutable base::Lock state_lock_;
170   int64 frame_id_;
171   scoped_refptr<CefBrowserInfo> browser_info_;
172   bool is_focused_;
173   CefString url_;
174   CefString name_;
175   int64 parent_frame_id_;
176 
177   // The following members are only accessed on the UI thread.
178   content::RenderFrameHost* render_frame_host_ = nullptr;
179 
180   std::queue<std::pair<std::string, RenderFrameAction>>
181       queued_renderer_actions_;
182 
183   mojo::Remote<cef::mojom::RenderFrame> render_frame_;
184 
185   IMPLEMENT_REFCOUNTING(CefFrameHostImpl);
186 };
187 
188 #endif  // CEF_LIBCEF_BROWSER_FRAME_HOST_IMPL_H_
189