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_
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "components/html_viewer/ax_provider_impl.h"
14 #include "components/html_viewer/frame_tree_manager.h"
15 #include "components/html_viewer/touch_handler.h"
16 #include "components/view_manager/public/cpp/view_manager_client_factory.h"
17 #include "components/view_manager/public/cpp/view_manager_delegate.h"
18 #include "components/view_manager/public/cpp/view_observer.h"
19 #include "mandoline/services/navigation/public/interfaces/navigation.mojom.h"
20 #include "mandoline/tab/public/interfaces/frame_tree.mojom.h"
21 #include "mojo/application/public/cpp/app_lifetime_helper.h"
22 #include "mojo/application/public/cpp/interface_factory.h"
23 #include "mojo/application/public/cpp/lazy_interface_ptr.h"
24 #include "mojo/application/public/cpp/service_provider_impl.h"
25 #include "mojo/application/public/interfaces/application.mojom.h"
26 #include "mojo/application/public/interfaces/content_handler.mojom.h"
27 #include "mojo/services/network/public/interfaces/url_loader.mojom.h"
28 #include "third_party/WebKit/public/web/WebFrameClient.h"
29 #include "third_party/WebKit/public/web/WebSandboxFlags.h"
30 #include "third_party/WebKit/public/web/WebViewClient.h"
31 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_impl.h"
34 class SingleThreadTaskRunner
;
42 namespace html_viewer
{
45 class DevToolsAgentImpl
;
47 class WebLayerTreeViewImpl
;
49 // A view for a single HTML document.
50 class HTMLDocument
: public blink::WebViewClient
,
51 public blink::WebFrameClient
,
52 public mojo::ViewManagerDelegate
,
53 public mojo::ViewObserver
,
54 public mojo::InterfaceFactory
<mojo::AxProvider
>,
55 public mojo::InterfaceFactory
<mandoline::FrameTreeClient
> {
57 // Load a new HTMLDocument with |response|.
58 // |html_document_app| is the application this app was created in, and
59 // |connection| the specific connection triggering this new instance.
60 // |setup| is used to obtain init type state (such as resources).
61 HTMLDocument(mojo::ApplicationImpl
* html_document_app
,
62 mojo::ApplicationConnection
* connection
,
63 mojo::URLResponsePtr response
,
65 ~HTMLDocument() override
;
68 // Data associated with a child iframe.
69 struct ChildFrameData
{
71 blink::WebTreeScopeType scope
;
74 using FrameToViewMap
= std::map
<blink::WebLocalFrame
*, ChildFrameData
>;
76 // Updates the size and scale factor of the webview and related classes from
78 void UpdateWebviewSizeFromViewSize();
80 void InitSetupAndLoadIfNecessary();
82 // WebViewClient methods:
83 virtual blink::WebStorageNamespace
* createSessionStorageNamespace();
85 // WebWidgetClient methods:
86 void initializeLayerTreeView() override
;
87 blink::WebLayerTreeView
* layerTreeView() override
;
89 // WebFrameClient methods:
90 virtual blink::WebMediaPlayer
* createMediaPlayer(
91 blink::WebLocalFrame
* frame
,
92 const blink::WebURL
& url
,
93 blink::WebMediaPlayerClient
* client
,
94 blink::WebContentDecryptionModule
* initial_cdm
);
95 virtual blink::WebFrame
* createChildFrame(
96 blink::WebLocalFrame
* parent
,
97 blink::WebTreeScopeType scope
,
98 const blink::WebString
& frameName
,
99 blink::WebSandboxFlags sandboxFlags
);
100 virtual void frameDetached(blink::WebFrame
* frame
);
101 virtual void frameDetached(blink::WebFrame
* frame
, DetachType type
);
102 virtual blink::WebCookieJar
* cookieJar(blink::WebLocalFrame
* frame
);
103 virtual blink::WebNavigationPolicy
decidePolicyForNavigation(
104 const NavigationPolicyInfo
& info
);
106 virtual void didAddMessageToConsole(const blink::WebConsoleMessage
& message
,
107 const blink::WebString
& source_name
,
108 unsigned source_line
,
109 const blink::WebString
& stack_trace
);
110 virtual void didFinishLoad(blink::WebLocalFrame
* frame
);
111 virtual void didNavigateWithinPage(blink::WebLocalFrame
* frame
,
112 const blink::WebHistoryItem
& history_item
,
113 blink::WebHistoryCommitType commit_type
);
114 virtual blink::WebEncryptedMediaClient
* encryptedMediaClient();
116 // ViewManagerDelegate methods:
117 void OnEmbed(mojo::View
* root
) override
;
118 void OnViewManagerDestroyed(mojo::ViewManager
* view_manager
) override
;
120 // ViewObserver methods:
121 void OnViewBoundsChanged(mojo::View
* view
,
122 const mojo::Rect
& old_bounds
,
123 const mojo::Rect
& new_bounds
) override
;
124 void OnViewViewportMetricsChanged(
126 const mojo::ViewportMetrics
& old_metrics
,
127 const mojo::ViewportMetrics
& new_metrics
) override
;
128 void OnViewDestroyed(mojo::View
* view
) override
;
129 void OnViewInputEvent(mojo::View
* view
, const mojo::EventPtr
& event
) override
;
130 void OnViewFocusChanged(mojo::View
* gained_focus
,
131 mojo::View
* lost_focus
) override
;
133 // mojo::InterfaceFactory<mojo::AxProvider>
134 void Create(mojo::ApplicationConnection
* connection
,
135 mojo::InterfaceRequest
<mojo::AxProvider
> request
) override
;
137 // mojo::InterfaceFactory<mandoline::FrameTreeClient>
139 mojo::ApplicationConnection
* connection
,
140 mojo::InterfaceRequest
<mandoline::FrameTreeClient
> request
) override
;
142 void Load(mojo::URLResponsePtr response
);
144 // Converts a WebLocalFrame to a WebRemoteFrame. Used once we know the
145 // url of a frame to trigger the navigation.
146 void ConvertLocalFrameToRemoteFrame(blink::WebLocalFrame
* frame
);
148 // Updates the focus state of |web_view_| based on the focus state of |root_|.
151 scoped_ptr
<mojo::AppRefCount
> app_refcount_
;
152 mojo::ApplicationImpl
* html_document_app_
;
153 mojo::URLResponsePtr response_
;
154 mojo::LazyInterfacePtr
<mojo::NavigatorHost
> navigator_host_
;
155 blink::WebView
* web_view_
;
157 mojo::ViewManagerClientFactory view_manager_client_factory_
;
158 scoped_ptr
<WebLayerTreeViewImpl
> web_layer_tree_view_impl_
;
159 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_thread_
;
161 // HTMLDocument owns these pointers; binding requests after document load.
162 std::set
<mojo::InterfaceRequest
<mojo::AxProvider
>*> ax_provider_requests_
;
163 std::set
<AxProviderImpl
*> ax_providers_
;
165 // A flag set on didFinishLoad.
166 bool did_finish_load_
= false;
170 scoped_ptr
<TouchHandler
> touch_handler_
;
172 FrameToViewMap frame_to_view_
;
174 FrameTreeManager frame_tree_manager_
;
175 mojo::Binding
<mandoline::FrameTreeClient
> frame_tree_manager_binding_
;
177 scoped_ptr
<DevToolsAgentImpl
> devtools_agent_
;
179 DISALLOW_COPY_AND_ASSIGN(HTMLDocument
);
182 } // namespace html_viewer
184 #endif // COMPONENTS_HTML_VIEWER_HTML_DOCUMENT_H_