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/WebKit/public/web/WebViewClient.h"
29 class ApplicationImpl
;
35 namespace html_viewer
{
37 class DevToolsAgentImpl
;
38 class GeolocationClientImpl
;
39 class HTMLFrameDelegate
;
40 class HTMLFrameTreeManager
;
43 class WebLayerTreeViewImpl
;
45 // Frame is used to represent a single frame in the frame tree of a page. The
46 // frame is either local or remote. Each Frame is associated with a single
47 // HTMLFrameTreeManager and can not be moved to another HTMLFrameTreeManager.
48 // Local frames have a mojo::View, remote frames do not.
50 // HTMLFrame serves as the FrameTreeClient. It implements it by forwarding
51 // the calls to HTMLFrameTreeManager so that HTMLFrameTreeManager can update
52 // the frame tree as appropriate.
54 // Local frames may share the connection (and client implementation) with an
55 // ancestor. This happens when a child frame is created. Once a navigate
56 // happens the frame is swapped to a remote frame.
58 // Remote frames may become local again if the embed happens in the same
59 // process. See HTMLFrameTreeManager for details.
60 class HTMLFrame
: public blink::WebFrameClient
,
61 public blink::WebViewClient
,
62 public blink::WebRemoteFrameClient
,
63 public mandoline::FrameTreeClient
,
64 public mojo::ViewObserver
{
68 HTMLFrameTreeManager
* manager
,
72 const mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>& properties
,
73 HTMLFrameDelegate
* delegate
)
78 properties(properties
),
80 allow_local_shared_frame(false) {}
83 HTMLFrameTreeManager
* manager
;
87 const mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>& properties
;
88 HTMLFrameDelegate
* delegate
;
91 friend class HTMLFrame
;
94 bool allow_local_shared_frame
;
97 explicit HTMLFrame(CreateParams
* params
);
99 // Closes and deletes this Frame.
102 uint32_t id() const { return id_
; }
104 // Returns the Frame whose id is |id|.
105 HTMLFrame
* FindFrame(uint32_t id
) {
106 return const_cast<HTMLFrame
*>(
107 const_cast<const HTMLFrame
*>(this)->FindFrame(id
));
109 const HTMLFrame
* FindFrame(uint32_t id
) const;
111 HTMLFrame
* parent() { return parent_
; }
113 const std::vector
<HTMLFrame
*>& children() { return children_
; }
115 // Returns the WebFrame for this Frame. This is either a WebLocalFrame or
117 blink::WebFrame
* web_frame() { return web_frame_
; }
119 // Returns the WebView for this frame, or null if there isn't one. The root
120 // has a WebView, the children WebFrameWidgets.
121 blink::WebView
* web_view();
123 // The mojo::View this frame renders to. This is non-null for the local frame
124 // the frame tree was created with as well as non-null for any frames created
126 mojo::View
* view() { return view_
; }
128 HTMLFrameTreeManager
* frame_tree_manager() { return frame_tree_manager_
; }
130 // Returns null if the browser side didn't request to setup an agent in this
132 DevToolsAgentImpl
* devtools_agent() { return devtools_agent_
.get(); }
134 // Returns true if this or one of the frames descendants is local.
135 bool HasLocalDescendant() const;
138 virtual ~HTMLFrame();
140 // TODO(sky): move implementations to match new position.
142 // WebViewClient methods:
143 virtual blink::WebStorageNamespace
* createSessionStorageNamespace();
144 virtual void didCancelCompositionOnSelectionChange();
145 virtual void didChangeContents();
147 // WebWidgetClient methods:
148 virtual void initializeLayerTreeView();
149 virtual blink::WebLayerTreeView
* layerTreeView();
150 virtual void resetInputMethod();
151 virtual void didHandleGestureEvent(const blink::WebGestureEvent
& event
,
152 bool eventCancelled
);
153 virtual void didUpdateTextOfFocusedElementByNonUserInput();
154 virtual void showImeIfNeeded();
156 // WebFrameClient methods:
157 virtual blink::WebMediaPlayer
* createMediaPlayer(
158 blink::WebLocalFrame
* frame
,
159 const blink::WebURL
& url
,
160 blink::WebMediaPlayerClient
* client
,
161 blink::WebMediaPlayerEncryptedMediaClient
* encrypted_client
,
162 blink::WebContentDecryptionModule
* initial_cdm
);
163 virtual blink::WebFrame
* createChildFrame(
164 blink::WebLocalFrame
* parent
,
165 blink::WebTreeScopeType scope
,
166 const blink::WebString
& frame_ame
,
167 blink::WebSandboxFlags sandbox_flags
);
168 virtual void frameDetached(blink::WebFrame
* frame
,
169 blink::WebFrameClient::DetachType type
);
170 virtual blink::WebCookieJar
* cookieJar(blink::WebLocalFrame
* frame
);
171 virtual blink::WebNavigationPolicy
decidePolicyForNavigation(
172 const NavigationPolicyInfo
& info
);
173 virtual void didHandleOnloadEvents(blink::WebLocalFrame
* frame
);
174 virtual void didAddMessageToConsole(const blink::WebConsoleMessage
& message
,
175 const blink::WebString
& source_name
,
176 unsigned source_line
,
177 const blink::WebString
& stack_trace
);
178 virtual void didFinishLoad(blink::WebLocalFrame
* frame
);
179 virtual void didNavigateWithinPage(blink::WebLocalFrame
* frame
,
180 const blink::WebHistoryItem
& history_item
,
181 blink::WebHistoryCommitType commit_type
);
182 virtual void didFirstVisuallyNonEmptyLayout(blink::WebLocalFrame
* frame
);
183 virtual blink::WebGeolocationClient
* geolocationClient();
184 virtual blink::WebEncryptedMediaClient
* encryptedMediaClient();
185 virtual void didStartLoading(bool to_different_document
);
186 virtual void didStopLoading();
187 virtual void didChangeLoadProgress(double load_progress
);
188 virtual void didChangeName(blink::WebLocalFrame
* frame
,
189 const blink::WebString
& name
);
190 virtual void didCommitProvisionalLoad(
191 blink::WebLocalFrame
* frame
,
192 const blink::WebHistoryItem
& item
,
193 blink::WebHistoryCommitType commit_type
);
196 friend class HTMLFrameTreeManager
;
198 // Binds this frame to the specified server. |this| serves as the
199 // FrameTreeClient for the server.
200 void Bind(mandoline::FrameTreeServerPtr frame_tree_server
,
201 mojo::InterfaceRequest
<mandoline::FrameTreeClient
>
202 frame_tree_client_request
);
204 // Sets the appropriate value from the client property. |name| identifies
205 // the property and |new_data| the new value.
206 void SetValueFromClientProperty(const std::string
& name
,
207 mojo::Array
<uint8_t> new_data
);
209 // Returns true if the Frame is local, false if remote.
210 bool IsLocal() const;
212 // The local root is the first ancestor (starting at this) that has its own
214 HTMLFrame
* GetLocalRoot();
216 // Gets the FrameTreeServer to use for this frame.
217 mandoline::FrameTreeServer
* GetFrameTreeServer();
219 // Returns the ApplicationImpl from the local root's delegate.
220 mojo::ApplicationImpl
* GetLocalRootApp();
222 void SetView(mojo::View
* view
);
224 // Creates the appropriate WebWidget implementation for the Frame.
225 void CreateRootWebWidget();
226 void CreateLocalRootWebWidget(blink::WebLocalFrame
* local_frame
);
228 void InitializeWebWidget();
232 // Updates the size and scale factor of the webview and related classes from
234 void UpdateWebViewSizeFromViewSize();
236 // Swaps this frame from a local frame to remote frame. |request| is the url
237 // to load in the frame.
240 // Swaps this frame from a remote frame to a local frame.
242 HTMLFrameDelegate
* delegate
,
244 const mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>& properties
);
246 GlobalState
* global_state() { return frame_tree_manager_
->global_state(); }
248 // Returns the Frame associated with the specified WebFrame.
249 HTMLFrame
* FindFrameWithWebFrame(blink::WebFrame
* web_frame
);
251 // The various frameDetached() implementations call into this.
252 void FrameDetachedImpl(blink::WebFrame
* web_frame
);
254 // Update text input state from WebView to mojo::View. If the focused element
255 // is editable and |show_ime| is True, the software keyboard will be shown.
256 void UpdateTextInputState(bool show_ime
);
258 // mojo::ViewObserver methods:
259 void OnViewBoundsChanged(mojo::View
* view
,
260 const mojo::Rect
& old_bounds
,
261 const mojo::Rect
& new_bounds
) override
;
262 void OnViewDestroyed(mojo::View
* view
) override
;
263 void OnViewInputEvent(mojo::View
* view
, const mojo::EventPtr
& event
) override
;
264 void OnViewFocusChanged(mojo::View
* gained_focus
,
265 mojo::View
* lost_focus
) override
;
267 // mandoline::FrameTreeClient:
268 void OnConnect(mandoline::FrameTreeServerPtr server
,
270 mojo::Array
<mandoline::FrameDataPtr
> frame_data
) override
;
271 void OnFrameAdded(uint32_t change_id
,
272 mandoline::FrameDataPtr frame_data
) override
;
273 void OnFrameRemoved(uint32_t change_id
, uint32_t frame_id
) override
;
274 void OnFrameClientPropertyChanged(uint32_t frame_id
,
275 const mojo::String
& name
,
276 mojo::Array
<uint8_t> new_value
) override
;
277 void OnPostMessageEvent(
278 uint32_t source_frame_id
,
279 uint32_t target_frame_id
,
280 mandoline::HTMLMessageEventPtr serialized_event
) override
;
281 void OnWillNavigate(uint32_t target_frame_id
,
282 const OnWillNavigateCallback
& callback
) override
;
284 // blink::WebRemoteFrameClient:
285 virtual void frameDetached(blink::WebRemoteFrameClient::DetachType type
);
286 virtual void postMessageEvent(blink::WebLocalFrame
* source_web_frame
,
287 blink::WebRemoteFrame
* target_web_frame
,
288 blink::WebSecurityOrigin target_origin
,
289 blink::WebDOMMessageEvent event
);
290 virtual void initializeChildFrame(const blink::WebRect
& frame_rect
,
292 virtual void navigate(const blink::WebURLRequest
& request
,
293 bool should_replace_current_entry
);
294 virtual void reload(bool ignore_cache
, bool is_client_redirect
);
295 virtual void forwardInputEvent(const blink::WebInputEvent
* event
);
297 HTMLFrameTreeManager
* frame_tree_manager_
;
299 // |view_| is non-null for local frames or remote frames that were once
302 // The id for this frame. If there is a view, this is the same id as the
305 std::vector
<HTMLFrame
*> children_
;
306 blink::WebFrame
* web_frame_
;
307 blink::WebWidget
* web_widget_
;
308 scoped_ptr
<GeolocationClientImpl
> geolocation_client_impl_
;
309 scoped_ptr
<WebLayerTreeViewImpl
> web_layer_tree_view_impl_
;
310 scoped_ptr
<TouchHandler
> touch_handler_
;
312 scoped_ptr
<WebLayerImpl
> web_layer_
;
314 HTMLFrameDelegate
* delegate_
;
315 scoped_ptr
<mojo::Binding
<mandoline::FrameTreeClient
>>
316 frame_tree_client_binding_
;
317 mandoline::FrameTreeServerPtr server_
;
319 ReplicatedFrameState state_
;
321 // If this frame is the result of creating a local frame
322 // (createChildFrame()), then |owned_view_| is the View initially created
323 // for the frame. While the frame is local |owned_view_| is the same as
324 // |view_|. If this frame becomes remote |view_| is set to null and
325 // |owned_view_| remains as the View initially created for the frame.
327 // This is done to ensure the View isn't prematurely deleted (it must exist
328 // as long as the frame is valid). If the View was deleted as soon as the
329 // frame was swapped to remote then the process rendering to the view would
331 scoped_ptr
<mojo::ScopedViewPtr
> owned_view_
;
333 blink::WebTextInputInfo text_input_info_
;
335 // This object is only valid in the context of performance tests.
336 tracing::StartupPerformanceDataCollectorPtr
337 startup_performance_data_collector_
;
339 scoped_ptr
<DevToolsAgentImpl
> devtools_agent_
;
341 base::WeakPtrFactory
<HTMLFrame
> weak_factory_
;
343 DISALLOW_COPY_AND_ASSIGN(HTMLFrame
);
346 } // namespace html_viewer
348 #endif // COMPONENTS_HTML_VIEWER_HTML_FRAME_H_