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"
28 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_impl.h"
31 class SingleThreadTaskRunner
;
39 namespace html_viewer
{
42 class DevToolsAgentImpl
;
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.
84 // Data associated with a child iframe.
85 struct ChildFrameData
{
87 blink::WebTreeScopeType scope
;
90 ~HTMLDocument() override
;
92 // Updates the size and scale factor of the webview and related classes from
94 void UpdateWebviewSizeFromViewSize();
96 void InitGlobalStateAndLoadIfNecessary();
98 // WebViewClient methods:
99 virtual blink::WebStorageNamespace
* createSessionStorageNamespace();
101 // WebWidgetClient methods:
102 void initializeLayerTreeView() override
;
103 blink::WebLayerTreeView
* layerTreeView() override
;
105 // WebFrameClient methods:
106 virtual blink::WebMediaPlayer
* createMediaPlayer(
107 blink::WebLocalFrame
* frame
,
108 const blink::WebURL
& url
,
109 blink::WebMediaPlayerClient
* client
,
110 blink::WebContentDecryptionModule
* initial_cdm
);
111 virtual blink::WebFrame
* createChildFrame(
112 blink::WebLocalFrame
* parent
,
113 blink::WebTreeScopeType scope
,
114 const blink::WebString
& frameName
,
115 blink::WebSandboxFlags sandboxFlags
);
116 virtual void frameDetached(blink::WebFrame
* frame
);
117 virtual void frameDetached(blink::WebFrame
* frame
, DetachType type
);
118 virtual blink::WebCookieJar
* cookieJar(blink::WebLocalFrame
* frame
);
119 virtual blink::WebNavigationPolicy
decidePolicyForNavigation(
120 const NavigationPolicyInfo
& info
);
122 virtual void didAddMessageToConsole(const blink::WebConsoleMessage
& message
,
123 const blink::WebString
& source_name
,
124 unsigned source_line
,
125 const blink::WebString
& stack_trace
);
126 virtual void didFinishLoad(blink::WebLocalFrame
* frame
);
127 virtual void didNavigateWithinPage(blink::WebLocalFrame
* frame
,
128 const blink::WebHistoryItem
& history_item
,
129 blink::WebHistoryCommitType commit_type
);
130 virtual blink::WebEncryptedMediaClient
* encryptedMediaClient();
132 // ViewManagerDelegate methods:
133 void OnEmbed(mojo::View
* root
) override
;
134 void OnViewManagerDestroyed(mojo::ViewManager
* view_manager
) override
;
136 // ViewObserver methods:
137 void OnViewBoundsChanged(mojo::View
* view
,
138 const mojo::Rect
& old_bounds
,
139 const mojo::Rect
& new_bounds
) override
;
140 void OnViewViewportMetricsChanged(
142 const mojo::ViewportMetrics
& old_metrics
,
143 const mojo::ViewportMetrics
& new_metrics
) override
;
144 void OnViewDestroyed(mojo::View
* view
) override
;
145 void OnViewInputEvent(mojo::View
* view
, const mojo::EventPtr
& event
) override
;
146 void OnViewFocusChanged(mojo::View
* gained_focus
,
147 mojo::View
* lost_focus
) override
;
149 // mojo::InterfaceFactory<mojo::AxProvider>
150 void Create(mojo::ApplicationConnection
* connection
,
151 mojo::InterfaceRequest
<mojo::AxProvider
> request
) override
;
153 void Load(mojo::URLResponsePtr response
);
155 // Converts a WebLocalFrame to a WebRemoteFrame. Used once we know the
156 // url of a frame to trigger the navigation.
157 void ConvertLocalFrameToRemoteFrame(blink::WebLocalFrame
* frame
);
159 // Updates the focus state of |web_view_| based on the focus state of |root_|.
162 scoped_ptr
<mojo::AppRefCount
> app_refcount_
;
163 mojo::ApplicationImpl
* html_document_app_
;
164 mojo::URLResponsePtr response_
;
165 mojo::LazyInterfacePtr
<mojo::NavigatorHost
> navigator_host_
;
166 blink::WebView
* web_view_
;
168 mojo::ViewManagerClientFactory view_manager_client_factory_
;
169 scoped_ptr
<WebLayerTreeViewImpl
> web_layer_tree_view_impl_
;
170 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_thread_
;
172 // HTMLDocument owns these pointers; binding requests after document load.
173 std::set
<mojo::InterfaceRequest
<mojo::AxProvider
>*> ax_provider_requests_
;
174 std::set
<AxProviderImpl
*> ax_providers_
;
176 // A flag set on didFinishLoad.
177 bool did_finish_load_
= false;
179 GlobalState
* global_state_
;
181 scoped_ptr
<TouchHandler
> touch_handler_
;
183 scoped_ptr
<DevToolsAgentImpl
> devtools_agent_
;
185 DeleteCallback delete_callback_
;
187 DISALLOW_COPY_AND_ASSIGN(HTMLDocument
);
190 } // namespace html_viewer
192 #endif // COMPONENTS_HTML_VIEWER_HTML_DOCUMENT_H_