1 // Copyright 2014 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_DOCUMENT_H_
6 #define COMPONENTS_HTML_VIEWER_HTML_DOCUMENT_H_
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "components/html_viewer/ax_provider_impl.h"
13 #include "components/html_viewer/touch_handler.h"
14 #include "components/view_manager/public/cpp/view_manager_client_factory.h"
15 #include "components/view_manager/public/cpp/view_manager_delegate.h"
16 #include "components/view_manager/public/cpp/view_observer.h"
17 #include "mandoline/services/navigation/public/interfaces/navigation.mojom.h"
18 #include "mojo/application/public/cpp/app_lifetime_helper.h"
19 #include "mojo/application/public/cpp/interface_factory.h"
20 #include "mojo/application/public/cpp/lazy_interface_ptr.h"
21 #include "mojo/application/public/cpp/service_provider_impl.h"
22 #include "mojo/application/public/interfaces/application.mojom.h"
23 #include "mojo/application/public/interfaces/content_handler.mojom.h"
24 #include "mojo/services/network/public/interfaces/url_loader.mojom.h"
25 #include "third_party/WebKit/public/web/WebFrameClient.h"
26 #include "third_party/WebKit/public/web/WebSandboxFlags.h"
27 #include "third_party/WebKit/public/web/WebViewClient.h"
30 class SingleThreadTaskRunner
;
38 namespace html_viewer
{
41 class DevToolsAgentImpl
;
42 class GeolocationClientImpl
;
44 class WebLayerTreeViewImpl
;
46 // A view for a single HTML document.
48 // HTMLDocument is deleted in one of two ways:
49 // . When the View the HTMLDocument is embedded in is destroyed.
50 // . Explicitly by way of Destroy().
51 class HTMLDocument
: public blink::WebViewClient
,
52 public blink::WebFrameClient
,
53 public mojo::ViewManagerDelegate
,
54 public mojo::ViewObserver
,
55 public mojo::InterfaceFactory
<mojo::AxProvider
> {
57 using DeleteCallback
= base::Callback
<void(HTMLDocument
*)>;
60 CreateParams(mojo::ApplicationImpl
* html_document_app
,
61 mojo::ApplicationConnection
* connection
,
62 mojo::URLResponsePtr response
,
63 GlobalState
* global_state
,
64 const DeleteCallback
& delete_callback
);
67 mojo::ApplicationImpl
* html_document_app
;
68 mojo::ApplicationConnection
* connection
;
69 mojo::URLResponsePtr response
;
70 GlobalState
* global_state
;
71 DeleteCallback delete_callback
;
74 // Load a new HTMLDocument with |response|.
75 // |html_document_app| is the application this app was created in, and
76 // |connection| the specific connection triggering this new instance.
77 // |setup| is used to obtain init type state (such as resources).
78 explicit HTMLDocument(CreateParams
* params
);
80 // Deletes this object.
83 blink::WebView
* web_view() const { return web_view_
; }
86 ~HTMLDocument() override
;
88 // WebViewClient methods:
89 virtual blink::WebStorageNamespace
* createSessionStorageNamespace();
91 // WebWidgetClient methods:
92 void initializeLayerTreeView() override
;
93 blink::WebLayerTreeView
* layerTreeView() override
;
95 // WebFrameClient methods:
96 virtual blink::WebMediaPlayer
* createMediaPlayer(
97 blink::WebLocalFrame
* frame
,
98 const blink::WebURL
& url
,
99 blink::WebMediaPlayerClient
* client
,
100 blink::WebMediaPlayerEncryptedMediaClient
* encrypted_client
,
101 blink::WebContentDecryptionModule
* initial_cdm
);
102 virtual blink::WebFrame
* createChildFrame(
103 blink::WebLocalFrame
* parent
,
104 blink::WebTreeScopeType scope
,
105 const blink::WebString
& frameName
,
106 blink::WebSandboxFlags sandboxFlags
);
107 virtual void frameDetached(blink::WebFrame
* frame
, DetachType type
);
108 virtual blink::WebCookieJar
* cookieJar(blink::WebLocalFrame
* frame
);
109 virtual blink::WebNavigationPolicy
decidePolicyForNavigation(
110 const NavigationPolicyInfo
& info
);
111 virtual blink::WebGeolocationClient
* geolocationClient();
113 virtual void didAddMessageToConsole(const blink::WebConsoleMessage
& message
,
114 const blink::WebString
& source_name
,
115 unsigned source_line
,
116 const blink::WebString
& stack_trace
);
117 virtual void didFinishLoad(blink::WebLocalFrame
* frame
);
118 virtual void didNavigateWithinPage(blink::WebLocalFrame
* frame
,
119 const blink::WebHistoryItem
& history_item
,
120 blink::WebHistoryCommitType commit_type
);
121 virtual blink::WebEncryptedMediaClient
* encryptedMediaClient();
124 // Data associated with a child iframe.
125 struct ChildFrameData
{
127 blink::WebTreeScopeType scope
;
130 // Updates the size and scale factor of the webview and related classes from
132 void UpdateWebviewSizeFromViewSize();
134 void InitGlobalStateAndLoadIfNecessary();
136 // ViewManagerDelegate methods:
137 void OnEmbed(mojo::View
* root
) override
;
138 void OnViewManagerDestroyed(mojo::ViewManager
* view_manager
) override
;
140 // ViewObserver methods:
141 void OnViewBoundsChanged(mojo::View
* view
,
142 const mojo::Rect
& old_bounds
,
143 const mojo::Rect
& new_bounds
) override
;
144 void OnViewViewportMetricsChanged(
146 const mojo::ViewportMetrics
& old_metrics
,
147 const mojo::ViewportMetrics
& new_metrics
) override
;
148 void OnViewDestroyed(mojo::View
* view
) override
;
149 void OnViewInputEvent(mojo::View
* view
, const mojo::EventPtr
& event
) override
;
150 void OnViewFocusChanged(mojo::View
* gained_focus
,
151 mojo::View
* lost_focus
) override
;
153 // mojo::InterfaceFactory<mojo::AxProvider>
154 void Create(mojo::ApplicationConnection
* connection
,
155 mojo::InterfaceRequest
<mojo::AxProvider
> request
) override
;
157 void Load(mojo::URLResponsePtr response
);
159 // Converts a WebLocalFrame to a WebRemoteFrame. Used once we know the
160 // url of a frame to trigger the navigation.
161 void ConvertLocalFrameToRemoteFrame(blink::WebLocalFrame
* frame
);
163 // Updates the focus state of |web_view_| based on the focus state of |root_|.
166 scoped_ptr
<mojo::AppRefCount
> app_refcount_
;
167 mojo::ApplicationImpl
* html_document_app_
;
168 mojo::URLResponsePtr response_
;
169 mojo::LazyInterfacePtr
<mojo::NavigatorHost
> navigator_host_
;
170 blink::WebView
* web_view_
;
172 mojo::ViewManagerClientFactory view_manager_client_factory_
;
173 scoped_ptr
<WebLayerTreeViewImpl
> web_layer_tree_view_impl_
;
174 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_thread_
;
175 scoped_ptr
<GeolocationClientImpl
> geolocation_client_impl_
;
177 // HTMLDocument owns these pointers; binding requests after document load.
178 std::set
<mojo::InterfaceRequest
<mojo::AxProvider
>*> ax_provider_requests_
;
179 std::set
<AxProviderImpl
*> ax_providers_
;
181 // A flag set on didFinishLoad.
182 bool did_finish_load_
= false;
184 GlobalState
* global_state_
;
186 scoped_ptr
<TouchHandler
> touch_handler_
;
188 scoped_ptr
<DevToolsAgentImpl
> devtools_agent_
;
190 DeleteCallback delete_callback_
;
192 DISALLOW_COPY_AND_ASSIGN(HTMLDocument
);
195 } // namespace html_viewer
197 #endif // COMPONENTS_HTML_VIEWER_HTML_DOCUMENT_H_