Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "athena/virtual_keyboard/vk_webui_controller.h" | 5 #include "ui/keyboard/webui/vk_webui_controller.h" |
| 6 | 6 |
| 7 #include "athena/virtual_keyboard/vk_message_handler.h" | |
| 8 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 9 #include "base/strings/stringprintf.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" | |
| 10 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 11 #include "content/public/browser/web_ui_data_source.h" | 12 #include "content/public/browser/web_ui_data_source.h" |
| 13 #include "content/public/common/service_registry.h" | |
| 12 #include "grit/keyboard_resources.h" | 14 #include "grit/keyboard_resources.h" |
| 13 #include "grit/keyboard_resources_map.h" | 15 #include "grit/keyboard_resources_map.h" |
| 16 #include "mojo/public/cpp/system/core.h" | |
| 14 #include "ui/keyboard/keyboard_constants.h" | 17 #include "ui/keyboard/keyboard_constants.h" |
| 15 #include "ui/keyboard/keyboard_util.h" | 18 #include "ui/keyboard/keyboard_util.h" |
| 19 #include "ui/keyboard/webui/vk_mojo_handler.h" | |
| 16 | 20 |
| 17 namespace athena { | 21 namespace keyboard { |
| 18 | 22 |
| 19 namespace { | 23 namespace { |
| 20 | 24 |
| 21 content::WebUIDataSource* CreateKeyboardUIDataSource() { | 25 content::WebUIDataSource* CreateKeyboardUIDataSource() { |
| 22 content::WebUIDataSource* source = | 26 content::WebUIDataSource* source = |
| 23 content::WebUIDataSource::Create(keyboard::kKeyboardHost); | 27 content::WebUIDataSource::Create(kKeyboardHost); |
| 24 | 28 |
| 25 size_t count = 0; | 29 size_t count = 0; |
| 26 const GritResourceMap* resources = | 30 const GritResourceMap* resources = GetKeyboardExtensionResources(&count); |
| 27 keyboard::GetKeyboardExtensionResources(&count); | |
| 28 source->SetDefaultResource(IDR_KEYBOARD_INDEX); | 31 source->SetDefaultResource(IDR_KEYBOARD_INDEX); |
| 29 | 32 |
| 30 const std::string keyboard_host = | 33 const std::string keyboard_host = base::StringPrintf("%s/", kKeyboardHost); |
| 31 base::StringPrintf("%s/", keyboard::kKeyboardHost); | |
| 32 for (size_t i = 0; i < count; ++i) { | 34 for (size_t i = 0; i < count; ++i) { |
| 33 size_t offset = 0; | 35 size_t offset = 0; |
| 36 // The webui URL needs to skip the 'keyboard/' at the front of the resource | |
| 37 // names, since it is part of the data-source name. | |
| 34 if (StartsWithASCII(std::string(resources[i].name), keyboard_host, false)) | 38 if (StartsWithASCII(std::string(resources[i].name), keyboard_host, false)) |
| 35 offset = keyboard_host.length(); | 39 offset = keyboard_host.length(); |
| 36 source->AddResourcePath(resources[i].name + offset, resources[i].value); | 40 source->AddResourcePath(resources[i].name + offset, resources[i].value); |
| 37 } | 41 } |
| 38 return source; | 42 return source; |
| 39 } | 43 } |
| 40 | 44 |
| 41 } // namespace | 45 } // namespace |
| 42 | 46 |
| 43 //////////////////////////////////////////////////////////////////////////////// | 47 //////////////////////////////////////////////////////////////////////////////// |
| 44 // VKWebUIController: | 48 // VKWebUIController: |
| 45 | 49 |
| 46 VKWebUIController::VKWebUIController(content::WebUI* web_ui) | 50 VKWebUIController::VKWebUIController(content::WebUI* web_ui) |
| 47 : WebUIController(web_ui) { | 51 : WebUIController(web_ui), |
| 52 weak_factory_(this) { | |
| 48 content::BrowserContext* browser_context = | 53 content::BrowserContext* browser_context = |
| 49 web_ui->GetWebContents()->GetBrowserContext(); | 54 web_ui->GetWebContents()->GetBrowserContext(); |
| 50 web_ui->AddMessageHandler(new VKMessageHandler()); | |
| 51 content::WebUIDataSource::Add(browser_context, CreateKeyboardUIDataSource()); | 55 content::WebUIDataSource::Add(browser_context, CreateKeyboardUIDataSource()); |
| 56 content::WebUIDataSource::AddMojoDataSource(browser_context) | |
| 57 ->AddResourcePath("ui/keyboard/webui/keyboard.mojom", | |
| 58 IDR_KEYBOARD_MOJO_GEN_JS); | |
| 52 } | 59 } |
| 53 | 60 |
| 54 VKWebUIController::~VKWebUIController() { | 61 VKWebUIController::~VKWebUIController() { |
| 55 } | 62 } |
| 56 | 63 |
| 64 void VKWebUIController::RenderViewCreated(content::RenderViewHost* host) { | |
| 65 host->GetMainFrame()->GetServiceRegistry()->AddService( | |
|
Sam McNally
2014/06/26 11:04:40
You'll want to use AddService<KeyboardUIHandlerMoj
sadrul
2014/08/01 22:41:47
Done.
| |
| 66 "webui_controller", | |
| 67 base::Bind(&VKWebUIController::CreateAndStoreUIHandler, | |
| 68 weak_factory_.GetWeakPtr())); | |
| 69 } | |
| 70 | |
| 71 void VKWebUIController::CreateAndStoreUIHandler( | |
| 72 mojo::ScopedMessagePipeHandle handle_to_api) { | |
|
Sam McNally
2014/06/26 11:04:40
With the above change, this can turn into mojo::In
sadrul
2014/08/01 22:41:47
Done.
| |
| 73 ui_handler_ = scoped_ptr<VKMojoHandler>( | |
| 74 mojo::BindToPipe(new VKMojoHandler(), handle_to_api.Pass())); | |
| 75 } | |
| 76 | |
| 57 //////////////////////////////////////////////////////////////////////////////// | 77 //////////////////////////////////////////////////////////////////////////////// |
| 58 // VKWebUIControllerFactory: | 78 // VKWebUIControllerFactory: |
| 59 | 79 |
| 60 content::WebUI::TypeID VKWebUIControllerFactory::GetWebUIType( | 80 content::WebUI::TypeID VKWebUIControllerFactory::GetWebUIType( |
| 61 content::BrowserContext* browser_context, | 81 content::BrowserContext* browser_context, |
| 62 const GURL& url) const { | 82 const GURL& url) const { |
| 63 if (url == GURL(keyboard::kKeyboardURL)) | 83 if (url == GURL(kKeyboardURL)) |
| 64 return const_cast<VKWebUIControllerFactory*>(this); | 84 return const_cast<VKWebUIControllerFactory*>(this); |
| 65 | 85 |
| 66 return content::WebUI::kNoWebUI; | 86 return content::WebUI::kNoWebUI; |
| 67 } | 87 } |
| 68 | 88 |
| 69 bool VKWebUIControllerFactory::UseWebUIForURL( | 89 bool VKWebUIControllerFactory::UseWebUIForURL( |
| 70 content::BrowserContext* browser_context, | 90 content::BrowserContext* browser_context, |
| 71 const GURL& url) const { | 91 const GURL& url) const { |
| 72 return GetWebUIType(browser_context, url) != content::WebUI::kNoWebUI; | 92 return GetWebUIType(browser_context, url) != content::WebUI::kNoWebUI; |
| 73 } | 93 } |
| 74 | 94 |
| 75 bool VKWebUIControllerFactory::UseWebUIBindingsForURL( | 95 bool VKWebUIControllerFactory::UseWebUIBindingsForURL( |
| 76 content::BrowserContext* browser_context, | 96 content::BrowserContext* browser_context, |
| 77 const GURL& url) const { | 97 const GURL& url) const { |
| 78 return UseWebUIForURL(browser_context, url); | 98 return UseWebUIForURL(browser_context, url); |
| 79 } | 99 } |
| 80 | 100 |
| 81 content::WebUIController* VKWebUIControllerFactory::CreateWebUIControllerForURL( | 101 content::WebUIController* VKWebUIControllerFactory::CreateWebUIControllerForURL( |
| 82 content::WebUI* web_ui, | 102 content::WebUI* web_ui, |
| 83 const GURL& url) const { | 103 const GURL& url) const { |
| 84 if (url == GURL(keyboard::kKeyboardURL)) | 104 if (url == GURL(kKeyboardURL)) |
| 85 return new VKWebUIController(web_ui); | 105 return new VKWebUIController(web_ui); |
| 86 return NULL; | 106 return NULL; |
| 87 } | 107 } |
| 88 | 108 |
| 89 // static | 109 // static |
| 90 VKWebUIControllerFactory* VKWebUIControllerFactory::GetInstance() { | 110 VKWebUIControllerFactory* VKWebUIControllerFactory::GetInstance() { |
| 91 return Singleton<VKWebUIControllerFactory>::get(); | 111 return Singleton<VKWebUIControllerFactory>::get(); |
| 92 } | 112 } |
| 93 | 113 |
| 94 // protected | 114 // protected |
| 95 VKWebUIControllerFactory::VKWebUIControllerFactory() { | 115 VKWebUIControllerFactory::VKWebUIControllerFactory() { |
| 96 } | 116 } |
| 97 | 117 |
| 98 VKWebUIControllerFactory::~VKWebUIControllerFactory() { | 118 VKWebUIControllerFactory::~VKWebUIControllerFactory() { |
| 99 } | 119 } |
| 100 | 120 |
| 101 } // namespace athena | 121 } // namespace keyboard |
| OLD | NEW |