OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef COMPONENTS_HTML_VIEWER_HTML_FRAME_H_ | |
6 #define COMPONENTS_HTML_VIEWER_HTML_FRAME_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <vector> | |
11 | |
12 #include "base/macros.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/time/time.h" | |
15 #include "cc/layers/surface_layer.h" | |
16 #include "components/html_viewer/html_frame_tree_manager.h" | |
17 #include "components/html_viewer/replicated_frame_state.h" | |
18 #include "components/mus/public/cpp/input_event_handler.h" | |
19 #include "components/mus/public/cpp/window_observer.h" | |
20 #include "components/web_view/public/interfaces/frame.mojom.h" | |
21 #include "mojo/public/cpp/bindings/binding.h" | |
22 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h" | |
23 #include "third_party/WebKit/public/platform/WebMediaPlayer.h" | |
24 #include "third_party/WebKit/public/platform/WebURLRequest.h" | |
25 #include "third_party/WebKit/public/web/WebFrameClient.h" | |
26 #include "third_party/WebKit/public/web/WebRemoteFrameClient.h" | |
27 #include "third_party/WebKit/public/web/WebSandboxFlags.h" | |
28 #include "third_party/WebKit/public/web/WebTextInputInfo.h" | |
29 | |
30 namespace cc_blink { | |
31 class WebLayerImpl; | |
32 } | |
33 | |
34 namespace blink { | |
35 class WebFrame; | |
36 class WebWidget; | |
37 } | |
38 | |
39 namespace mojo { | |
40 class Rect; | |
41 class Shell; | |
42 } | |
43 | |
44 namespace mus { | |
45 class ScopedWindowPtr; | |
46 class Window; | |
47 } | |
48 | |
49 namespace html_viewer { | |
50 | |
51 class DevToolsAgentImpl; | |
52 class GeolocationClientImpl; | |
53 class HTMLFrameDelegate; | |
54 class HTMLFrameTreeManager; | |
55 class HTMLWidget; | |
56 class TouchHandler; | |
57 class WebLayerTreeViewImpl; | |
58 | |
59 // Frame is used to represent a single frame in the frame tree of a page. The | |
60 // frame is either local or remote. Each Frame is associated with a single | |
61 // HTMLFrameTreeManager and can not be moved to another HTMLFrameTreeManager. | |
62 // Local frames have a mus::Window, remote frames do not. | |
63 // | |
64 // HTMLFrame serves as the FrameClient. It implements it by forwarding the | |
65 // calls to HTMLFrameTreeManager so that HTMLFrameTreeManager can update the | |
66 // frame tree as appropriate. | |
67 // | |
68 // Local frames may share the connection (and client implementation) with an | |
69 // ancestor. This happens when a child frame is created. Once a navigate | |
70 // happens the frame is swapped to a remote frame. | |
71 // | |
72 // Remote frames may become local again if the embed happens in the same | |
73 // process. See HTMLFrameTreeManager for details. | |
74 class HTMLFrame : public blink::WebFrameClient, | |
75 public blink::WebRemoteFrameClient, | |
76 public web_view::mojom::FrameClient, | |
77 public mus::WindowObserver, | |
78 public mus::InputEventHandler { | |
79 public: | |
80 struct CreateParams { | |
81 CreateParams( | |
82 HTMLFrameTreeManager* manager, | |
83 HTMLFrame* parent, | |
84 uint32_t id, | |
85 mus::Window* window, | |
86 const mojo::Map<mojo::String, mojo::Array<uint8_t>>& properties, | |
87 HTMLFrameDelegate* delegate) | |
88 : manager(manager), | |
89 parent(parent), | |
90 id(id), | |
91 window(window), | |
92 properties(properties), | |
93 delegate(delegate), | |
94 is_local_create_child(false) {} | |
95 ~CreateParams() {} | |
96 | |
97 HTMLFrameTreeManager* manager; | |
98 HTMLFrame* parent; | |
99 uint32_t id; | |
100 mus::Window* window; | |
101 const mojo::Map<mojo::String, mojo::Array<uint8_t>>& properties; | |
102 HTMLFrameDelegate* delegate; | |
103 | |
104 private: | |
105 friend class HTMLFrame; | |
106 | |
107 // True if the creation is the result of createChildFrame(). That is, a | |
108 // local parent is creating a new child frame. | |
109 bool is_local_create_child; | |
110 }; | |
111 | |
112 explicit HTMLFrame(CreateParams* params); | |
113 | |
114 // Closes and deletes this Frame. | |
115 void Close(); | |
116 | |
117 uint32_t id() const { return id_; } | |
118 | |
119 // Returns the Frame whose id is |id|. | |
120 HTMLFrame* FindFrame(uint32_t id) { | |
121 return const_cast<HTMLFrame*>( | |
122 const_cast<const HTMLFrame*>(this)->FindFrame(id)); | |
123 } | |
124 const HTMLFrame* FindFrame(uint32_t id) const; | |
125 | |
126 HTMLFrame* parent() { return parent_; } | |
127 | |
128 const std::vector<HTMLFrame*>& children() { return children_; } | |
129 | |
130 // Returns the WebFrame for this Frame. This is either a WebLocalFrame or | |
131 // WebRemoteFrame. | |
132 blink::WebFrame* web_frame() { return web_frame_; } | |
133 | |
134 // Returns the WebView for this frame, or null if there isn't one. The root | |
135 // has a WebView, the children WebFrameWidgets. | |
136 blink::WebView* web_view(); | |
137 blink::WebWidget* GetWebWidget(); | |
138 | |
139 // The mus::Window this frame renders to. This is non-null for the local frame | |
140 // the frame tree was created with as well as non-null for any frames created | |
141 // locally. | |
142 mus::Window* window() { return window_; } | |
143 | |
144 HTMLFrameTreeManager* frame_tree_manager() { return frame_tree_manager_; } | |
145 | |
146 // Returns null if the browser side didn't request to setup an agent in this | |
147 // frame. | |
148 DevToolsAgentImpl* devtools_agent() { return devtools_agent_.get(); } | |
149 | |
150 // Returns true if the Frame is local, false if remote. | |
151 bool IsLocal() const; | |
152 | |
153 // Returns true if this or one of the frames descendants is local. | |
154 bool HasLocalDescendant() const; | |
155 | |
156 void LoadRequest(const blink::WebURLRequest& request, | |
157 base::TimeTicks navigation_start_time); | |
158 | |
159 protected: | |
160 ~HTMLFrame() override; | |
161 | |
162 // WebFrameClient methods: | |
163 blink::WebMediaPlayer* createMediaPlayer( | |
164 blink::WebLocalFrame* frame, | |
165 blink::WebMediaPlayer::LoadType load_type, | |
166 const blink::WebURL& url, | |
167 blink::WebMediaPlayerClient* client, | |
168 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client, | |
169 blink::WebContentDecryptionModule* initial_cdm, | |
170 const blink::WebString& sink_id, | |
171 blink::WebMediaSession* media_session) override; | |
172 blink::WebFrame* createChildFrame( | |
173 blink::WebLocalFrame* parent, | |
174 blink::WebTreeScopeType scope, | |
175 const blink::WebString& frame_ame, | |
176 blink::WebSandboxFlags sandbox_flags, | |
177 const blink::WebFrameOwnerProperties& frame_owner_properties) override; | |
178 void frameDetached(blink::WebFrame* frame, | |
179 blink::WebFrameClient::DetachType type) override; | |
180 blink::WebCookieJar* cookieJar(blink::WebLocalFrame* frame) override; | |
181 blink::WebNavigationPolicy decidePolicyForNavigation( | |
182 const NavigationPolicyInfo& info) override; | |
183 bool hasPendingNavigation(blink::WebLocalFrame* frame) override; | |
184 void didHandleOnloadEvents(blink::WebLocalFrame* frame) override; | |
185 void didAddMessageToConsole(const blink::WebConsoleMessage& message, | |
186 const blink::WebString& source_name, | |
187 unsigned source_line, | |
188 const blink::WebString& stack_trace) override; | |
189 void didFinishLoad(blink::WebLocalFrame* frame) override; | |
190 void didNavigateWithinPage(blink::WebLocalFrame* frame, | |
191 const blink::WebHistoryItem& history_item, | |
192 blink::WebHistoryCommitType commit_type) override; | |
193 blink::WebGeolocationClient* geolocationClient() override; | |
194 blink::WebEncryptedMediaClient* encryptedMediaClient() override; | |
195 void didStartLoading(bool to_different_document) override; | |
196 void didStopLoading() override; | |
197 void didChangeLoadProgress(double load_progress) override; | |
198 void dispatchLoad() override; | |
199 void didChangeName(blink::WebLocalFrame* frame, | |
200 const blink::WebString& name) override; | |
201 void didCommitProvisionalLoad( | |
202 blink::WebLocalFrame* frame, | |
203 const blink::WebHistoryItem& item, | |
204 blink::WebHistoryCommitType commit_type) override; | |
205 void didReceiveTitle(blink::WebLocalFrame* frame, | |
206 const blink::WebString& title, | |
207 blink::WebTextDirection direction) override; | |
208 void reportFindInFrameMatchCount(int identifier, | |
209 int count, | |
210 bool finalUpdate) override; | |
211 void reportFindInPageSelection(int identifier, | |
212 int activeMatchOrdinal, | |
213 const blink::WebRect& selection) override; | |
214 bool shouldSearchSingleFrame() override; | |
215 | |
216 private: | |
217 friend class HTMLFrameTreeManager; | |
218 | |
219 // Binds this frame to the specified server. |this| serves as the | |
220 // FrameClient for the server. | |
221 void Bind(web_view::mojom::FramePtr frame, | |
222 mojo::InterfaceRequest<web_view::mojom::FrameClient> | |
223 frame_client_request); | |
224 | |
225 // Sets the appropriate value from the client property. |name| identifies | |
226 // the property and |new_data| the new value. | |
227 void SetValueFromClientProperty(const std::string& name, | |
228 mojo::Array<uint8_t> new_data); | |
229 | |
230 // The local root is the first ancestor (starting at this) that has its own | |
231 // delegate. | |
232 HTMLFrame* GetFirstAncestorWithDelegate(); | |
233 | |
234 // Returns the Shell from the first ancestor with a delegate. | |
235 mojo::Shell* GetShell(); | |
236 | |
237 // Gets the server Frame to use for this frame. | |
238 web_view::mojom::Frame* GetServerFrame(); | |
239 | |
240 void SetWindow(mus::Window* window); | |
241 | |
242 // Creates the appropriate WebWidget implementation for the Frame. | |
243 void CreateRootWebWidget(); | |
244 void CreateLocalRootWebWidget(blink::WebLocalFrame* local_frame); | |
245 | |
246 void UpdateFocus(); | |
247 | |
248 // Swaps this frame from a local frame to remote frame. |request| is the url | |
249 // to load in the frame. | |
250 void SwapToRemote(); | |
251 | |
252 // Swaps this frame from a remote frame to a local frame. | |
253 void SwapToLocal( | |
254 HTMLFrameDelegate* delegate, | |
255 mus::Window* window, | |
256 const mojo::Map<mojo::String, mojo::Array<uint8_t>>& properties); | |
257 | |
258 // Invoked when changing the delegate. This informs the new delegate to take | |
259 // over. This is used when a different connection is going to take over | |
260 // responsibility for the frame. | |
261 void SwapDelegate(HTMLFrameDelegate* delegate); | |
262 | |
263 GlobalState* global_state() { return frame_tree_manager_->global_state(); } | |
264 | |
265 // Returns the focused element if the focused element is in this | |
266 // frame. Returns an empty one otherwise. | |
267 blink::WebElement GetFocusedElement(); | |
268 | |
269 // Returns the Frame associated with the specified WebFrame. | |
270 HTMLFrame* FindFrameWithWebFrame(blink::WebFrame* web_frame); | |
271 | |
272 // The various frameDetached() implementations call into this. | |
273 void FrameDetachedImpl(blink::WebFrame* web_frame); | |
274 | |
275 // mus::WindowObserver methods: | |
276 void OnWindowBoundsChanged(mus::Window* window, | |
277 const gfx::Rect& old_bounds, | |
278 const gfx::Rect& new_bounds) override; | |
279 void OnWindowDestroyed(mus::Window* window) override; | |
280 void OnWindowFocusChanged(mus::Window* gained_focus, | |
281 mus::Window* lost_focus) override; | |
282 | |
283 // mus::InputEventHandler: | |
284 void OnWindowInputEvent(mus::Window* window, | |
285 mus::mojom::EventPtr event, | |
286 scoped_ptr<base::Closure>* ack_callback) override; | |
287 | |
288 // web_view::mojom::FrameClient: | |
289 void OnConnect(web_view::mojom::FramePtr server, | |
290 uint32_t change_id, | |
291 uint32_t window_id, | |
292 web_view::mojom::WindowConnectType window_connect_type, | |
293 mojo::Array<web_view::mojom::FrameDataPtr> frame_data, | |
294 int64_t navigation_start_time_ticks, | |
295 const OnConnectCallback& callback) override; | |
296 void OnFrameAdded(uint32_t change_id, | |
297 web_view::mojom::FrameDataPtr frame_data) override; | |
298 void OnFrameRemoved(uint32_t change_id, uint32_t frame_id) override; | |
299 void OnFrameClientPropertyChanged(uint32_t frame_id, | |
300 const mojo::String& name, | |
301 mojo::Array<uint8_t> new_value) override; | |
302 void OnPostMessageEvent( | |
303 uint32_t source_frame_id, | |
304 uint32_t target_frame_id, | |
305 web_view::mojom::HTMLMessageEventPtr serialized_event) override; | |
306 void OnWillNavigate(const mojo::String& origin, | |
307 const OnWillNavigateCallback& callback) override; | |
308 void OnFrameLoadingStateChanged(uint32_t frame_id, bool loading) override; | |
309 void OnDispatchFrameLoadEvent(uint32_t frame_id) override; | |
310 void Find(int32_t request_id, | |
311 const mojo::String& search_text, | |
312 web_view::mojom::FindOptionsPtr options, | |
313 bool wrap_within_frame, | |
314 const FindCallback& callback) override; | |
315 void StopFinding(bool clear_selection) override; | |
316 void HighlightFindResults(int32_t request_id, | |
317 const mojo::String& search_test, | |
318 web_view::mojom::FindOptionsPtr options, | |
319 bool reset) override; | |
320 void StopHighlightingFindResults() override; | |
321 | |
322 // blink::WebRemoteFrameClient: | |
323 void frameDetached(blink::WebRemoteFrameClient::DetachType type) override; | |
324 void postMessageEvent(blink::WebLocalFrame* source_web_frame, | |
325 blink::WebRemoteFrame* target_web_frame, | |
326 blink::WebSecurityOrigin target_origin, | |
327 blink::WebDOMMessageEvent event) override; | |
328 void initializeChildFrame(const blink::WebRect& frame_rect, | |
329 float scale_factor) override; | |
330 void navigate(const blink::WebURLRequest& request, | |
331 bool should_replace_current_entry) override; | |
332 void reload(bool ignore_cache, bool is_client_redirect) override; | |
333 void frameRectsChanged(const blink::WebRect& frame_rect) override; | |
334 | |
335 HTMLFrameTreeManager* frame_tree_manager_; | |
336 HTMLFrame* parent_; | |
337 // |window_| is non-null for local frames or remote frames that were once | |
338 // local. | |
339 mus::Window* window_; | |
340 // The id for this frame. If there is a window, this is the same id as the | |
341 // window has. | |
342 const uint32_t id_; | |
343 std::vector<HTMLFrame*> children_; | |
344 blink::WebFrame* web_frame_; | |
345 scoped_ptr<HTMLWidget> html_widget_; | |
346 scoped_ptr<GeolocationClientImpl> geolocation_client_impl_; | |
347 scoped_ptr<TouchHandler> touch_handler_; | |
348 | |
349 scoped_ptr<cc_blink::WebLayerImpl> web_layer_; | |
350 scoped_refptr<cc::SurfaceLayer> surface_layer_; | |
351 | |
352 HTMLFrameDelegate* delegate_; | |
353 scoped_ptr<mojo::Binding<web_view::mojom::FrameClient>> frame_client_binding_; | |
354 web_view::mojom::FramePtr server_; | |
355 | |
356 ReplicatedFrameState state_; | |
357 | |
358 // If this frame is the result of creating a local frame | |
359 // (createChildFrame()), then |owned_window_| is the Window initially created | |
360 // for the frame. While the frame is local |owned_window_| is the same as | |
361 // |window_|. If this frame becomes remote |window_| is set to null and | |
362 // |owned_window_| remains as the Window initially created for the frame. | |
363 // | |
364 // This is done to ensure the Window isn't prematurely deleted (it must exist | |
365 // as long as the frame is valid). If the Window was deleted as soon as the | |
366 // frame was swapped to remote then the process rendering to the window would | |
367 // be severed. | |
368 scoped_ptr<mus::ScopedWindowPtr> owned_window_; | |
369 | |
370 // This object is only valid in the context of performance tests. | |
371 tracing::StartupPerformanceDataCollectorPtr | |
372 startup_performance_data_collector_; | |
373 | |
374 scoped_ptr<DevToolsAgentImpl> devtools_agent_; | |
375 | |
376 // A navigation request has been sent to the frame server side, and we haven't | |
377 // received response to it. | |
378 bool pending_navigation_; | |
379 | |
380 base::TimeTicks navigation_start_time_; | |
381 | |
382 base::WeakPtrFactory<HTMLFrame> weak_factory_; | |
383 | |
384 DISALLOW_COPY_AND_ASSIGN(HTMLFrame); | |
385 }; | |
386 | |
387 } // namespace html_viewer | |
388 | |
389 #endif // COMPONENTS_HTML_VIEWER_HTML_FRAME_H_ | |
OLD | NEW |