Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / components / html_viewer / html_frame.h
blob26af5fb0ee80917a91f8e0f0a8f870aad1b0c9b1
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_
8 #include <vector>
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 "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"
24 namespace cc_blink {
25 class WebLayerImpl;
28 namespace blink {
29 class WebFrame;
30 class WebWidget;
33 namespace mojo {
34 class ApplicationImpl;
35 class Rect;
36 class ScopedViewPtr;
37 class View;
40 namespace html_viewer {
42 class DevToolsAgentImpl;
43 class GeolocationClientImpl;
44 class HTMLFrameDelegate;
45 class HTMLFrameTreeManager;
46 class HTMLWidget;
47 class TouchHandler;
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 web_view::FrameTreeClient,
68 public mojo::ViewObserver {
69 public:
70 struct CreateParams {
71 CreateParams(
72 HTMLFrameTreeManager* manager,
73 HTMLFrame* parent,
74 uint32_t id,
75 mojo::View* view,
76 const mojo::Map<mojo::String, mojo::Array<uint8_t>>& properties,
77 HTMLFrameDelegate* delegate)
78 : manager(manager),
79 parent(parent),
80 id(id),
81 view(view),
82 properties(properties),
83 delegate(delegate),
84 allow_local_shared_frame(false) {}
85 ~CreateParams() {}
87 HTMLFrameTreeManager* manager;
88 HTMLFrame* parent;
89 uint32_t id;
90 mojo::View* view;
91 const mojo::Map<mojo::String, mojo::Array<uint8_t>>& properties;
92 HTMLFrameDelegate* delegate;
94 private:
95 friend class HTMLFrame;
97 // TODO(sky): nuke.
98 bool allow_local_shared_frame;
101 explicit HTMLFrame(CreateParams* params);
103 // Closes and deletes this Frame.
104 void Close();
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
120 // WebRemoteFrame.
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
130 // locally.
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
136 // frame.
137 DevToolsAgentImpl* devtools_agent() { return devtools_agent_.get(); }
139 // Returns true if the Frame is local, false if remote.
140 bool IsLocal() const;
142 // Returns true if this or one of the frames descendants is local.
143 bool HasLocalDescendant() const;
145 protected:
146 virtual ~HTMLFrame();
148 // TODO(sky): move implementations to match new position.
150 // WebFrameClient methods:
151 virtual blink::WebMediaPlayer* createMediaPlayer(
152 blink::WebLocalFrame* frame,
153 const blink::WebURL& url,
154 blink::WebMediaPlayerClient* client,
155 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
156 blink::WebContentDecryptionModule* initial_cdm);
157 virtual blink::WebFrame* createChildFrame(
158 blink::WebLocalFrame* parent,
159 blink::WebTreeScopeType scope,
160 const blink::WebString& frame_ame,
161 blink::WebSandboxFlags sandbox_flags);
162 virtual void frameDetached(blink::WebFrame* frame,
163 blink::WebFrameClient::DetachType type);
164 virtual blink::WebCookieJar* cookieJar(blink::WebLocalFrame* frame);
165 virtual blink::WebNavigationPolicy decidePolicyForNavigation(
166 const NavigationPolicyInfo& info);
167 virtual void didHandleOnloadEvents(blink::WebLocalFrame* frame);
168 virtual void didAddMessageToConsole(const blink::WebConsoleMessage& message,
169 const blink::WebString& source_name,
170 unsigned source_line,
171 const blink::WebString& stack_trace);
172 virtual void didFinishLoad(blink::WebLocalFrame* frame);
173 virtual void didNavigateWithinPage(blink::WebLocalFrame* frame,
174 const blink::WebHistoryItem& history_item,
175 blink::WebHistoryCommitType commit_type);
176 virtual void didFirstVisuallyNonEmptyLayout(blink::WebLocalFrame* frame);
177 virtual blink::WebGeolocationClient* geolocationClient();
178 virtual blink::WebEncryptedMediaClient* encryptedMediaClient();
179 virtual void didStartLoading(bool to_different_document);
180 virtual void didStopLoading();
181 virtual void didChangeLoadProgress(double load_progress);
182 virtual void didChangeName(blink::WebLocalFrame* frame,
183 const blink::WebString& name);
184 virtual void didCommitProvisionalLoad(
185 blink::WebLocalFrame* frame,
186 const blink::WebHistoryItem& item,
187 blink::WebHistoryCommitType commit_type);
188 virtual void didReceiveTitle(blink::WebLocalFrame* frame,
189 const blink::WebString& title,
190 blink::WebTextDirection direction);
192 private:
193 friend class HTMLFrameTreeManager;
195 // Binds this frame to the specified server. |this| serves as the
196 // FrameTreeClient for the server.
197 void Bind(web_view::FrameTreeServerPtr frame_tree_server,
198 mojo::InterfaceRequest<web_view::FrameTreeClient>
199 frame_tree_client_request);
201 // Sets the appropriate value from the client property. |name| identifies
202 // the property and |new_data| the new value.
203 void SetValueFromClientProperty(const std::string& name,
204 mojo::Array<uint8_t> new_data);
206 // The local root is the first ancestor (starting at this) that has its own
207 // connection.
208 HTMLFrame* GetLocalRoot();
210 // Gets the FrameTreeServer to use for this frame.
211 web_view::FrameTreeServer* GetFrameTreeServer();
213 // Returns the ApplicationImpl from the local root's delegate.
214 mojo::ApplicationImpl* GetLocalRootApp();
216 void SetView(mojo::View* view);
218 // Creates the appropriate WebWidget implementation for the Frame.
219 void CreateRootWebWidget();
220 void CreateLocalRootWebWidget(blink::WebLocalFrame* local_frame);
222 void UpdateFocus();
224 // Swaps this frame from a local frame to remote frame. |request| is the url
225 // to load in the frame.
226 void SwapToRemote();
228 // Swaps this frame from a remote frame to a local frame.
229 void SwapToLocal(
230 HTMLFrameDelegate* delegate,
231 mojo::View* view,
232 const mojo::Map<mojo::String, mojo::Array<uint8_t>>& properties);
234 // Invoked when changing the delegate. This informs the new delegate to take
235 // over. This is used when a different connection is going to take over
236 // responsibility for the frame.
237 void SwapDelegate(HTMLFrameDelegate* delegate);
239 GlobalState* global_state() { return frame_tree_manager_->global_state(); }
241 // Returns the Frame associated with the specified WebFrame.
242 HTMLFrame* FindFrameWithWebFrame(blink::WebFrame* web_frame);
244 // The various frameDetached() implementations call into this.
245 void FrameDetachedImpl(blink::WebFrame* web_frame);
247 // mojo::ViewObserver methods:
248 void OnViewBoundsChanged(mojo::View* view,
249 const mojo::Rect& old_bounds,
250 const mojo::Rect& new_bounds) override;
251 void OnViewDestroyed(mojo::View* view) override;
252 void OnViewInputEvent(mojo::View* view, const mojo::EventPtr& event) override;
253 void OnViewFocusChanged(mojo::View* gained_focus,
254 mojo::View* lost_focus) override;
256 // web_view::FrameTreeClient:
257 void OnConnect(web_view::FrameTreeServerPtr server,
258 uint32_t change_id,
259 uint32_t view_id,
260 web_view::ViewConnectType view_connect_type,
261 mojo::Array<web_view::FrameDataPtr> frame_data,
262 const OnConnectCallback& callback) override;
263 void OnFrameAdded(uint32_t change_id,
264 web_view::FrameDataPtr frame_data) override;
265 void OnFrameRemoved(uint32_t change_id, uint32_t frame_id) override;
266 void OnFrameClientPropertyChanged(uint32_t frame_id,
267 const mojo::String& name,
268 mojo::Array<uint8_t> new_value) override;
269 void OnPostMessageEvent(
270 uint32_t source_frame_id,
271 uint32_t target_frame_id,
272 web_view::HTMLMessageEventPtr serialized_event) override;
273 void OnWillNavigate(uint32_t target_frame_id) override;
275 // blink::WebRemoteFrameClient:
276 virtual void frameDetached(blink::WebRemoteFrameClient::DetachType type);
277 virtual void postMessageEvent(blink::WebLocalFrame* source_web_frame,
278 blink::WebRemoteFrame* target_web_frame,
279 blink::WebSecurityOrigin target_origin,
280 blink::WebDOMMessageEvent event);
281 virtual void initializeChildFrame(const blink::WebRect& frame_rect,
282 float scale_factor);
283 virtual void navigate(const blink::WebURLRequest& request,
284 bool should_replace_current_entry);
285 virtual void reload(bool ignore_cache, bool is_client_redirect);
286 virtual void forwardInputEvent(const blink::WebInputEvent* event);
288 HTMLFrameTreeManager* frame_tree_manager_;
289 HTMLFrame* parent_;
290 // |view_| is non-null for local frames or remote frames that were once
291 // local.
292 mojo::View* view_;
293 // The id for this frame. If there is a view, this is the same id as the
294 // view has.
295 const uint32_t id_;
296 std::vector<HTMLFrame*> children_;
297 blink::WebFrame* web_frame_;
298 scoped_ptr<HTMLWidget> html_widget_;
299 scoped_ptr<GeolocationClientImpl> geolocation_client_impl_;
300 scoped_ptr<TouchHandler> touch_handler_;
302 scoped_ptr<cc_blink::WebLayerImpl> web_layer_;
304 HTMLFrameDelegate* delegate_;
305 scoped_ptr<mojo::Binding<web_view::FrameTreeClient>>
306 frame_tree_client_binding_;
307 web_view::FrameTreeServerPtr server_;
309 ReplicatedFrameState state_;
311 // If this frame is the result of creating a local frame
312 // (createChildFrame()), then |owned_view_| is the View initially created
313 // for the frame. While the frame is local |owned_view_| is the same as
314 // |view_|. If this frame becomes remote |view_| is set to null and
315 // |owned_view_| remains as the View initially created for the frame.
317 // This is done to ensure the View isn't prematurely deleted (it must exist
318 // as long as the frame is valid). If the View was deleted as soon as the
319 // frame was swapped to remote then the process rendering to the view would
320 // be severed.
321 scoped_ptr<mojo::ScopedViewPtr> owned_view_;
323 // This object is only valid in the context of performance tests.
324 tracing::StartupPerformanceDataCollectorPtr
325 startup_performance_data_collector_;
327 scoped_ptr<DevToolsAgentImpl> devtools_agent_;
329 base::WeakPtrFactory<HTMLFrame> weak_factory_;
331 DISALLOW_COPY_AND_ASSIGN(HTMLFrame);
334 } // namespace html_viewer
336 #endif // COMPONENTS_HTML_VIEWER_HTML_FRAME_H_