OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "athena/virtual_keyboard/vk_webui_controller.h" | |
6 | |
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" | |
16 | |
17 namespace athena { | |
18 | |
19 namespace { | |
20 | |
21 content::WebUIDataSource* CreateKeyboardUIDataSource() { | |
22 content::WebUIDataSource* source = | |
23 content::WebUIDataSource::Create(keyboard::kKeyboardHost); | |
24 | |
25 size_t count = 0; | |
26 const GritResourceMap* resources = | |
27 keyboard::GetKeyboardExtensionResources(&count); | |
28 source->SetDefaultResource(IDR_KEYBOARD_INDEX); | |
29 | |
30 const std::string keyboard_host = | |
31 base::StringPrintf("%s/", keyboard::kKeyboardHost); | |
32 for (size_t i = 0; i < count; ++i) { | |
33 size_t offset = 0; | |
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); | |
37 } | |
38 return source; | |
39 } | |
40 | |
41 } // namespace | |
42 | |
43 //////////////////////////////////////////////////////////////////////////////// | |
44 // VKWebUIController: | |
45 | |
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()); | |
52 } | |
53 | |
54 VKWebUIController::~VKWebUIController() { | |
55 } | |
56 | |
57 //////////////////////////////////////////////////////////////////////////////// | |
58 // VKWebUIControllerFactory: | |
59 | |
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); | |
65 | |
66 return content::WebUI::kNoWebUI; | |
67 } | |
68 | |
69 bool VKWebUIControllerFactory::UseWebUIForURL( | |
70 content::BrowserContext* browser_context, | |
71 const GURL& url) const { | |
72 return GetWebUIType(browser_context, url) != content::WebUI::kNoWebUI; | |
73 } | |
74 | |
75 bool VKWebUIControllerFactory::UseWebUIBindingsForURL( | |
76 content::BrowserContext* browser_context, | |
77 const GURL& url) const { | |
78 return UseWebUIForURL(browser_context, url); | |
79 } | |
80 | |
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); | |
86 return NULL; | |
87 } | |
88 | |
89 // static | |
90 VKWebUIControllerFactory* VKWebUIControllerFactory::GetInstance() { | |
91 return Singleton<VKWebUIControllerFactory>::get(); | |
92 } | |
93 | |
94 // protected | |
95 VKWebUIControllerFactory::VKWebUIControllerFactory() { | |
96 } | |
97 | |
98 VKWebUIControllerFactory::~VKWebUIControllerFactory() { | |
99 } | |
100 | |
101 } // namespace athena | |
OLD | NEW |