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/mus/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
;
43 namespace html_viewer
{
45 class DevToolsAgentImpl
;
46 class GeolocationClientImpl
;
47 class HTMLFrameDelegate
;
48 class HTMLFrameTreeManager
;
51 class WebLayerTreeViewImpl
;
53 // Frame is used to represent a single frame in the frame tree of a page. The
54 // frame is either local or remote. Each Frame is associated with a single
55 // HTMLFrameTreeManager and can not be moved to another HTMLFrameTreeManager.
56 // Local frames have a mus::View, remote frames do not.
58 // HTMLFrame serves as the FrameTreeClient. It implements it by forwarding
59 // the calls to HTMLFrameTreeManager so that HTMLFrameTreeManager can update
60 // the frame tree as appropriate.
62 // Local frames may share the connection (and client implementation) with an
63 // ancestor. This happens when a child frame is created. Once a navigate
64 // happens the frame is swapped to a remote frame.
66 // Remote frames may become local again if the embed happens in the same
67 // process. See HTMLFrameTreeManager for details.
68 class HTMLFrame
: public blink::WebFrameClient
,
69 public blink::WebRemoteFrameClient
,
70 public web_view::FrameTreeClient
,
71 public mus::ViewObserver
{
75 HTMLFrameTreeManager
* manager
,
79 const mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>& properties
,
80 HTMLFrameDelegate
* delegate
)
85 properties(properties
),
87 is_local_create_child(false) {}
90 HTMLFrameTreeManager
* manager
;
94 const mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>& properties
;
95 HTMLFrameDelegate
* delegate
;
98 friend class HTMLFrame
;
100 // True if the creation is the result of createChildFrame(). That is, a
101 // local parent is creating a new child frame.
102 bool is_local_create_child
;
105 explicit HTMLFrame(CreateParams
* params
);
107 // Closes and deletes this Frame.
110 uint32_t id() const { return id_
; }
112 // Returns the Frame whose id is |id|.
113 HTMLFrame
* FindFrame(uint32_t id
) {
114 return const_cast<HTMLFrame
*>(
115 const_cast<const HTMLFrame
*>(this)->FindFrame(id
));
117 const HTMLFrame
* FindFrame(uint32_t id
) const;
119 HTMLFrame
* parent() { return parent_
; }
121 const std::vector
<HTMLFrame
*>& children() { return children_
; }
123 // Returns the WebFrame for this Frame. This is either a WebLocalFrame or
125 blink::WebFrame
* web_frame() { return web_frame_
; }
127 // Returns the WebView for this frame, or null if there isn't one. The root
128 // has a WebView, the children WebFrameWidgets.
129 blink::WebView
* web_view();
130 blink::WebWidget
* GetWebWidget();
132 // The mus::View this frame renders to. This is non-null for the local frame
133 // the frame tree was created with as well as non-null for any frames created
135 mus::View
* view() { return view_
; }
137 HTMLFrameTreeManager
* frame_tree_manager() { return frame_tree_manager_
; }
139 // Returns null if the browser side didn't request to setup an agent in this
141 DevToolsAgentImpl
* devtools_agent() { return devtools_agent_
.get(); }
143 // Returns true if the Frame is local, false if remote.
144 bool IsLocal() const;
146 // Returns true if this or one of the frames descendants is local.
147 bool HasLocalDescendant() const;
150 virtual ~HTMLFrame();
152 // WebFrameClient methods:
153 virtual blink::WebMediaPlayer
* createMediaPlayer(
154 blink::WebLocalFrame
* frame
,
155 const blink::WebURL
& url
,
156 blink::WebMediaPlayerClient
* client
,
157 blink::WebMediaPlayerEncryptedMediaClient
* encrypted_client
,
158 blink::WebContentDecryptionModule
* initial_cdm
);
159 virtual blink::WebFrame
* createChildFrame(
160 blink::WebLocalFrame
* parent
,
161 blink::WebTreeScopeType scope
,
162 const blink::WebString
& frame_ame
,
163 blink::WebSandboxFlags sandbox_flags
);
164 virtual void frameDetached(blink::WebFrame
* frame
,
165 blink::WebFrameClient::DetachType type
);
166 virtual blink::WebCookieJar
* cookieJar(blink::WebLocalFrame
* frame
);
167 virtual blink::WebNavigationPolicy
decidePolicyForNavigation(
168 const NavigationPolicyInfo
& info
);
169 virtual void didHandleOnloadEvents(blink::WebLocalFrame
* frame
);
170 virtual void didAddMessageToConsole(const blink::WebConsoleMessage
& message
,
171 const blink::WebString
& source_name
,
172 unsigned source_line
,
173 const blink::WebString
& stack_trace
);
174 virtual void didFinishLoad(blink::WebLocalFrame
* frame
);
175 virtual void didNavigateWithinPage(blink::WebLocalFrame
* frame
,
176 const blink::WebHistoryItem
& history_item
,
177 blink::WebHistoryCommitType commit_type
);
178 virtual blink::WebGeolocationClient
* geolocationClient();
179 virtual blink::WebEncryptedMediaClient
* encryptedMediaClient();
180 virtual void didStartLoading(bool to_different_document
);
181 virtual void didStopLoading();
182 virtual void didChangeLoadProgress(double load_progress
);
183 virtual void dispatchLoad();
184 virtual void didChangeName(blink::WebLocalFrame
* frame
,
185 const blink::WebString
& name
);
186 virtual void didCommitProvisionalLoad(
187 blink::WebLocalFrame
* frame
,
188 const blink::WebHistoryItem
& item
,
189 blink::WebHistoryCommitType commit_type
);
190 virtual void didReceiveTitle(blink::WebLocalFrame
* frame
,
191 const blink::WebString
& title
,
192 blink::WebTextDirection direction
);
195 friend class HTMLFrameTreeManager
;
197 // Binds this frame to the specified server. |this| serves as the
198 // FrameTreeClient for the server.
199 void Bind(web_view::FrameTreeServerPtr frame_tree_server
,
200 mojo::InterfaceRequest
<web_view::FrameTreeClient
>
201 frame_tree_client_request
);
203 // Sets the appropriate value from the client property. |name| identifies
204 // the property and |new_data| the new value.
205 void SetValueFromClientProperty(const std::string
& name
,
206 mojo::Array
<uint8_t> new_data
);
208 // The local root is the first ancestor (starting at this) that has its own
210 HTMLFrame
* GetFirstAncestorWithDelegate();
212 // Returns the ApplicationImpl from the first ancestor with a delegate.
213 mojo::ApplicationImpl
* GetApp();
215 // Gets the FrameTreeServer to use for this frame.
216 web_view::FrameTreeServer
* GetFrameTreeServer();
218 void SetView(mus::View
* view
);
220 // Creates the appropriate WebWidget implementation for the Frame.
221 void CreateRootWebWidget();
222 void CreateLocalRootWebWidget(blink::WebLocalFrame
* local_frame
);
226 // Swaps this frame from a local frame to remote frame. |request| is the url
227 // to load in the frame.
230 // Swaps this frame from a remote frame to a local frame.
232 HTMLFrameDelegate
* delegate
,
234 const mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>& properties
);
236 // Invoked when changing the delegate. This informs the new delegate to take
237 // over. This is used when a different connection is going to take over
238 // responsibility for the frame.
239 void SwapDelegate(HTMLFrameDelegate
* delegate
);
241 GlobalState
* global_state() { return frame_tree_manager_
->global_state(); }
243 // Returns the Frame associated with the specified WebFrame.
244 HTMLFrame
* FindFrameWithWebFrame(blink::WebFrame
* web_frame
);
246 // The various frameDetached() implementations call into this.
247 void FrameDetachedImpl(blink::WebFrame
* web_frame
);
249 // mus::ViewObserver methods:
250 void OnViewBoundsChanged(mus::View
* view
,
251 const mojo::Rect
& old_bounds
,
252 const mojo::Rect
& new_bounds
) override
;
253 void OnViewDestroyed(mus::View
* view
) override
;
254 void OnViewInputEvent(mus::View
* view
, const mojo::EventPtr
& event
) override
;
255 void OnViewFocusChanged(mus::View
* gained_focus
,
256 mus::View
* lost_focus
) override
;
258 // web_view::FrameTreeClient:
259 void OnConnect(web_view::FrameTreeServerPtr server
,
262 web_view::ViewConnectType view_connect_type
,
263 mojo::Array
<web_view::FrameDataPtr
> frame_data
,
264 const OnConnectCallback
& callback
) override
;
265 void OnFrameAdded(uint32_t change_id
,
266 web_view::FrameDataPtr frame_data
) override
;
267 void OnFrameRemoved(uint32_t change_id
, uint32_t frame_id
) override
;
268 void OnFrameClientPropertyChanged(uint32_t frame_id
,
269 const mojo::String
& name
,
270 mojo::Array
<uint8_t> new_value
) override
;
271 void OnPostMessageEvent(
272 uint32_t source_frame_id
,
273 uint32_t target_frame_id
,
274 web_view::HTMLMessageEventPtr serialized_event
) override
;
275 void OnWillNavigate() override
;
276 void OnFrameLoadingStateChanged(uint32_t frame_id
, bool loading
) override
;
277 void OnDispatchFrameLoadEvent(uint32_t frame_id
) override
;
279 // blink::WebRemoteFrameClient:
280 virtual void frameDetached(blink::WebRemoteFrameClient::DetachType type
);
281 virtual void postMessageEvent(blink::WebLocalFrame
* source_web_frame
,
282 blink::WebRemoteFrame
* target_web_frame
,
283 blink::WebSecurityOrigin target_origin
,
284 blink::WebDOMMessageEvent event
);
285 virtual void initializeChildFrame(const blink::WebRect
& frame_rect
,
287 virtual void navigate(const blink::WebURLRequest
& request
,
288 bool should_replace_current_entry
);
289 virtual void reload(bool ignore_cache
, bool is_client_redirect
);
290 virtual void forwardInputEvent(const blink::WebInputEvent
* event
);
292 HTMLFrameTreeManager
* frame_tree_manager_
;
294 // |view_| is non-null for local frames or remote frames that were once
297 // The id for this frame. If there is a view, this is the same id as the
300 std::vector
<HTMLFrame
*> children_
;
301 blink::WebFrame
* web_frame_
;
302 scoped_ptr
<HTMLWidget
> html_widget_
;
303 scoped_ptr
<GeolocationClientImpl
> geolocation_client_impl_
;
304 scoped_ptr
<TouchHandler
> touch_handler_
;
306 scoped_ptr
<cc_blink::WebLayerImpl
> web_layer_
;
308 HTMLFrameDelegate
* delegate_
;
309 scoped_ptr
<mojo::Binding
<web_view::FrameTreeClient
>>
310 frame_tree_client_binding_
;
311 web_view::FrameTreeServerPtr server_
;
313 ReplicatedFrameState state_
;
315 // If this frame is the result of creating a local frame
316 // (createChildFrame()), then |owned_view_| is the View initially created
317 // for the frame. While the frame is local |owned_view_| is the same as
318 // |view_|. If this frame becomes remote |view_| is set to null and
319 // |owned_view_| remains as the View initially created for the frame.
321 // This is done to ensure the View isn't prematurely deleted (it must exist
322 // as long as the frame is valid). If the View was deleted as soon as the
323 // frame was swapped to remote then the process rendering to the view would
325 scoped_ptr
<mus::ScopedViewPtr
> owned_view_
;
327 // This object is only valid in the context of performance tests.
328 tracing::StartupPerformanceDataCollectorPtr
329 startup_performance_data_collector_
;
331 scoped_ptr
<DevToolsAgentImpl
> devtools_agent_
;
333 base::WeakPtrFactory
<HTMLFrame
> weak_factory_
;
335 DISALLOW_COPY_AND_ASSIGN(HTMLFrame
);
338 } // namespace html_viewer
340 #endif // COMPONENTS_HTML_VIEWER_HTML_FRAME_H_