• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ui/keyboard/webui/vk_webui_controller.h"
6 
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"
21 
22 namespace keyboard {
23 
24 namespace {
25 
CreateKeyboardUIDataSource()26 content::WebUIDataSource* CreateKeyboardUIDataSource() {
27   content::WebUIDataSource* source =
28       content::WebUIDataSource::Create(kKeyboardHost);
29 
30   size_t count = 0;
31   const GritResourceMap* resources = GetKeyboardExtensionResources(&count);
32   source->SetDefaultResource(IDR_KEYBOARD_INDEX);
33 
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);
42   }
43   return source;
44 }
45 
46 }  // namespace
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 // VKWebUIController:
50 
VKWebUIController(content::WebUI * web_ui)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);
58 }
59 
~VKWebUIController()60 VKWebUIController::~VKWebUIController() {
61 }
62 
RenderViewCreated(content::RenderViewHost * host)63 void VKWebUIController::RenderViewCreated(content::RenderViewHost* host) {
64   host->GetMainFrame()->GetServiceRegistry()->AddService<KeyboardUIHandlerMojo>(
65       base::Bind(&VKWebUIController::CreateAndStoreUIHandler,
66                  weak_factory_.GetWeakPtr()));
67 }
68 
CreateAndStoreUIHandler(mojo::InterfaceRequest<KeyboardUIHandlerMojo> request)69 void VKWebUIController::CreateAndStoreUIHandler(
70     mojo::InterfaceRequest<KeyboardUIHandlerMojo> request) {
71   ui_handler_ = scoped_ptr<VKMojoHandler>(
72       mojo::WeakBindToRequest(new VKMojoHandler(), &request));
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 // VKWebUIControllerFactory:
77 
GetWebUIType(content::BrowserContext * browser_context,const GURL & url) const78 content::WebUI::TypeID VKWebUIControllerFactory::GetWebUIType(
79     content::BrowserContext* browser_context,
80     const GURL& url) const {
81   if (url == GURL(kKeyboardURL))
82     return const_cast<VKWebUIControllerFactory*>(this);
83 
84   return content::WebUI::kNoWebUI;
85 }
86 
UseWebUIForURL(content::BrowserContext * browser_context,const GURL & url) const87 bool VKWebUIControllerFactory::UseWebUIForURL(
88     content::BrowserContext* browser_context,
89     const GURL& url) const {
90   return GetWebUIType(browser_context, url) != content::WebUI::kNoWebUI;
91 }
92 
UseWebUIBindingsForURL(content::BrowserContext * browser_context,const GURL & url) const93 bool VKWebUIControllerFactory::UseWebUIBindingsForURL(
94     content::BrowserContext* browser_context,
95     const GURL& url) const {
96   return UseWebUIForURL(browser_context, url);
97 }
98 
CreateWebUIControllerForURL(content::WebUI * web_ui,const GURL & url) const99 content::WebUIController* VKWebUIControllerFactory::CreateWebUIControllerForURL(
100     content::WebUI* web_ui,
101     const GURL& url) const {
102   if (url == GURL(kKeyboardURL))
103     return new VKWebUIController(web_ui);
104   return NULL;
105 }
106 
107 // static
GetInstance()108 VKWebUIControllerFactory* VKWebUIControllerFactory::GetInstance() {
109   return Singleton<VKWebUIControllerFactory>::get();
110 }
111 
112 // protected
VKWebUIControllerFactory()113 VKWebUIControllerFactory::VKWebUIControllerFactory() {
114 }
115 
~VKWebUIControllerFactory()116 VKWebUIControllerFactory::~VKWebUIControllerFactory() {
117 }
118 
119 }  // namespace keyboard
120