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 "mandoline/tab/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 mandoline::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 this or one of the frames descendants is local.
140 bool HasLocalDescendant() const;
143 virtual ~HTMLFrame();
145 // TODO(sky): move implementations to match new position.
147 // WebFrameClient methods:
148 virtual blink::WebMediaPlayer
* createMediaPlayer(
149 blink::WebLocalFrame
* frame
,
150 const blink::WebURL
& url
,
151 blink::WebMediaPlayerClient
* client
,
152 blink::WebMediaPlayerEncryptedMediaClient
* encrypted_client
,
153 blink::WebContentDecryptionModule
* initial_cdm
);
154 virtual blink::WebFrame
* createChildFrame(
155 blink::WebLocalFrame
* parent
,
156 blink::WebTreeScopeType scope
,
157 const blink::WebString
& frame_ame
,
158 blink::WebSandboxFlags sandbox_flags
);
159 virtual void frameDetached(blink::WebFrame
* frame
,
160 blink::WebFrameClient::DetachType type
);
161 virtual blink::WebCookieJar
* cookieJar(blink::WebLocalFrame
* frame
);
162 virtual blink::WebNavigationPolicy
decidePolicyForNavigation(
163 const NavigationPolicyInfo
& info
);
164 virtual void didHandleOnloadEvents(blink::WebLocalFrame
* frame
);
165 virtual void didAddMessageToConsole(const blink::WebConsoleMessage
& message
,
166 const blink::WebString
& source_name
,
167 unsigned source_line
,
168 const blink::WebString
& stack_trace
);
169 virtual void didFinishLoad(blink::WebLocalFrame
* frame
);
170 virtual void didNavigateWithinPage(blink::WebLocalFrame
* frame
,
171 const blink::WebHistoryItem
& history_item
,
172 blink::WebHistoryCommitType commit_type
);
173 virtual void didFirstVisuallyNonEmptyLayout(blink::WebLocalFrame
* frame
);
174 virtual blink::WebGeolocationClient
* geolocationClient();
175 virtual blink::WebEncryptedMediaClient
* encryptedMediaClient();
176 virtual void didStartLoading(bool to_different_document
);
177 virtual void didStopLoading();
178 virtual void didChangeLoadProgress(double load_progress
);
179 virtual void didChangeName(blink::WebLocalFrame
* frame
,
180 const blink::WebString
& name
);
181 virtual void didCommitProvisionalLoad(
182 blink::WebLocalFrame
* frame
,
183 const blink::WebHistoryItem
& item
,
184 blink::WebHistoryCommitType commit_type
);
187 friend class HTMLFrameTreeManager
;
189 // Binds this frame to the specified server. |this| serves as the
190 // FrameTreeClient for the server.
191 void Bind(mandoline::FrameTreeServerPtr frame_tree_server
,
192 mojo::InterfaceRequest
<mandoline::FrameTreeClient
>
193 frame_tree_client_request
);
195 // Sets the appropriate value from the client property. |name| identifies
196 // the property and |new_data| the new value.
197 void SetValueFromClientProperty(const std::string
& name
,
198 mojo::Array
<uint8_t> new_data
);
200 // Returns true if the Frame is local, false if remote.
201 bool IsLocal() const;
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 mandoline::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 GlobalState
* global_state() { return frame_tree_manager_
->global_state(); }
233 // Returns the Frame associated with the specified WebFrame.
234 HTMLFrame
* FindFrameWithWebFrame(blink::WebFrame
* web_frame
);
236 // The various frameDetached() implementations call into this.
237 void FrameDetachedImpl(blink::WebFrame
* web_frame
);
239 // mojo::ViewObserver methods:
240 void OnViewBoundsChanged(mojo::View
* view
,
241 const mojo::Rect
& old_bounds
,
242 const mojo::Rect
& new_bounds
) override
;
243 void OnViewDestroyed(mojo::View
* view
) override
;
244 void OnViewInputEvent(mojo::View
* view
, const mojo::EventPtr
& event
) override
;
245 void OnViewFocusChanged(mojo::View
* gained_focus
,
246 mojo::View
* lost_focus
) override
;
248 // mandoline::FrameTreeClient:
249 void OnConnect(mandoline::FrameTreeServerPtr server
,
251 mojo::Array
<mandoline::FrameDataPtr
> frame_data
) override
;
252 void OnFrameAdded(uint32_t change_id
,
253 mandoline::FrameDataPtr frame_data
) override
;
254 void OnFrameRemoved(uint32_t change_id
, uint32_t frame_id
) override
;
255 void OnFrameClientPropertyChanged(uint32_t frame_id
,
256 const mojo::String
& name
,
257 mojo::Array
<uint8_t> new_value
) override
;
258 void OnPostMessageEvent(
259 uint32_t source_frame_id
,
260 uint32_t target_frame_id
,
261 mandoline::HTMLMessageEventPtr serialized_event
) override
;
262 void OnWillNavigate(uint32_t target_frame_id
,
263 const OnWillNavigateCallback
& callback
) override
;
265 // blink::WebRemoteFrameClient:
266 virtual void frameDetached(blink::WebRemoteFrameClient::DetachType type
);
267 virtual void postMessageEvent(blink::WebLocalFrame
* source_web_frame
,
268 blink::WebRemoteFrame
* target_web_frame
,
269 blink::WebSecurityOrigin target_origin
,
270 blink::WebDOMMessageEvent event
);
271 virtual void initializeChildFrame(const blink::WebRect
& frame_rect
,
273 virtual void navigate(const blink::WebURLRequest
& request
,
274 bool should_replace_current_entry
);
275 virtual void reload(bool ignore_cache
, bool is_client_redirect
);
276 virtual void forwardInputEvent(const blink::WebInputEvent
* event
);
278 HTMLFrameTreeManager
* frame_tree_manager_
;
280 // |view_| is non-null for local frames or remote frames that were once
283 // The id for this frame. If there is a view, this is the same id as the
286 std::vector
<HTMLFrame
*> children_
;
287 blink::WebFrame
* web_frame_
;
288 scoped_ptr
<HTMLWidget
> html_widget_
;
289 scoped_ptr
<GeolocationClientImpl
> geolocation_client_impl_
;
290 scoped_ptr
<TouchHandler
> touch_handler_
;
292 scoped_ptr
<cc_blink::WebLayerImpl
> web_layer_
;
294 HTMLFrameDelegate
* delegate_
;
295 scoped_ptr
<mojo::Binding
<mandoline::FrameTreeClient
>>
296 frame_tree_client_binding_
;
297 mandoline::FrameTreeServerPtr server_
;
299 ReplicatedFrameState state_
;
301 // If this frame is the result of creating a local frame
302 // (createChildFrame()), then |owned_view_| is the View initially created
303 // for the frame. While the frame is local |owned_view_| is the same as
304 // |view_|. If this frame becomes remote |view_| is set to null and
305 // |owned_view_| remains as the View initially created for the frame.
307 // This is done to ensure the View isn't prematurely deleted (it must exist
308 // as long as the frame is valid). If the View was deleted as soon as the
309 // frame was swapped to remote then the process rendering to the view would
311 scoped_ptr
<mojo::ScopedViewPtr
> owned_view_
;
313 // This object is only valid in the context of performance tests.
314 tracing::StartupPerformanceDataCollectorPtr
315 startup_performance_data_collector_
;
317 scoped_ptr
<DevToolsAgentImpl
> devtools_agent_
;
319 base::WeakPtrFactory
<HTMLFrame
> weak_factory_
;
321 DISALLOW_COPY_AND_ASSIGN(HTMLFrame
);
324 } // namespace html_viewer
326 #endif // COMPONENTS_HTML_VIEWER_HTML_FRAME_H_