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 #include "ui/keyboard/webui/vk_webui_controller.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/stringprintf.h"
9 #include "content/public/browser/render_frame_host.h"
10 #include "content/public/browser/render_view_host.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/browser/web_ui_data_source.h"
13 #include "content/public/common/service_registry.h"
14 #include "grit/keyboard_resources.h"
15 #include "grit/keyboard_resources_map.h"
16 #include "mojo/public/cpp/bindings/interface_impl.h"
17 #include "mojo/public/cpp/system/core.h"
18 #include "ui/keyboard/keyboard_constants.h"
19 #include "ui/keyboard/keyboard_util.h"
20 #include "ui/keyboard/webui/vk_mojo_handler.h"
26 content::WebUIDataSource
* CreateKeyboardUIDataSource() {
27 content::WebUIDataSource
* source
=
28 content::WebUIDataSource::Create(kKeyboardHost
);
31 const GritResourceMap
* resources
= GetKeyboardExtensionResources(&count
);
32 source
->SetDefaultResource(IDR_KEYBOARD_INDEX
);
34 const std::string keyboard_host
= base::StringPrintf("%s/", kKeyboardHost
);
35 for (size_t i
= 0; i
< count
; ++i
) {
37 // The webui URL needs to skip the 'keyboard/' at the front of the resource
38 // names, since it is part of the data-source name.
39 if (StartsWithASCII(std::string(resources
[i
].name
), keyboard_host
, false))
40 offset
= keyboard_host
.length();
41 source
->AddResourcePath(resources
[i
].name
+ offset
, resources
[i
].value
);
48 ////////////////////////////////////////////////////////////////////////////////
51 VKWebUIController::VKWebUIController(content::WebUI
* web_ui
)
52 : WebUIController(web_ui
), weak_factory_(this) {
53 content::BrowserContext
* browser_context
=
54 web_ui
->GetWebContents()->GetBrowserContext();
55 content::WebUIDataSource::Add(browser_context
, CreateKeyboardUIDataSource());
56 content::WebUIDataSource::AddMojoDataSource(browser_context
)->AddResourcePath(
57 "ui/keyboard/webui/keyboard.mojom", IDR_KEYBOARD_MOJO_GEN_JS
);
60 VKWebUIController::~VKWebUIController() {
63 void VKWebUIController::RenderViewCreated(content::RenderViewHost
* host
) {
64 host
->GetMainFrame()->GetServiceRegistry()->AddService
<KeyboardUIHandlerMojo
>(
65 base::Bind(&VKWebUIController::CreateAndStoreUIHandler
,
66 weak_factory_
.GetWeakPtr()));
69 void VKWebUIController::CreateAndStoreUIHandler(
70 mojo::InterfaceRequest
<KeyboardUIHandlerMojo
> request
) {
71 ui_handler_
= make_scoped_ptr(new VKMojoHandler(request
.Pass()));
74 ////////////////////////////////////////////////////////////////////////////////
75 // VKWebUIControllerFactory:
77 content::WebUI::TypeID
VKWebUIControllerFactory::GetWebUIType(
78 content::BrowserContext
* browser_context
,
79 const GURL
& url
) const {
80 if (url
== GURL(kKeyboardURL
))
81 return const_cast<VKWebUIControllerFactory
*>(this);
83 return content::WebUI::kNoWebUI
;
86 bool VKWebUIControllerFactory::UseWebUIForURL(
87 content::BrowserContext
* browser_context
,
88 const GURL
& url
) const {
89 return GetWebUIType(browser_context
, url
) != content::WebUI::kNoWebUI
;
92 bool VKWebUIControllerFactory::UseWebUIBindingsForURL(
93 content::BrowserContext
* browser_context
,
94 const GURL
& url
) const {
95 return UseWebUIForURL(browser_context
, url
);
98 content::WebUIController
* VKWebUIControllerFactory::CreateWebUIControllerForURL(
99 content::WebUI
* web_ui
,
100 const GURL
& url
) const {
101 if (url
== GURL(kKeyboardURL
))
102 return new VKWebUIController(web_ui
);
107 VKWebUIControllerFactory
* VKWebUIControllerFactory::GetInstance() {
108 return Singleton
<VKWebUIControllerFactory
>::get();
112 VKWebUIControllerFactory::VKWebUIControllerFactory() {
115 VKWebUIControllerFactory::~VKWebUIControllerFactory() {
118 } // namespace keyboard