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 GeolocationClientImpl
;
38 class HTMLFrameDelegate
;
39 class HTMLFrameTreeManager
;
42 class WebLayerTreeViewImpl
;
44 // Frame is used to represent a single frame in the frame tree of a page. The
45 // frame is either local or remote. Each Frame is associated with a single
46 // HTMLFrameTreeManager and can not be moved to another HTMLFrameTreeManager.
47 // Local frames have a mojo::View, remote frames do not.
49 // HTMLFrame serves as the FrameTreeClient. It implements it by forwarding
50 // the calls to HTMLFrameTreeManager so that HTMLFrameTreeManager can update
51 // the frame tree as appropriate.
53 // Local frames may share the connection (and client implementation) with an
54 // ancestor. This happens when a child frame is created. Once a navigate
55 // happens the frame is swapped to a remote frame.
57 // Remote frames may become local again if the embed happens in the same
58 // process. See HTMLFrameTreeManager for details.
59 class HTMLFrame
: public blink::WebFrameClient
,
60 public blink::WebViewClient
,
61 public blink::WebRemoteFrameClient
,
62 public mandoline::FrameTreeClient
,
63 public mojo::ViewObserver
{
67 HTMLFrameTreeManager
* manager
,
71 const mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>& properties
,
72 HTMLFrameDelegate
* delegate
)
77 properties(properties
),
79 allow_local_shared_frame(false) {}
82 HTMLFrameTreeManager
* manager
;
86 const mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>& properties
;
87 HTMLFrameDelegate
* delegate
;
90 friend class HTMLFrame
;
93 bool allow_local_shared_frame
;
96 explicit HTMLFrame(CreateParams
* params
);
98 // Called when another app is embedded in the View this Frame is associated
100 void OnViewUnembed();
102 // Closes and deletes this Frame.
105 uint32_t id() const { return id_
; }
107 // Returns the Frame whose id is |id|.
108 HTMLFrame
* FindFrame(uint32_t id
) {
109 return const_cast<HTMLFrame
*>(
110 const_cast<const HTMLFrame
*>(this)->FindFrame(id
));
112 const HTMLFrame
* FindFrame(uint32_t id
) const;
114 HTMLFrame
* parent() { return parent_
; }
116 const std::vector
<HTMLFrame
*>& children() { return children_
; }
118 // Returns the WebFrame for this Frame. This is either a WebLocalFrame or
120 blink::WebFrame
* web_frame() { return web_frame_
; }
122 // Returns the WebView for this frame, or null if there isn't one. The root
123 // has a WebView, the children WebFrameWidgets.
124 blink::WebView
* web_view();
126 // The mojo::View this frame renders to. This is non-null for the local frame
127 // the frame tree was created with as well as non-null for any frames created
129 mojo::View
* view() { return view_
; }
131 HTMLFrameTreeManager
* frame_tree_manager() { return frame_tree_manager_
; }
133 // Returns true if this or one of the frames descendants is local.
134 bool HasLocalDescendant() const;
137 virtual ~HTMLFrame();
139 // TODO(sky): move implementations to match new position.
141 // WebViewClient methods:
142 virtual blink::WebStorageNamespace
* createSessionStorageNamespace();
143 virtual void didCancelCompositionOnSelectionChange();
144 virtual void didChangeContents();
146 // WebWidgetClient methods:
147 virtual void initializeLayerTreeView();
148 virtual blink::WebLayerTreeView
* layerTreeView();
149 virtual void resetInputMethod();
150 virtual void didHandleGestureEvent(const blink::WebGestureEvent
& event
,
151 bool eventCancelled
);
152 virtual void didUpdateTextOfFocusedElementByNonUserInput();
153 virtual void showImeIfNeeded();
155 // WebFrameClient methods:
156 virtual blink::WebMediaPlayer
* createMediaPlayer(
157 blink::WebLocalFrame
* frame
,
158 const blink::WebURL
& url
,
159 blink::WebMediaPlayerClient
* client
,
160 blink::WebMediaPlayerEncryptedMediaClient
* encrypted_client
,
161 blink::WebContentDecryptionModule
* initial_cdm
);
162 virtual blink::WebFrame
* createChildFrame(
163 blink::WebLocalFrame
* parent
,
164 blink::WebTreeScopeType scope
,
165 const blink::WebString
& frame_ame
,
166 blink::WebSandboxFlags sandbox_flags
);
167 virtual void frameDetached(blink::WebFrame
* frame
,
168 blink::WebFrameClient::DetachType type
);
169 virtual blink::WebCookieJar
* cookieJar(blink::WebLocalFrame
* frame
);
170 virtual blink::WebNavigationPolicy
decidePolicyForNavigation(
171 const NavigationPolicyInfo
& info
);
172 virtual void didHandleOnloadEvents(blink::WebLocalFrame
* frame
);
173 virtual void didAddMessageToConsole(const blink::WebConsoleMessage
& message
,
174 const blink::WebString
& source_name
,
175 unsigned source_line
,
176 const blink::WebString
& stack_trace
);
177 virtual void didFinishLoad(blink::WebLocalFrame
* frame
);
178 virtual void didNavigateWithinPage(blink::WebLocalFrame
* frame
,
179 const blink::WebHistoryItem
& history_item
,
180 blink::WebHistoryCommitType commit_type
);
181 virtual void didFirstVisuallyNonEmptyLayout(blink::WebLocalFrame
* frame
);
182 virtual blink::WebGeolocationClient
* geolocationClient();
183 virtual blink::WebEncryptedMediaClient
* encryptedMediaClient();
184 virtual void didStartLoading(bool to_different_document
);
185 virtual void didStopLoading();
186 virtual void didChangeLoadProgress(double load_progress
);
187 virtual void didChangeName(blink::WebLocalFrame
* frame
,
188 const blink::WebString
& name
);
189 virtual void didCommitProvisionalLoad(
190 blink::WebLocalFrame
* frame
,
191 const blink::WebHistoryItem
& item
,
192 blink::WebHistoryCommitType commit_type
);
195 friend class HTMLFrameTreeManager
;
197 // Binds this frame to the specified server. |this| serves as the
198 // FrameTreeClient for the server.
199 void Bind(mandoline::FrameTreeServerPtr frame_tree_server
,
200 mojo::InterfaceRequest
<mandoline::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 // Returns true if the Frame is local, false if remote.
209 bool IsLocal() const;
211 // The local root is the first ancestor (starting at this) that has its own
213 HTMLFrame
* GetLocalRoot();
215 // Gets the FrameTreeServer to use for this frame.
216 mandoline::FrameTreeServer
* GetFrameTreeServer();
218 // Returns the ApplicationImpl from the local root's delegate.
219 mojo::ApplicationImpl
* GetLocalRootApp();
221 void SetView(mojo::View
* view
);
223 // Creates the appropriate WebWidget implementation for the Frame.
224 void CreateRootWebWidget();
225 void CreateLocalRootWebWidget(blink::WebLocalFrame
* local_frame
);
227 void InitializeWebWidget();
231 // Updates the size and scale factor of the webview and related classes from
233 void UpdateWebViewSizeFromViewSize();
235 // Swaps this frame from a local frame to remote frame. |request| is the url
236 // to load in the frame.
239 // Swaps this frame from a remote frame to a local frame.
241 HTMLFrameDelegate
* delegate
,
243 const mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>& properties
);
245 GlobalState
* global_state() { return frame_tree_manager_
->global_state(); }
247 // Returns the Frame associated with the specified WebFrame.
248 HTMLFrame
* FindFrameWithWebFrame(blink::WebFrame
* web_frame
);
250 // The various frameDetached() implementations call into this.
251 void FrameDetachedImpl(blink::WebFrame
* web_frame
);
253 // Update text input state from WebView to mojo::View. If the focused element
254 // is editable and |show_ime| is True, the software keyboard will be shown.
255 void UpdateTextInputState(bool show_ime
);
257 // mojo::ViewObserver methods:
258 void OnViewBoundsChanged(mojo::View
* view
,
259 const mojo::Rect
& old_bounds
,
260 const mojo::Rect
& new_bounds
) override
;
261 void OnViewDestroyed(mojo::View
* view
) override
;
262 void OnViewInputEvent(mojo::View
* view
, const mojo::EventPtr
& event
) override
;
263 void OnViewFocusChanged(mojo::View
* gained_focus
,
264 mojo::View
* lost_focus
) override
;
266 // mandoline::FrameTreeClient:
267 void OnConnect(mandoline::FrameTreeServerPtr server
,
269 mojo::Array
<mandoline::FrameDataPtr
> frame_data
) override
;
270 void OnFrameAdded(uint32_t change_id
,
271 mandoline::FrameDataPtr frame_data
) override
;
272 void OnFrameRemoved(uint32_t change_id
, uint32_t frame_id
) override
;
273 void OnFrameClientPropertyChanged(uint32_t frame_id
,
274 const mojo::String
& name
,
275 mojo::Array
<uint8_t> new_value
) override
;
276 void OnPostMessageEvent(
277 uint32_t source_frame_id
,
278 uint32_t target_frame_id
,
279 mandoline::HTMLMessageEventPtr serialized_event
) override
;
281 // blink::WebRemoteFrameClient:
282 virtual void frameDetached(blink::WebRemoteFrameClient::DetachType type
);
283 virtual void postMessageEvent(blink::WebLocalFrame
* source_web_frame
,
284 blink::WebRemoteFrame
* target_web_frame
,
285 blink::WebSecurityOrigin target_origin
,
286 blink::WebDOMMessageEvent event
);
287 virtual void initializeChildFrame(const blink::WebRect
& frame_rect
,
289 virtual void navigate(const blink::WebURLRequest
& request
,
290 bool should_replace_current_entry
);
291 virtual void reload(bool ignore_cache
, bool is_client_redirect
);
292 virtual void forwardInputEvent(const blink::WebInputEvent
* event
);
294 HTMLFrameTreeManager
* frame_tree_manager_
;
296 // |view_| is non-null for local frames or remote frames that were once
299 // The id for this frame. If there is a view, this is the same id as the
302 std::vector
<HTMLFrame
*> children_
;
303 blink::WebFrame
* web_frame_
;
304 blink::WebWidget
* web_widget_
;
305 scoped_ptr
<GeolocationClientImpl
> geolocation_client_impl_
;
306 scoped_ptr
<WebLayerTreeViewImpl
> web_layer_tree_view_impl_
;
307 scoped_ptr
<TouchHandler
> touch_handler_
;
309 scoped_ptr
<WebLayerImpl
> web_layer_
;
311 HTMLFrameDelegate
* delegate_
;
312 scoped_ptr
<mojo::Binding
<mandoline::FrameTreeClient
>>
313 frame_tree_client_binding_
;
314 mandoline::FrameTreeServerPtr server_
;
316 ReplicatedFrameState state_
;
318 // If this frame is the result of creating a local frame
319 // (createChildFrame()), then |owned_view_| is the View initially created
320 // for the frame. While the frame is local |owned_view_| is the same as
321 // |view_|. If this frame becomes remote |view_| is set to null and
322 // |owned_view_| remains as the View initially created for the frame.
324 // This is done to ensure the View isn't prematurely deleted (it must exist
325 // as long as the frame is valid). If the View was deleted as soon as the
326 // frame was swapped to remote then the process rendering to the view would
328 scoped_ptr
<mojo::ScopedViewPtr
> owned_view_
;
330 blink::WebTextInputInfo text_input_info_
;
332 // This object is only valid in the context of performance tests.
333 tracing::StartupPerformanceDataCollectorPtr
334 startup_performance_data_collector_
;
336 base::WeakPtrFactory
<HTMLFrame
> weak_factory_
;
338 DISALLOW_COPY_AND_ASSIGN(HTMLFrame
);
341 } // namespace html_viewer
343 #endif // COMPONENTS_HTML_VIEWER_HTML_FRAME_H_