Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / ui / keyboard / webui / vk_webui_controller.cc
blob5fd04ce85dba6f0e66dcd93b6be2643a565e085b
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"
22 namespace keyboard {
24 namespace {
26 content::WebUIDataSource* CreateKeyboardUIDataSource() {
27 content::WebUIDataSource* source =
28 content::WebUIDataSource::Create(kKeyboardHost);
30 size_t count = 0;
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) {
36 size_t offset = 0;
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);
43 return source;
46 } // namespace
48 ////////////////////////////////////////////////////////////////////////////////
49 // VKWebUIController:
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);
103 return NULL;
106 // static
107 VKWebUIControllerFactory* VKWebUIControllerFactory::GetInstance() {
108 return Singleton<VKWebUIControllerFactory>::get();
111 // protected
112 VKWebUIControllerFactory::VKWebUIControllerFactory() {
115 VKWebUIControllerFactory::~VKWebUIControllerFactory() {
118 } // namespace keyboard