Stack sampling profiler: add fire-and-forget interface
[chromium-blink-merge.git] / components / html_viewer / html_frame.h
blob97e7f4e50b65670bd539ca2479b56ad661b0a0a0
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/view_manager/public/cpp/view_observer.h"
14 #include "mandoline/tab/public/interfaces/frame_tree.mojom.h"
15 #include "third_party/WebKit/public/platform/WebURLRequest.h"
16 #include "third_party/WebKit/public/web/WebFrameClient.h"
17 #include "third_party/WebKit/public/web/WebRemoteFrameClient.h"
18 #include "third_party/WebKit/public/web/WebSandboxFlags.h"
19 #include "third_party/WebKit/public/web/WebViewClient.h"
21 namespace blink {
22 class WebFrame;
25 namespace mojo {
26 class Rect;
27 class View;
30 namespace html_viewer {
32 class GeolocationClientImpl;
33 class HTMLFrameDelegate;
34 class HTMLFrameTreeManager;
35 class TouchHandler;
36 class WebLayerImpl;
37 class WebLayerTreeViewImpl;
39 // Frame is used to represent a single frame in the frame tree of a page. The
40 // frame is either local or remote. Each Frame is associated with a single
41 // HTMLFrameTreeManager and can not be moved to another HTMLFrameTreeManager.
42 // Local frames have a mojo::View, remote frames do not.
44 // HTMLFrame serves as the FrameTreeClient. It implements it by forwarding
45 // the calls to HTMLFrameTreeManager so that HTMLFrameTreeManager can update
46 // the frame tree as appropriate.
48 // Local frames may share the connection (and client implementation) with an
49 // ancestor. This happens when a child frame is created. Once a navigate
50 // happens the frame is swapped to a remote frame.
52 // Remote frames may become local again if the embed happens in the same
53 // process. See HTMLFrameTreeManager for details.
54 class HTMLFrame : public blink::WebFrameClient,
55 public blink::WebViewClient,
56 public blink::WebRemoteFrameClient,
57 public mandoline::FrameTreeClient,
58 public mojo::ViewObserver {
59 public:
60 struct CreateParams {
61 CreateParams(HTMLFrameTreeManager* manager, HTMLFrame* parent, uint32_t id)
62 : manager(manager), parent(parent), id(id) {}
63 ~CreateParams() {}
65 HTMLFrameTreeManager* manager;
66 HTMLFrame* parent;
67 uint32_t id;
70 explicit HTMLFrame(const CreateParams& params);
72 void Init(mojo::View* local_view,
73 const blink::WebString& remote_frame_name,
74 const blink::WebString& remote_origin);
76 // Closes and deletes this Frame.
77 void Close();
79 uint32_t id() const { return id_; }
81 // Returns the Frame whose id is |id|.
82 HTMLFrame* FindFrame(uint32_t id) {
83 return const_cast<HTMLFrame*>(
84 const_cast<const HTMLFrame*>(this)->FindFrame(id));
86 const HTMLFrame* FindFrame(uint32_t id) const;
88 HTMLFrame* parent() { return parent_; }
90 const std::vector<HTMLFrame*>& children() { return children_; }
92 // Returns the WebFrame for this Frame. This is either a WebLocalFrame or
93 // WebRemoteFrame.
94 blink::WebFrame* web_frame() { return web_frame_; }
96 // Returns the WebView for this frame, or null if there isn't one. The root
97 // has a WebView, the children WebFrameWidgets.
98 blink::WebView* web_view();
100 // The mojo::View this frame renders to. This is non-null for the local frame
101 // the frame tree was created with as well as non-null for any frames created
102 // locally.
103 mojo::View* view() { return view_; }
105 HTMLFrameTreeManager* frame_tree_manager() { return frame_tree_manager_; }
107 // Returns true if this or one of the frames descendants is local.
108 bool HasLocalDescendant() const;
110 private:
111 friend class HTMLFrameTreeManager;
113 virtual ~HTMLFrame();
115 void set_delegate(HTMLFrameDelegate* delegate) { delegate_ = delegate; }
117 // Binds this frame to the specified server. |this| serves as the
118 // FrameTreeClient for the server.
119 void Bind(mandoline::FrameTreeServerPtr frame_tree_server,
120 mojo::InterfaceRequest<mandoline::FrameTreeClient>
121 frame_tree_client_request);
123 // Sets the name of the remote frame. Does nothing if this is a local frame.
124 void SetRemoteFrameName(const mojo::String& name);
126 // Returns true if the Frame is local, false if remote.
127 bool IsLocal() const;
129 // The local root is the first ancestor (starting at this) that has its own
130 // connection.
131 HTMLFrame* GetLocalRoot();
133 // Returns the ApplicationImpl from the local root's delegate.
134 mojo::ApplicationImpl* GetLocalRootApp();
136 void SetView(mojo::View* view);
138 // Creates the appropriate WebWidget implementation for the Frame.
139 void CreateWebWidget();
141 void UpdateFocus();
143 // Updates the size and scale factor of the webview and related classes from
144 // |root_|.
145 void UpdateWebViewSizeFromViewSize();
147 // Swaps this frame from a local frame to remote frame. |request| is the url
148 // to load in the frame.
149 void SwapToRemote(const blink::WebURLRequest& request);
151 // See comment in SwapToRemote() for details on this.
152 void FinishSwapToRemote();
154 // Swaps this frame from a remote frame to a local frame.
155 void SwapToLocal(mojo::View* view, const blink::WebString& name);
157 GlobalState* global_state() { return frame_tree_manager_->global_state(); }
159 // Returns the Frame associated with the specified WebFrame.
160 HTMLFrame* FindFrameWithWebFrame(blink::WebFrame* web_frame);
162 // The various frameDetached() implementations call into this.
163 void FrameDetachedImpl(blink::WebFrame* web_frame);
165 // mojo::ViewObserver methods:
166 void OnViewBoundsChanged(mojo::View* view,
167 const mojo::Rect& old_bounds,
168 const mojo::Rect& new_bounds) override;
169 void OnViewDestroyed(mojo::View* view) override;
170 void OnViewInputEvent(mojo::View* view, const mojo::EventPtr& event) override;
171 void OnViewFocusChanged(mojo::View* gained_focus,
172 mojo::View* lost_focus) override;
174 // mandoline::FrameTreeClient:
175 void OnConnect(mandoline::FrameTreeServerPtr server,
176 mojo::Array<mandoline::FrameDataPtr> frame_data) override;
177 void OnFrameAdded(mandoline::FrameDataPtr frame_data) override;
178 void OnFrameRemoved(uint32_t frame_id) override;
179 void OnFrameNameChanged(uint32_t frame_id, const mojo::String& name) override;
181 // WebViewClient methods:
182 virtual void initializeLayerTreeView() override;
183 virtual blink::WebLayerTreeView* layerTreeView() override;
184 virtual blink::WebStorageNamespace* createSessionStorageNamespace();
186 // WebFrameClient methods:
187 virtual blink::WebMediaPlayer* createMediaPlayer(
188 blink::WebLocalFrame* frame,
189 const blink::WebURL& url,
190 blink::WebMediaPlayerClient* client,
191 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
192 blink::WebContentDecryptionModule* initial_cdm);
193 virtual blink::WebFrame* createChildFrame(
194 blink::WebLocalFrame* parent,
195 blink::WebTreeScopeType scope,
196 const blink::WebString& frame_ame,
197 blink::WebSandboxFlags sandbox_flags);
198 virtual void frameDetached(blink::WebFrame* frame,
199 blink::WebFrameClient::DetachType type);
200 virtual blink::WebCookieJar* cookieJar(blink::WebLocalFrame* frame);
201 virtual blink::WebNavigationPolicy decidePolicyForNavigation(
202 const NavigationPolicyInfo& info);
203 virtual void didAddMessageToConsole(const blink::WebConsoleMessage& message,
204 const blink::WebString& source_name,
205 unsigned source_line,
206 const blink::WebString& stack_trace);
207 virtual void didFinishLoad(blink::WebLocalFrame* frame);
208 virtual void didNavigateWithinPage(blink::WebLocalFrame* frame,
209 const blink::WebHistoryItem& history_item,
210 blink::WebHistoryCommitType commit_type);
211 virtual blink::WebGeolocationClient* geolocationClient();
212 virtual blink::WebEncryptedMediaClient* encryptedMediaClient();
213 virtual void didStartLoading(bool to_different_document);
214 virtual void didStopLoading();
215 virtual void didChangeLoadProgress(double load_progress);
216 virtual void didChangeName(blink::WebLocalFrame* frame,
217 const blink::WebString& name);
219 // blink::WebRemoteFrameClient:
220 virtual void frameDetached(blink::WebRemoteFrameClient::DetachType type);
221 virtual void postMessageEvent(blink::WebLocalFrame* source_frame,
222 blink::WebRemoteFrame* target_frame,
223 blink::WebSecurityOrigin target_origin,
224 blink::WebDOMMessageEvent event);
225 virtual void initializeChildFrame(const blink::WebRect& frame_rect,
226 float scale_factor);
227 virtual void navigate(const blink::WebURLRequest& request,
228 bool should_replace_current_entry);
229 virtual void reload(bool ignore_cache, bool is_client_redirect);
230 virtual void forwardInputEvent(const blink::WebInputEvent* event);
232 HTMLFrameTreeManager* frame_tree_manager_;
233 HTMLFrame* parent_;
234 mojo::View* view_;
235 // The id for this frame. If there is a view, this is the same id as the
236 // view has.
237 const uint32_t id_;
238 std::vector<HTMLFrame*> children_;
239 blink::WebFrame* web_frame_;
240 blink::WebWidget* web_widget_;
241 scoped_ptr<GeolocationClientImpl> geolocation_client_impl_;
242 scoped_ptr<WebLayerTreeViewImpl> web_layer_tree_view_impl_;
243 scoped_ptr<TouchHandler> touch_handler_;
245 // TODO(sky): better factor this, maybe push to View.
246 blink::WebTreeScopeType scope_;
248 scoped_ptr<WebLayerImpl> web_layer_;
250 HTMLFrameDelegate* delegate_;
251 scoped_ptr<mojo::Binding<mandoline::FrameTreeClient>>
252 frame_tree_client_binding_;
253 mandoline::FrameTreeServerPtr server_;
255 base::WeakPtrFactory<HTMLFrame> weak_factory_;
257 DISALLOW_COPY_AND_ASSIGN(HTMLFrame);
260 } // namespace html_viewer
262 #endif // COMPONENTS_HTML_VIEWER_HTML_FRAME_H_