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 #include "components/html_viewer/html_widget.h"
7 #include "base/command_line.h"
8 #include "components/html_viewer/global_state.h"
9 #include "components/html_viewer/ime_controller.h"
10 #include "components/html_viewer/stats_collection_controller.h"
11 #include "components/html_viewer/web_layer_tree_view_impl.h"
12 #include "components/html_viewer/web_storage_namespace_impl.h"
13 #include "components/mus/public/cpp/view.h"
14 #include "mojo/application/public/cpp/application_impl.h"
15 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h"
16 #include "third_party/WebKit/public/web/WebFrameWidget.h"
17 #include "third_party/WebKit/public/web/WebSettings.h"
18 #include "third_party/WebKit/public/web/WebView.h"
19 #include "ui/gfx/geometry/dip_util.h"
21 namespace html_viewer
{
24 const char kDisableWebGLSwitch
[] = "disable-webgl";
26 scoped_ptr
<WebLayerTreeViewImpl
> CreateWebLayerTreeView(
27 GlobalState
* global_state
) {
28 return make_scoped_ptr(new WebLayerTreeViewImpl(
29 global_state
->compositor_thread(),
30 global_state
->gpu_memory_buffer_manager(),
31 global_state
->raster_thread_helper()->task_graph_runner()));
34 void InitializeWebLayerTreeView(WebLayerTreeViewImpl
* web_layer_tree_view
,
35 mojo::ApplicationImpl
* app
,
37 blink::WebWidget
* widget
) {
39 mojo::URLRequestPtr
request(mojo::URLRequest::New());
40 request
->url
= mojo::String::From("mojo:mus");
41 mojo::GpuPtr gpu_service
;
42 app
->ConnectToService(request
.Pass(), &gpu_service
);
43 web_layer_tree_view
->Initialize(gpu_service
.Pass(), view
, widget
);
46 void UpdateWebViewSizeFromViewSize(mus::View
* view
,
47 blink::WebWidget
* web_widget
,
48 WebLayerTreeViewImpl
* web_layer_tree_view
) {
49 const gfx::Size
size_in_pixels(view
->bounds().width
, view
->bounds().height
);
50 const gfx::Size size_in_dips
= gfx::ConvertSizeToDIP(
51 view
->viewport_metrics().device_pixel_ratio
, size_in_pixels
);
53 blink::WebSize(size_in_dips
.width(), size_in_dips
.height()));
54 web_layer_tree_view
->setViewportSize(size_in_pixels
);
57 void ConfigureSettings(blink::WebSettings
* settings
) {
58 settings
->setCookieEnabled(true);
59 settings
->setDefaultFixedFontSize(13);
60 settings
->setDefaultFontSize(16);
61 settings
->setLoadsImagesAutomatically(true);
62 settings
->setJavaScriptEnabled(true);
64 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
65 settings
->setExperimentalWebGLEnabled(
66 !command_line
->HasSwitch(kDisableWebGLSwitch
));
71 // HTMLWidgetRootRemote -------------------------------------------------------
73 HTMLWidgetRootRemote::HTMLWidgetRootRemote()
74 : web_view_(blink::WebView::create(nullptr)) {
75 ConfigureSettings(web_view_
->settings());
78 HTMLWidgetRootRemote::~HTMLWidgetRootRemote() {}
80 blink::WebWidget
* HTMLWidgetRootRemote::GetWidget() {
84 void HTMLWidgetRootRemote::OnViewBoundsChanged(mus::View
* view
) {}
86 // HTMLWidgetRootLocal --------------------------------------------------------
88 HTMLWidgetRootLocal::CreateParams::CreateParams(mojo::ApplicationImpl
* app
,
89 GlobalState
* global_state
,
91 : app(app
), global_state(global_state
), view(view
) {}
93 HTMLWidgetRootLocal::CreateParams::~CreateParams() {}
95 HTMLWidgetRootLocal::HTMLWidgetRootLocal(CreateParams
* create_params
)
96 : app_(create_params
->app
),
97 global_state_(create_params
->global_state
),
98 view_(create_params
->view
),
100 web_view_
= blink::WebView::create(this);
101 ime_controller_
.reset(new ImeController(view_
, web_view_
));
102 // Creating the widget calls initializeLayerTreeView() to create the
103 // |web_layer_tree_view_impl_|. As we haven't yet assigned the |web_view_|
104 // we have to set it here.
105 if (web_layer_tree_view_impl_
) {
106 InitializeWebLayerTreeView(web_layer_tree_view_impl_
.get(), app_
, view_
,
108 UpdateWebViewSizeFromViewSize(view_
, web_view_
,
109 web_layer_tree_view_impl_
.get());
111 ConfigureSettings(web_view_
->settings());
114 HTMLWidgetRootLocal::~HTMLWidgetRootLocal() {}
116 blink::WebStorageNamespace
*
117 HTMLWidgetRootLocal::createSessionStorageNamespace() {
118 return new WebStorageNamespaceImpl();
121 void HTMLWidgetRootLocal::initializeLayerTreeView() {
122 web_layer_tree_view_impl_
= CreateWebLayerTreeView(global_state_
);
125 blink::WebLayerTreeView
* HTMLWidgetRootLocal::layerTreeView() {
126 return web_layer_tree_view_impl_
.get();
129 void HTMLWidgetRootLocal::didFirstVisuallyNonEmptyLayout() {
130 static bool called
= false;
132 const int64 time
= base::Time::Now().ToInternalValue();
133 tracing::StartupPerformanceDataCollectorPtr collector
=
134 StatsCollectionController::ConnectToDataCollector(app_
);
136 collector
->SetFirstVisuallyNonEmptyLayoutTime(time
);
141 void HTMLWidgetRootLocal::resetInputMethod() {
142 ime_controller_
->ResetInputMethod();
145 void HTMLWidgetRootLocal::didHandleGestureEvent(
146 const blink::WebGestureEvent
& event
,
147 bool event_cancelled
) {
148 ime_controller_
->DidHandleGestureEvent(event
, event_cancelled
);
151 void HTMLWidgetRootLocal::didUpdateTextOfFocusedElementByNonUserInput() {
152 ime_controller_
->DidUpdateTextOfFocusedElementByNonUserInput();
155 void HTMLWidgetRootLocal::showImeIfNeeded() {
156 ime_controller_
->ShowImeIfNeeded();
159 blink::WebWidget
* HTMLWidgetRootLocal::GetWidget() {
163 void HTMLWidgetRootLocal::OnViewBoundsChanged(mus::View
* view
) {
164 UpdateWebViewSizeFromViewSize(view
, web_view_
,
165 web_layer_tree_view_impl_
.get());
168 // HTMLWidgetLocalRoot --------------------------------------------------------
170 HTMLWidgetLocalRoot::HTMLWidgetLocalRoot(mojo::ApplicationImpl
* app
,
171 GlobalState
* global_state
,
173 blink::WebLocalFrame
* web_local_frame
)
174 : app_(app
), global_state_(global_state
), web_frame_widget_(nullptr) {
175 web_frame_widget_
= blink::WebFrameWidget::create(this, web_local_frame
);
176 ime_controller_
.reset(new ImeController(view
, web_frame_widget_
));
177 // Creating the widget calls initializeLayerTreeView() to create the
178 // |web_layer_tree_view_impl_|. As we haven't yet assigned the
179 // |web_frame_widget_|
180 // we have to set it here.
181 if (web_layer_tree_view_impl_
) {
182 InitializeWebLayerTreeView(web_layer_tree_view_impl_
.get(), app_
, view
,
184 UpdateWebViewSizeFromViewSize(view
, web_frame_widget_
,
185 web_layer_tree_view_impl_
.get());
189 HTMLWidgetLocalRoot::~HTMLWidgetLocalRoot() {}
191 blink::WebWidget
* HTMLWidgetLocalRoot::GetWidget() {
192 return web_frame_widget_
;
195 void HTMLWidgetLocalRoot::OnViewBoundsChanged(mus::View
* view
) {
196 UpdateWebViewSizeFromViewSize(view
, web_frame_widget_
,
197 web_layer_tree_view_impl_
.get());
200 void HTMLWidgetLocalRoot::initializeLayerTreeView() {
201 web_layer_tree_view_impl_
= CreateWebLayerTreeView(global_state_
);
204 blink::WebLayerTreeView
* HTMLWidgetLocalRoot::layerTreeView() {
205 return web_layer_tree_view_impl_
.get();
208 void HTMLWidgetLocalRoot::resetInputMethod() {
209 ime_controller_
->ResetInputMethod();
212 void HTMLWidgetLocalRoot::didHandleGestureEvent(
213 const blink::WebGestureEvent
& event
,
214 bool event_cancelled
) {
215 ime_controller_
->DidHandleGestureEvent(event
, event_cancelled
);
218 void HTMLWidgetLocalRoot::didUpdateTextOfFocusedElementByNonUserInput() {
219 ime_controller_
->DidUpdateTextOfFocusedElementByNonUserInput();
222 void HTMLWidgetLocalRoot::showImeIfNeeded() {
223 ime_controller_
->ShowImeIfNeeded();
226 } // namespace html_viewer