1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. 2 // Portions copyright (c) 2011 The Chromium Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 6 #ifndef CEF_LIBCEF_RENDERER_BROWSER_IMPL_H_ 7 #define CEF_LIBCEF_RENDERER_BROWSER_IMPL_H_ 8 #pragma once 9 10 #include <stdint.h> 11 12 #include <map> 13 #include <memory> 14 #include <string> 15 #include <vector> 16 17 #include "include/cef_browser.h" 18 #include "include/cef_client.h" 19 #include "libcef/common/tracker.h" 20 #include "libcef/renderer/frame_impl.h" 21 22 #include "third_party/blink/public/web/web_view_observer.h" 23 24 namespace blink { 25 class WebFrame; 26 class WebNode; 27 } // namespace blink 28 29 namespace content { 30 class RenderView; 31 } 32 33 // Renderer plumbing for CEF features. There is a one-to-one relationship 34 // between RenderView on the renderer side and RenderViewHost on the browser 35 // side. 36 // 37 // RenderViewObserver: Interface for observing RenderView notifications. 38 class CefBrowserImpl : public CefBrowser, public blink::WebViewObserver { 39 public: 40 // Returns the browser associated with the specified RenderView. 41 static CefRefPtr<CefBrowserImpl> GetBrowserForView(content::RenderView* view); 42 // Returns the browser associated with the specified main WebFrame. 43 static CefRefPtr<CefBrowserImpl> GetBrowserForMainFrame( 44 blink::WebFrame* frame); 45 46 // CefBrowser methods. 47 bool IsValid() override; 48 CefRefPtr<CefBrowserHost> GetHost() override; 49 bool CanGoBack() override; 50 void GoBack() override; 51 bool CanGoForward() override; 52 void GoForward() override; 53 bool IsLoading() override; 54 void Reload() override; 55 void ReloadIgnoreCache() override; 56 void StopLoad() override; 57 int GetIdentifier() override; 58 bool IsSame(CefRefPtr<CefBrowser> that) override; 59 bool IsPopup() override; 60 bool HasDocument() override; 61 CefRefPtr<CefFrame> GetMainFrame() override; 62 CefRefPtr<CefFrame> GetFocusedFrame() override; 63 CefRefPtr<CefFrame> GetFrame(int64 identifier) override; 64 CefRefPtr<CefFrame> GetFrame(const CefString& name) override; 65 size_t GetFrameCount() override; 66 void GetFrameIdentifiers(std::vector<int64>& identifiers) override; 67 void GetFrameNames(std::vector<CefString>& names) override; 68 69 CefBrowserImpl(content::RenderView* render_view, 70 int browser_id, 71 bool is_popup, 72 bool is_windowless); 73 74 CefBrowserImpl(const CefBrowserImpl&) = delete; 75 CefBrowserImpl& operator=(const CefBrowserImpl&) = delete; 76 77 ~CefBrowserImpl() override; 78 79 // Returns the matching CefFrameImpl reference or creates a new one. 80 CefRefPtr<CefFrameImpl> GetWebFrameImpl(blink::WebLocalFrame* frame); 81 CefRefPtr<CefFrameImpl> GetWebFrameImpl(int64_t frame_id); 82 83 // Frame objects will be deleted immediately before the frame is closed. 84 void AddFrameObject(int64_t frame_id, CefTrackNode* tracked_object); 85 browser_id()86 int browser_id() const { return browser_id_; } is_popup()87 bool is_popup() const { return is_popup_; } is_windowless()88 bool is_windowless() const { return is_windowless_; } 89 90 // blink::WebViewObserver methods. 91 void OnDestruct() override; 92 void FrameDetached(int64_t frame_id); 93 94 void OnLoadingStateChange(bool isLoading); 95 96 private: 97 // ID of the browser that this RenderView is associated with. During loading 98 // of cross-origin requests multiple RenderViews may be associated with the 99 // same browser ID. 100 int browser_id_; 101 bool is_popup_; 102 bool is_windowless_; 103 104 // Map of unique frame ids to CefFrameImpl references. 105 using FrameMap = std::map<int64, CefRefPtr<CefFrameImpl>>; 106 FrameMap frames_; 107 108 // Map of unique frame ids to CefTrackManager objects that need to be cleaned 109 // up when the frame is deleted. 110 using FrameObjectMap = std::map<int64, CefRefPtr<CefTrackManager>>; 111 FrameObjectMap frame_objects_; 112 113 struct LoadingState { LoadingStateLoadingState114 LoadingState(bool is_loading, bool can_go_back, bool can_go_forward) 115 : is_loading_(is_loading), 116 can_go_back_(can_go_back), 117 can_go_forward_(can_go_forward) {} 118 IsMatchLoadingState119 bool IsMatch(bool is_loading, bool can_go_back, bool can_go_forward) const { 120 return is_loading_ == is_loading && can_go_back_ == can_go_back && 121 can_go_forward_ == can_go_forward; 122 } 123 124 bool is_loading_; 125 bool can_go_back_; 126 bool can_go_forward_; 127 }; 128 std::unique_ptr<LoadingState> last_loading_state_; 129 130 IMPLEMENT_REFCOUNTING(CefBrowserImpl); 131 }; 132 133 #endif // CEF_LIBCEF_RENDERER_BROWSER_IMPL_H_ 134