1 // Copyright 2021 The Chromium Embedded Framework 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.
4
5 #include "libcef/browser/browser_frame.h"
6
7 #include "libcef/browser/browser_host_base.h"
8 #include "libcef/browser/thread_util.h"
9
10 #include "content/public/browser/render_frame_host.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "mojo/public/cpp/bindings/self_owned_receiver.h"
15
CefBrowserFrame(content::RenderFrameHost * render_frame_host,mojo::PendingReceiver<cef::mojom::BrowserFrame> receiver)16 CefBrowserFrame::CefBrowserFrame(
17 content::RenderFrameHost* render_frame_host,
18 mojo::PendingReceiver<cef::mojom::BrowserFrame> receiver)
19 : FrameServiceBase(render_frame_host, std::move(receiver)) {}
20
21 CefBrowserFrame::~CefBrowserFrame() = default;
22
23 // static
RegisterBrowserInterfaceBindersForFrame(content::RenderFrameHost * render_frame_host,mojo::BinderMapWithContext<content::RenderFrameHost * > * map)24 void CefBrowserFrame::RegisterBrowserInterfaceBindersForFrame(
25 content::RenderFrameHost* render_frame_host,
26 mojo::BinderMapWithContext<content::RenderFrameHost*>* map) {
27 map->Add<cef::mojom::BrowserFrame>(base::BindRepeating(
28 [](content::RenderFrameHost* frame_host,
29 mojo::PendingReceiver<cef::mojom::BrowserFrame> receiver) {
30 // This object is bound to the lifetime of |frame_host| and the mojo
31 // connection. See DocumentServiceBase for details.
32 new CefBrowserFrame(frame_host, std::move(receiver));
33 }));
34 }
35
SendMessage(const std::string & name,base::Value arguments)36 void CefBrowserFrame::SendMessage(const std::string& name,
37 base::Value arguments) {
38 // Always send to the newly created RFH, which may be speculative when
39 // navigating cross-origin.
40 if (auto host = GetFrameHost(/*prefer_speculative=*/true)) {
41 host->SendMessage(name, std::move(arguments));
42 }
43 }
44
FrameAttached(mojo::PendingRemote<cef::mojom::RenderFrame> render_frame,bool reattached)45 void CefBrowserFrame::FrameAttached(
46 mojo::PendingRemote<cef::mojom::RenderFrame> render_frame,
47 bool reattached) {
48 // Always send to the newly created RFH, which may be speculative when
49 // navigating cross-origin.
50 if (auto host = GetFrameHost(/*prefer_speculative=*/true)) {
51 host->FrameAttached(std::move(render_frame), reattached);
52 }
53 }
54
DidFinishFrameLoad(const GURL & validated_url,int http_status_code)55 void CefBrowserFrame::DidFinishFrameLoad(const GURL& validated_url,
56 int http_status_code) {
57 if (auto host = GetFrameHost()) {
58 host->DidFinishFrameLoad(validated_url, http_status_code);
59 }
60 }
61
UpdateDraggableRegions(absl::optional<std::vector<cef::mojom::DraggableRegionEntryPtr>> regions)62 void CefBrowserFrame::UpdateDraggableRegions(
63 absl::optional<std::vector<cef::mojom::DraggableRegionEntryPtr>> regions) {
64 if (auto host = GetFrameHost()) {
65 host->UpdateDraggableRegions(std::move(regions));
66 }
67 }
68
GetFrameHost(bool prefer_speculative) const69 CefRefPtr<CefFrameHostImpl> CefBrowserFrame::GetFrameHost(
70 bool prefer_speculative) const {
71 CEF_REQUIRE_UIT();
72 auto rfh = render_frame_host();
73 if (auto browser = CefBrowserHostBase::GetBrowserForHost(rfh)) {
74 return browser->browser_info()->GetFrameForHost(rfh, nullptr,
75 prefer_speculative);
76 }
77 NOTREACHED();
78 return nullptr;
79 }
80