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 CefRefPtr<CefBrowserHost> GetHost() override; 48 bool CanGoBack() override; 49 void GoBack() override; 50 bool CanGoForward() override; 51 void GoForward() override; 52 bool IsLoading() override; 53 void Reload() override; 54 void ReloadIgnoreCache() override; 55 void StopLoad() override; 56 int GetIdentifier() override; 57 bool IsSame(CefRefPtr<CefBrowser> that) override; 58 bool IsPopup() override; 59 bool HasDocument() override; 60 CefRefPtr<CefFrame> GetMainFrame() override; 61 CefRefPtr<CefFrame> GetFocusedFrame() override; 62 CefRefPtr<CefFrame> GetFrame(int64 identifier) override; 63 CefRefPtr<CefFrame> GetFrame(const CefString& name) override; 64 size_t GetFrameCount() override; 65 void GetFrameIdentifiers(std::vector<int64>& identifiers) override; 66 void GetFrameNames(std::vector<CefString>& names) override; 67 68 CefBrowserImpl(content::RenderView* render_view, 69 int browser_id, 70 bool is_popup, 71 bool is_windowless); 72 ~CefBrowserImpl() override; 73 74 // Returns the matching CefFrameImpl reference or creates a new one. 75 CefRefPtr<CefFrameImpl> GetWebFrameImpl(blink::WebLocalFrame* frame); 76 CefRefPtr<CefFrameImpl> GetWebFrameImpl(int64_t frame_id); 77 78 // Frame objects will be deleted immediately before the frame is closed. 79 void AddFrameObject(int64_t frame_id, CefTrackNode* tracked_object); 80 browser_id()81 int browser_id() const { return browser_id_; } is_popup()82 bool is_popup() const { return is_popup_; } is_windowless()83 bool is_windowless() const { return is_windowless_; } 84 85 // blink::WebViewObserver methods. 86 void OnDestruct() override; 87 void FrameDetached(int64_t frame_id); 88 89 void OnLoadingStateChange(bool isLoading); 90 91 private: 92 // ID of the browser that this RenderView is associated with. During loading 93 // of cross-origin requests multiple RenderViews may be associated with the 94 // same browser ID. 95 int browser_id_; 96 bool is_popup_; 97 bool is_windowless_; 98 99 // Map of unique frame ids to CefFrameImpl references. 100 typedef std::map<int64, CefRefPtr<CefFrameImpl>> FrameMap; 101 FrameMap frames_; 102 103 // Map of unique frame ids to CefTrackManager objects that need to be cleaned 104 // up when the frame is deleted. 105 typedef std::map<int64, CefRefPtr<CefTrackManager>> FrameObjectMap; 106 FrameObjectMap frame_objects_; 107 108 struct LoadingState { LoadingStateLoadingState109 LoadingState(bool is_loading, bool can_go_back, bool can_go_forward) 110 : is_loading_(is_loading), 111 can_go_back_(can_go_back), 112 can_go_forward_(can_go_forward) {} 113 IsMatchLoadingState114 bool IsMatch(bool is_loading, bool can_go_back, bool can_go_forward) const { 115 return is_loading_ == is_loading && can_go_back_ == can_go_back && 116 can_go_forward_ == can_go_forward; 117 } 118 119 bool is_loading_; 120 bool can_go_back_; 121 bool can_go_forward_; 122 }; 123 std::unique_ptr<LoadingState> last_loading_state_; 124 125 IMPLEMENT_REFCOUNTING(CefBrowserImpl); 126 DISALLOW_COPY_AND_ASSIGN(CefBrowserImpl); 127 }; 128 129 #endif // CEF_LIBCEF_RENDERER_BROWSER_IMPL_H_ 130