• 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 #include "libcef/common/response_manager.h"
15 
16 #include "base/synchronization/lock.h"
17 #include "ui/base/page_transition_types.h"
18 
19 namespace content {
20 class RenderFrameHost;
21 struct Referrer;
22 }  // namespace content
23 
24 namespace IPC {
25 class Message;
26 }
27 
28 class GURL;
29 
30 struct Cef_DraggableRegion_Params;
31 struct Cef_Request_Params;
32 struct Cef_Response_Params;
33 class CefBrowserInfo;
34 class CefBrowserHostBase;
35 struct CefNavigateParams;
36 
37 // Implementation of CefFrame. CefFrameHostImpl objects should always be created
38 // or retrieved via CefBrowerInfo.
39 class CefFrameHostImpl : public CefFrame {
40  public:
41   // Create a temporary frame.
42   CefFrameHostImpl(scoped_refptr<CefBrowserInfo> browser_info,
43                    bool is_main_frame,
44                    int64_t parent_frame_id);
45 
46   // Create a frame backed by a RFH and owned by CefBrowserInfo.
47   CefFrameHostImpl(scoped_refptr<CefBrowserInfo> browser_info,
48                    content::RenderFrameHost* render_frame_host);
49 
50   // Update an existing main frame object.
51   void SetRenderFrameHost(content::RenderFrameHost* host);
52 
53   ~CefFrameHostImpl() override;
54 
55   // CefFrame methods
56   bool IsValid() override;
57   void Undo() override;
58   void Redo() override;
59   void Cut() override;
60   void Copy() override;
61   void Paste() override;
62   void Delete() override;
63   void SelectAll() override;
64   void ViewSource() override;
65   void GetSource(CefRefPtr<CefStringVisitor> visitor) override;
66   void GetText(CefRefPtr<CefStringVisitor> visitor) override;
67   void LoadRequest(CefRefPtr<CefRequest> request) override;
68   void LoadURL(const CefString& url) override;
69   void ExecuteJavaScript(const CefString& jsCode,
70                          const CefString& scriptUrl,
71                          int startLine) override;
72   bool IsMain() override;
73   bool IsFocused() override;
74   CefString GetName() override;
75   int64 GetIdentifier() override;
76   CefRefPtr<CefFrame> GetParent() override;
77   CefString GetURL() override;
78   CefRefPtr<CefBrowser> GetBrowser() override;
79   CefRefPtr<CefV8Context> GetV8Context() override;
80   void VisitDOM(CefRefPtr<CefDOMVisitor> visitor) override;
81   CefRefPtr<CefURLRequest> CreateURLRequest(
82       CefRefPtr<CefRequest> request,
83       CefRefPtr<CefURLRequestClient> client) override;
84   void SendProcessMessage(CefProcessId target_process,
85                           CefRefPtr<CefProcessMessage> message) override;
86 
87   void SetFocused(bool focused);
88   void RefreshAttributes();
89 
90   // Notification that a move or resize of the renderer's containing window has
91   // started. Used on Windows and Linux with the Alloy runtime.
92   void NotifyMoveOrResizeStarted();
93 
94   // Navigate as specified by the |params| argument.
95   void Navigate(const CefNavigateParams& params);
96 
97   // Load the specified URL.
98   void LoadURLWithExtras(const std::string& url,
99                          const content::Referrer& referrer,
100                          ui::PageTransition transition,
101                          const std::string& extra_headers);
102 
103   // Send a command to the renderer for execution.
104   void SendCommand(const std::string& command,
105                    CefRefPtr<CefResponseManager::Handler> responseHandler);
106 
107   // Send code to the renderer for execution.
108   void SendCode(bool is_javascript,
109                 const std::string& code,
110                 const std::string& script_url,
111                 int script_start_line,
112                 CefRefPtr<CefResponseManager::Handler> responseHandler);
113 
114   // Send JavaScript to the renderer for execution.
115   void SendJavaScript(const std::string& jsCode,
116                       const std::string& scriptUrl,
117                       int startLine);
118 
119   // Called from CefBrowserHostBase::DidStopLoading.
120   void MaybeSendDidStopLoading();
121 
122   // Called from CefBrowserHostBase::OnMessageReceived.
123   bool OnMessageReceived(const IPC::Message& message);
124 
125   void ExecuteJavaScriptWithUserGestureForTests(const CefString& javascript);
126 
127   // Returns the RFH associated with this frame. Must be called on the UI
128   // thread.
129   content::RenderFrameHost* GetRenderFrameHost() const;
130 
131   // Owned frame objects will be detached explicitly when the associated
132   // RenderFrame is deleted. Temporary frame objects will be detached
133   // implicitly via CefBrowserInfo::browser() returning nullptr.
134   void Detach();
135 
136   static int64_t MakeFrameId(const content::RenderFrameHost* host);
137   static int64_t MakeFrameId(int32_t render_process_id,
138                              int32_t render_routing_id);
139 
140   static const int64_t kMainFrameId;
141   static const int64_t kFocusedFrameId;
142   static const int64_t kUnspecifiedFrameId;
143   static const int64_t kInvalidFrameId;
144 
145   // PageTransition type for explicit navigations. This must pass the check in
146   // ContentBrowserClient::IsExplicitNavigation for debug URLs (HandleDebugURL)
147   // to work as expected.
148   static const ui::PageTransition kPageTransitionExplicit;
149 
150  private:
151   int64 GetFrameId() const;
152   CefRefPtr<CefBrowserHostBase> GetBrowserHostBase() const;
153 
154   // OnMessageReceived message handlers.
155   void OnAttached();
156   void OnDidFinishLoad(const GURL& validated_url, int http_status_code);
157   void OnUpdateDraggableRegions(
158       const std::vector<Cef_DraggableRegion_Params>& regions);
159   void OnRequest(const Cef_Request_Params& params);
160   void OnResponse(const Cef_Response_Params& params);
161   void OnResponseAck(int request_id);
162 
163   // Send a message to the RenderFrameHost associated with this frame.
164   void Send(IPC::Message* message);
165 
166   const bool is_main_frame_;
167 
168   // The following members may be read/modified from any thread. All access must
169   // be protected by |state_lock_|.
170   mutable base::Lock state_lock_;
171   int64 frame_id_;
172   scoped_refptr<CefBrowserInfo> browser_info_;
173   bool is_focused_;
174   CefString url_;
175   CefString name_;
176   int64 parent_frame_id_;
177 
178   // The following members are only accessed on the UI thread.
179   content::RenderFrameHost* render_frame_host_ = nullptr;
180 
181   bool is_attached_ = false;
182 
183   // Qeueud messages to send when the renderer process attaches.
184   std::queue<std::unique_ptr<IPC::Message>> queued_messages_;
185 
186   // Manages response registrations.
187   std::unique_ptr<CefResponseManager> response_manager_;
188 
189   IMPLEMENT_REFCOUNTING(CefFrameHostImpl);
190   DISALLOW_COPY_AND_ASSIGN(CefFrameHostImpl);
191 };
192 
193 #endif  // CEF_LIBCEF_BROWSER_FRAME_HOST_IMPL_H_
194