1 // Copyright 2015 The Chromium 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.
5 #ifndef COMPONENTS_HTML_VIEWER_HTML_FRAME_H_
6 #define COMPONENTS_HTML_VIEWER_HTML_FRAME_H_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "components/html_viewer/html_frame_tree_manager.h"
13 #include "components/html_viewer/replicated_frame_state.h"
14 #include "components/view_manager/public/cpp/view_observer.h"
15 #include "components/web_view/public/interfaces/frame_tree.mojom.h"
16 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h"
17 #include "third_party/WebKit/public/platform/WebURLRequest.h"
18 #include "third_party/WebKit/public/web/WebFrameClient.h"
19 #include "third_party/WebKit/public/web/WebRemoteFrameClient.h"
20 #include "third_party/WebKit/public/web/WebSandboxFlags.h"
21 #include "third_party/WebKit/public/web/WebTextInputInfo.h"
22 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
34 class ApplicationImpl
;
40 namespace html_viewer
{
42 class DevToolsAgentImpl
;
43 class GeolocationClientImpl
;
44 class HTMLFrameDelegate
;
45 class HTMLFrameTreeManager
;
48 class WebLayerTreeViewImpl
;
50 // Frame is used to represent a single frame in the frame tree of a page. The
51 // frame is either local or remote. Each Frame is associated with a single
52 // HTMLFrameTreeManager and can not be moved to another HTMLFrameTreeManager.
53 // Local frames have a mojo::View, remote frames do not.
55 // HTMLFrame serves as the FrameTreeClient. It implements it by forwarding
56 // the calls to HTMLFrameTreeManager so that HTMLFrameTreeManager can update
57 // the frame tree as appropriate.
59 // Local frames may share the connection (and client implementation) with an
60 // ancestor. This happens when a child frame is created. Once a navigate
61 // happens the frame is swapped to a remote frame.
63 // Remote frames may become local again if the embed happens in the same
64 // process. See HTMLFrameTreeManager for details.
65 class HTMLFrame
: public blink::WebFrameClient
,
66 public blink::WebRemoteFrameClient
,
67 public web_view::FrameTreeClient
,
68 public mojo::ViewObserver
{
72 HTMLFrameTreeManager
* manager
,
76 const mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>& properties
,
77 HTMLFrameDelegate
* delegate
)
82 properties(properties
),
84 allow_local_shared_frame(false) {}
87 HTMLFrameTreeManager
* manager
;
91 const mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>& properties
;
92 HTMLFrameDelegate
* delegate
;
95 friend class HTMLFrame
;
98 bool allow_local_shared_frame
;
101 explicit HTMLFrame(CreateParams
* params
);
103 // Closes and deletes this Frame.
106 uint32_t id() const { return id_
; }
108 // Returns the Frame whose id is |id|.
109 HTMLFrame
* FindFrame(uint32_t id
) {
110 return const_cast<HTMLFrame
*>(
111 const_cast<const HTMLFrame
*>(this)->FindFrame(id
));
113 const HTMLFrame
* FindFrame(uint32_t id
) const;
115 HTMLFrame
* parent() { return parent_
; }
117 const std::vector
<HTMLFrame
*>& children() { return children_
; }
119 // Returns the WebFrame for this Frame. This is either a WebLocalFrame or
121 blink::WebFrame
* web_frame() { return web_frame_
; }
123 // Returns the WebView for this frame, or null if there isn't one. The root
124 // has a WebView, the children WebFrameWidgets.
125 blink::WebView
* web_view();
126 blink::WebWidget
* GetWebWidget();
128 // The mojo::View this frame renders to. This is non-null for the local frame
129 // the frame tree was created with as well as non-null for any frames created
131 mojo::View
* view() { return view_
; }
133 HTMLFrameTreeManager
* frame_tree_manager() { return frame_tree_manager_
; }
135 // Returns null if the browser side didn't request to setup an agent in this
137 DevToolsAgentImpl
* devtools_agent() { return devtools_agent_
.get(); }
139 // Returns true if the Frame is local, false if remote.
140 bool IsLocal() const;
142 // Returns true if this or one of the frames descendants is local.
143 bool HasLocalDescendant() const;
146 virtual ~HTMLFrame();
148 // TODO(sky): move implementations to match new position.
150 // WebFrameClient methods:
151 virtual blink::WebMediaPlayer
* createMediaPlayer(
152 blink::WebLocalFrame
* frame
,
153 const blink::WebURL
& url
,
154 blink::WebMediaPlayerClient
* client
,
155 blink::WebMediaPlayerEncryptedMediaClient
* encrypted_client
,
156 blink::WebContentDecryptionModule
* initial_cdm
);
157 virtual blink::WebFrame
* createChildFrame(
158 blink::WebLocalFrame
* parent
,
159 blink::WebTreeScopeType scope
,
160 const blink::WebString
& frame_ame
,
161 blink::WebSandboxFlags sandbox_flags
);
162 virtual void frameDetached(blink::WebFrame
* frame
,
163 blink::WebFrameClient::DetachType type
);
164 virtual blink::WebCookieJar
* cookieJar(blink::WebLocalFrame
* frame
);
165 virtual blink::WebNavigationPolicy
decidePolicyForNavigation(
166 const NavigationPolicyInfo
& info
);
167 virtual void didHandleOnloadEvents(blink::WebLocalFrame
* frame
);
168 virtual void didAddMessageToConsole(const blink::WebConsoleMessage
& message
,
169 const blink::WebString
& source_name
,
170 unsigned source_line
,
171 const blink::WebString
& stack_trace
);
172 virtual void didFinishLoad(blink::WebLocalFrame
* frame
);
173 virtual void didNavigateWithinPage(blink::WebLocalFrame
* frame
,
174 const blink::WebHistoryItem
& history_item
,
175 blink::WebHistoryCommitType commit_type
);
176 virtual void didFirstVisuallyNonEmptyLayout(blink::WebLocalFrame
* frame
);
177 virtual blink::WebGeolocationClient
* geolocationClient();
178 virtual blink::WebEncryptedMediaClient
* encryptedMediaClient();
179 virtual void didStartLoading(bool to_different_document
);
180 virtual void didStopLoading();
181 virtual void didChangeLoadProgress(double load_progress
);
182 virtual void didChangeName(blink::WebLocalFrame
* frame
,
183 const blink::WebString
& name
);
184 virtual void didCommitProvisionalLoad(
185 blink::WebLocalFrame
* frame
,
186 const blink::WebHistoryItem
& item
,
187 blink::WebHistoryCommitType commit_type
);
190 friend class HTMLFrameTreeManager
;
192 // Binds this frame to the specified server. |this| serves as the
193 // FrameTreeClient for the server.
194 void Bind(web_view::FrameTreeServerPtr frame_tree_server
,
195 mojo::InterfaceRequest
<web_view::FrameTreeClient
>
196 frame_tree_client_request
);
198 // Sets the appropriate value from the client property. |name| identifies
199 // the property and |new_data| the new value.
200 void SetValueFromClientProperty(const std::string
& name
,
201 mojo::Array
<uint8_t> new_data
);
203 // The local root is the first ancestor (starting at this) that has its own
205 HTMLFrame
* GetLocalRoot();
207 // Gets the FrameTreeServer to use for this frame.
208 web_view::FrameTreeServer
* GetFrameTreeServer();
210 // Returns the ApplicationImpl from the local root's delegate.
211 mojo::ApplicationImpl
* GetLocalRootApp();
213 void SetView(mojo::View
* view
);
215 // Creates the appropriate WebWidget implementation for the Frame.
216 void CreateRootWebWidget();
217 void CreateLocalRootWebWidget(blink::WebLocalFrame
* local_frame
);
221 // Swaps this frame from a local frame to remote frame. |request| is the url
222 // to load in the frame.
225 // Swaps this frame from a remote frame to a local frame.
227 HTMLFrameDelegate
* delegate
,
229 const mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>& properties
);
231 // Invoked when changing the delegate. This informs the new delegate to take
232 // over. This is used when a different connection is going to take over
233 // responsibility for the frame.
234 void SwapDelegate(HTMLFrameDelegate
* delegate
);
236 GlobalState
* global_state() { return frame_tree_manager_
->global_state(); }
238 // Returns the Frame associated with the specified WebFrame.
239 HTMLFrame
* FindFrameWithWebFrame(blink::WebFrame
* web_frame
);
241 // The various frameDetached() implementations call into this.
242 void FrameDetachedImpl(blink::WebFrame
* web_frame
);
244 // mojo::ViewObserver methods:
245 void OnViewBoundsChanged(mojo::View
* view
,
246 const mojo::Rect
& old_bounds
,
247 const mojo::Rect
& new_bounds
) override
;
248 void OnViewDestroyed(mojo::View
* view
) override
;
249 void OnViewInputEvent(mojo::View
* view
, const mojo::EventPtr
& event
) override
;
250 void OnViewFocusChanged(mojo::View
* gained_focus
,
251 mojo::View
* lost_focus
) override
;
253 // web_view::FrameTreeClient:
254 void OnConnect(web_view::FrameTreeServerPtr server
,
257 web_view::ViewConnectType view_connect_type
,
258 mojo::Array
<web_view::FrameDataPtr
> frame_data
,
259 const OnConnectCallback
& callback
) override
;
260 void OnFrameAdded(uint32_t change_id
,
261 web_view::FrameDataPtr frame_data
) override
;
262 void OnFrameRemoved(uint32_t change_id
, uint32_t frame_id
) override
;
263 void OnFrameClientPropertyChanged(uint32_t frame_id
,
264 const mojo::String
& name
,
265 mojo::Array
<uint8_t> new_value
) override
;
266 void OnPostMessageEvent(
267 uint32_t source_frame_id
,
268 uint32_t target_frame_id
,
269 web_view::HTMLMessageEventPtr serialized_event
) override
;
270 void OnWillNavigate(uint32_t target_frame_id
) override
;
272 // blink::WebRemoteFrameClient:
273 virtual void frameDetached(blink::WebRemoteFrameClient::DetachType type
);
274 virtual void postMessageEvent(blink::WebLocalFrame
* source_web_frame
,
275 blink::WebRemoteFrame
* target_web_frame
,
276 blink::WebSecurityOrigin target_origin
,
277 blink::WebDOMMessageEvent event
);
278 virtual void initializeChildFrame(const blink::WebRect
& frame_rect
,
280 virtual void navigate(const blink::WebURLRequest
& request
,
281 bool should_replace_current_entry
);
282 virtual void reload(bool ignore_cache
, bool is_client_redirect
);
283 virtual void forwardInputEvent(const blink::WebInputEvent
* event
);
285 HTMLFrameTreeManager
* frame_tree_manager_
;
287 // |view_| is non-null for local frames or remote frames that were once
290 // The id for this frame. If there is a view, this is the same id as the
293 std::vector
<HTMLFrame
*> children_
;
294 blink::WebFrame
* web_frame_
;
295 scoped_ptr
<HTMLWidget
> html_widget_
;
296 scoped_ptr
<GeolocationClientImpl
> geolocation_client_impl_
;
297 scoped_ptr
<TouchHandler
> touch_handler_
;
299 scoped_ptr
<cc_blink::WebLayerImpl
> web_layer_
;
301 HTMLFrameDelegate
* delegate_
;
302 scoped_ptr
<mojo::Binding
<web_view::FrameTreeClient
>>
303 frame_tree_client_binding_
;
304 web_view::FrameTreeServerPtr server_
;
306 ReplicatedFrameState state_
;
308 // If this frame is the result of creating a local frame
309 // (createChildFrame()), then |owned_view_| is the View initially created
310 // for the frame. While the frame is local |owned_view_| is the same as
311 // |view_|. If this frame becomes remote |view_| is set to null and
312 // |owned_view_| remains as the View initially created for the frame.
314 // This is done to ensure the View isn't prematurely deleted (it must exist
315 // as long as the frame is valid). If the View was deleted as soon as the
316 // frame was swapped to remote then the process rendering to the view would
318 scoped_ptr
<mojo::ScopedViewPtr
> owned_view_
;
320 // This object is only valid in the context of performance tests.
321 tracing::StartupPerformanceDataCollectorPtr
322 startup_performance_data_collector_
;
324 scoped_ptr
<DevToolsAgentImpl
> devtools_agent_
;
326 base::WeakPtrFactory
<HTMLFrame
> weak_factory_
;
328 DISALLOW_COPY_AND_ASSIGN(HTMLFrame
);
331 } // namespace html_viewer
333 #endif // COMPONENTS_HTML_VIEWER_HTML_FRAME_H_