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 "athena/virtual_keyboard/vk_webui_controller.h"
7 #include "athena/virtual_keyboard/vk_message_handler.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/browser/web_ui_data_source.h"
12 #include "grit/keyboard_resources.h"
13 #include "grit/keyboard_resources_map.h"
14 #include "ui/keyboard/keyboard_constants.h"
15 #include "ui/keyboard/keyboard_util.h"
21 content::WebUIDataSource
* CreateKeyboardUIDataSource() {
22 content::WebUIDataSource
* source
=
23 content::WebUIDataSource::Create(keyboard::kKeyboardHost
);
26 const GritResourceMap
* resources
=
27 keyboard::GetKeyboardExtensionResources(&count
);
28 source
->SetDefaultResource(IDR_KEYBOARD_INDEX
);
30 const std::string keyboard_host
=
31 base::StringPrintf("%s/", keyboard::kKeyboardHost
);
32 for (size_t i
= 0; i
< count
; ++i
) {
34 if (StartsWithASCII(std::string(resources
[i
].name
), keyboard_host
, false))
35 offset
= keyboard_host
.length();
36 source
->AddResourcePath(resources
[i
].name
+ offset
, resources
[i
].value
);
43 ////////////////////////////////////////////////////////////////////////////////
46 VKWebUIController::VKWebUIController(content::WebUI
* web_ui
)
47 : WebUIController(web_ui
) {
48 content::BrowserContext
* browser_context
=
49 web_ui
->GetWebContents()->GetBrowserContext();
50 web_ui
->AddMessageHandler(new VKMessageHandler());
51 content::WebUIDataSource::Add(browser_context
, CreateKeyboardUIDataSource());
54 VKWebUIController::~VKWebUIController() {
57 ////////////////////////////////////////////////////////////////////////////////
58 // VKWebUIControllerFactory:
60 content::WebUI::TypeID
VKWebUIControllerFactory::GetWebUIType(
61 content::BrowserContext
* browser_context
,
62 const GURL
& url
) const {
63 if (url
== GURL(keyboard::kKeyboardURL
))
64 return const_cast<VKWebUIControllerFactory
*>(this);
66 return content::WebUI::kNoWebUI
;
69 bool VKWebUIControllerFactory::UseWebUIForURL(
70 content::BrowserContext
* browser_context
,
71 const GURL
& url
) const {
72 return GetWebUIType(browser_context
, url
) != content::WebUI::kNoWebUI
;
75 bool VKWebUIControllerFactory::UseWebUIBindingsForURL(
76 content::BrowserContext
* browser_context
,
77 const GURL
& url
) const {
78 return UseWebUIForURL(browser_context
, url
);
81 content::WebUIController
* VKWebUIControllerFactory::CreateWebUIControllerForURL(
82 content::WebUI
* web_ui
,
83 const GURL
& url
) const {
84 if (url
== GURL(keyboard::kKeyboardURL
))
85 return new VKWebUIController(web_ui
);
90 VKWebUIControllerFactory
* VKWebUIControllerFactory::GetInstance() {
91 return Singleton
<VKWebUIControllerFactory
>::get();
95 VKWebUIControllerFactory::VKWebUIControllerFactory() {
98 VKWebUIControllerFactory::~VKWebUIControllerFactory() {
101 } // namespace athena