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_RENDERER_FRAME_IMPL_H_ 6 #define CEF_LIBCEF_RENDERER_FRAME_IMPL_H_ 7 #pragma once 8 9 #include <queue> 10 #include <string> 11 12 #include "include/cef_frame.h" 13 #include "include/cef_v8.h" 14 15 #include "base/memory/weak_ptr.h" 16 #include "base/timer/timer.h" 17 #include "cef/libcef/common/mojom/cef.mojom.h" 18 #include "mojo/public/cpp/bindings/pending_receiver.h" 19 #include "mojo/public/cpp/bindings/receiver.h" 20 #include "mojo/public/cpp/bindings/remote.h" 21 22 namespace base { 23 class ListValue; 24 } 25 26 namespace blink { 27 class ResourceLoadInfoNotifierWrapper; 28 class WebLocalFrame; 29 class WebURLLoader; 30 class WebURLLoaderFactory; 31 } // namespace blink 32 33 class GURL; 34 35 class CefBrowserImpl; 36 37 // Implementation of CefFrame. CefFrameImpl objects are owned by the 38 // CefBrowerImpl and will be detached when the browser is notified that the 39 // associated renderer WebFrame will close. 40 class CefFrameImpl : public CefFrame, public cef::mojom::RenderFrame { 41 public: 42 CefFrameImpl(CefBrowserImpl* browser, 43 blink::WebLocalFrame* frame, 44 int64_t frame_id); 45 46 CefFrameImpl(const CefFrameImpl&) = delete; 47 CefFrameImpl& operator=(const CefFrameImpl&) = delete; 48 49 ~CefFrameImpl() override; 50 51 // CefFrame implementation. 52 bool IsValid() override; 53 void Undo() override; 54 void Redo() override; 55 void Cut() override; 56 void Copy() override; 57 void Paste() override; 58 void Delete() override; 59 void SelectAll() override; 60 void ViewSource() override; 61 void GetSource(CefRefPtr<CefStringVisitor> visitor) override; 62 void GetText(CefRefPtr<CefStringVisitor> visitor) override; 63 void LoadRequest(CefRefPtr<CefRequest> request) override; 64 void LoadURL(const CefString& url) override; 65 void ExecuteJavaScript(const CefString& jsCode, 66 const CefString& scriptUrl, 67 int startLine) override; 68 bool IsMain() override; 69 bool IsFocused() override; 70 CefString GetName() override; 71 int64 GetIdentifier() override; 72 CefRefPtr<CefFrame> GetParent() override; 73 CefString GetURL() override; 74 CefRefPtr<CefBrowser> GetBrowser() override; 75 CefRefPtr<CefV8Context> GetV8Context() override; 76 void VisitDOM(CefRefPtr<CefDOMVisitor> visitor) override; 77 CefRefPtr<CefURLRequest> CreateURLRequest( 78 CefRefPtr<CefRequest> request, 79 CefRefPtr<CefURLRequestClient> client) override; 80 void SendProcessMessage(CefProcessId target_process, 81 CefRefPtr<CefProcessMessage> message) override; 82 83 // Used by CefRenderURLRequest. 84 std::unique_ptr<blink::WebURLLoader> CreateURLLoader(); 85 std::unique_ptr<blink::ResourceLoadInfoNotifierWrapper> 86 CreateResourceLoadInfoNotifierWrapper(); 87 88 // Forwarded from CefRenderFrameObserver. 89 void OnAttached(); 90 void OnWasShown(); 91 void OnDidFinishLoad(); 92 void OnDraggableRegionsChanged(); 93 void OnContextCreated(); 94 void OnDetached(); 95 web_frame()96 blink::WebLocalFrame* web_frame() const { return frame_; } 97 98 private: 99 // Execute an action on the associated WebLocalFrame. This will queue the 100 // action if the JavaScript context is not yet created. 101 using LocalFrameAction = 102 base::OnceCallback<void(blink::WebLocalFrame* frame)>; 103 void ExecuteOnLocalFrame(const std::string& function_name, 104 LocalFrameAction action); 105 106 // Initiate the connection to the BrowserFrame channel. 107 void ConnectBrowserFrame(); 108 109 // Returns the remote BrowserFrame object. 110 using BrowserFrameType = mojo::Remote<cef::mojom::BrowserFrame>; 111 const BrowserFrameType& GetBrowserFrame(bool expect_acked = true); 112 113 // Called if the BrowserFrame connection attempt times out. 114 void OnBrowserFrameTimeout(); 115 116 // Called if/when the BrowserFrame channel is disconnected. This may occur due 117 // to frame navigation, destruction, or insertion into the bfcache (when the 118 // browser-side frame representation is destroyed and closes the connection). 119 void OnBrowserFrameDisconnect(); 120 121 // Send an action to the remote BrowserFrame. This will queue the action if 122 // the remote frame is not yet attached. 123 using BrowserFrameAction = base::OnceCallback<void(const BrowserFrameType&)>; 124 void SendToBrowserFrame(const std::string& function_name, 125 BrowserFrameAction action); 126 127 // cef::mojom::RenderFrame methods: 128 void FrameAttachedAck() override; 129 void SendMessage(const std::string& name, base::Value arguments) override; 130 void SendCommand(const std::string& command) override; 131 void SendCommandWithResponse( 132 const std::string& command, 133 cef::mojom::RenderFrame::SendCommandWithResponseCallback callback) 134 override; 135 void SendJavaScript(const std::u16string& jsCode, 136 const std::string& scriptUrl, 137 int32_t startLine) override; 138 void LoadRequest(cef::mojom::RequestParamsPtr params) override; 139 void DidStopLoading() override; 140 void MoveOrResizeStarted() override; 141 142 CefBrowserImpl* browser_; 143 blink::WebLocalFrame* frame_; 144 const int64 frame_id_; 145 146 bool context_created_ = false; 147 std::queue<std::pair<std::string, LocalFrameAction>> queued_context_actions_; 148 149 // Number of times that browser reconnect has been attempted. 150 size_t browser_connect_retry_ct_ = 0; 151 152 // Current browser connection state. 153 enum class ConnectionState { 154 DISCONNECTED, 155 CONNECTION_PENDING, 156 CONNECTION_ACKED, 157 RECONNECT_PENDING, 158 } browser_connection_state_ = ConnectionState::DISCONNECTED; 159 160 base::OneShotTimer browser_connect_timer_; 161 162 std::queue<std::pair<std::string, BrowserFrameAction>> 163 queued_browser_actions_; 164 165 std::unique_ptr<blink::WebURLLoaderFactory> url_loader_factory_; 166 167 mojo::Receiver<cef::mojom::RenderFrame> receiver_{this}; 168 169 mojo::Remote<cef::mojom::BrowserFrame> browser_frame_; 170 171 base::WeakPtrFactory<CefFrameImpl> weak_ptr_factory_{this}; 172 173 IMPLEMENT_REFCOUNTING(CefFrameImpl); 174 }; 175 176 #endif // CEF_LIBCEF_RENDERER_FRAME_IMPL_H_ 177