[Sync] Fix double-tapping signin on Android.
[chromium-blink-merge.git] / components / html_viewer / html_frame.h
bloba293ba31d74d31382fcb526efa77123bbf5f1bce
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 "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"
24 namespace blink {
25 class WebFrame;
28 namespace mojo {
29 class ApplicationImpl;
30 class Rect;
31 class ScopedViewPtr;
32 class View;
35 namespace html_viewer {
37 class GeolocationClientImpl;
38 class HTMLFrameDelegate;
39 class HTMLFrameTreeManager;
40 class TouchHandler;
41 class WebLayerImpl;
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 {
64 public:
65 struct CreateParams {
66 CreateParams(
67 HTMLFrameTreeManager* manager,
68 HTMLFrame* parent,
69 uint32_t id,
70 mojo::View* view,
71 const mojo::Map<mojo::String, mojo::Array<uint8_t>>& properties,
72 HTMLFrameDelegate* delegate)
73 : manager(manager),
74 parent(parent),
75 id(id),
76 view(view),
77 properties(properties),
78 delegate(delegate),
79 allow_local_shared_frame(false) {}
80 ~CreateParams() {}
82 HTMLFrameTreeManager* manager;
83 HTMLFrame* parent;
84 uint32_t id;
85 mojo::View* view;
86 const mojo::Map<mojo::String, mojo::Array<uint8_t>>& properties;
87 HTMLFrameDelegate* delegate;
89 private:
90 friend class HTMLFrame;
92 // TODO(sky): nuke.
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
99 // with.
100 void OnViewUnembed();
102 // Closes and deletes this Frame.
103 void Close();
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
119 // WebRemoteFrame.
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
128 // locally.
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;
136 protected:
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);
194 private:
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
212 // connection.
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();
229 void UpdateFocus();
231 // Updates the size and scale factor of the webview and related classes from
232 // |root_|.
233 void UpdateWebViewSizeFromViewSize();
235 // Swaps this frame from a local frame to remote frame. |request| is the url
236 // to load in the frame.
237 void SwapToRemote();
239 // Swaps this frame from a remote frame to a local frame.
240 void SwapToLocal(
241 HTMLFrameDelegate* delegate,
242 mojo::View* view,
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,
268 uint32_t change_id,
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,
288 float scale_factor);
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_;
295 HTMLFrame* parent_;
296 // |view_| is non-null for local frames or remote frames that were once
297 // local.
298 mojo::View* view_;
299 // The id for this frame. If there is a view, this is the same id as the
300 // view has.
301 const uint32_t id_;
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
327 // be severed.
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_