Chromium Code Reviews| Index: chrome/browser/chromeos/chrome_interface_factory.cc |
| diff --git a/chrome/browser/chromeos/chrome_interface_factory.cc b/chrome/browser/chromeos/chrome_interface_factory.cc |
| index a4b334d8dcbf16edd9190bc3a48a0d2592912427..3dbfaa25f07297bcf4496db7eaaf9a68a2b7b483 100644 |
| --- a/chrome/browser/chromeos/chrome_interface_factory.cc |
| +++ b/chrome/browser/chromeos/chrome_interface_factory.cc |
| @@ -4,6 +4,13 @@ |
| #include "chrome/browser/chromeos/chrome_interface_factory.h" |
| +#include <memory> |
| + |
| +#include "ash/sysui/public/interfaces/wallpaper.mojom.h" |
| +#include "base/lazy_instance.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/profiles/profile_manager.h" |
| #include "chrome/browser/ui/ash/app_list/app_list_presenter_service.h" |
| @@ -11,7 +18,16 @@ |
| #include "chrome/browser/ui/ash/keyboard_ui_service.h" |
| #include "chrome/browser/ui/browser_commands.h" |
| #include "chrome/browser/ui/browser_finder.h" |
| +#include "content/public/common/mojo_shell_connection.h" |
| +#include "mash/public/interfaces/launchable.mojom.h" |
| +#include "mojo/public/cpp/bindings/binding_set.h" |
| #include "services/shell/public/cpp/connection.h" |
| +#include "ui/app_list/presenter/app_list_presenter.mojom.h" |
| +#include "ui/keyboard/keyboard.mojom.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| class ChromeLaunchable : public mash::mojom::Launchable { |
| public: |
| @@ -57,49 +73,94 @@ class ChromeLaunchable : public mash::mojom::Launchable { |
| DISALLOW_COPY_AND_ASSIGN(ChromeLaunchable); |
| }; |
| -namespace chromeos { |
| +class FactoryImpl { |
| + public: |
| + FactoryImpl() {} |
| + ~FactoryImpl() {} |
| + |
| + template <typename Interface> |
| + static void AddFactory( |
| + shell::Connection* connection, |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| + connection->AddInterface<Interface>( |
| + base::Bind(&FactoryImpl::CallMainThreadFactory<Interface>), |
| + task_runner); |
| + } |
| -ChromeInterfaceFactory::ChromeInterfaceFactory() {} |
| -ChromeInterfaceFactory::~ChromeInterfaceFactory() {} |
| + private: |
| + static FactoryImpl* Get() { |
| + if (!factory_.Get()) |
| + factory_.Get().reset(new FactoryImpl); |
| + return factory_.Get().get(); |
| + } |
| -bool ChromeInterfaceFactory::OnConnect(shell::Connection* connection) { |
| - connection->AddInterface<keyboard::mojom::Keyboard>(this); |
| - connection->AddInterface<mash::mojom::Launchable>(this); |
| - connection->AddInterface<ash::sysui::mojom::WallpaperManager>(this); |
| - connection->AddInterface<app_list::mojom::AppListPresenter>(this); |
| - return true; |
| -} |
| + template <typename Interface> |
| + static void CallMainThreadFactory(mojo::InterfaceRequest<Interface> request) { |
| + Get()->BindRequest(std::move(request)); |
| + } |
| -void ChromeInterfaceFactory::Create( |
| - shell::Connection* connection, |
| - mojo::InterfaceRequest<keyboard::mojom::Keyboard> request) { |
| - if (!keyboard_ui_service_) |
| - keyboard_ui_service_.reset(new KeyboardUIService); |
| - keyboard_bindings_.AddBinding(keyboard_ui_service_.get(), std::move(request)); |
| -} |
| + void BindRequest(keyboard::mojom::KeyboardRequest request) { |
| + if (!keyboard_ui_service_) |
| + keyboard_ui_service_.reset(new KeyboardUIService); |
| + keyboard_bindings_.AddBinding(keyboard_ui_service_.get(), |
| + std::move(request)); |
| + } |
| -void ChromeInterfaceFactory::Create(shell::Connection* connection, |
| - mash::mojom::LaunchableRequest request) { |
| - if (!launchable_) |
| - launchable_.reset(new ChromeLaunchable); |
| - launchable_->ProcessRequest(std::move(request)); |
| -} |
| + void BindRequest(mash::mojom::LaunchableRequest request) { |
| + if (!launchable_) |
| + launchable_.reset(new ChromeLaunchable); |
| + launchable_->ProcessRequest(std::move(request)); |
| + } |
| -void ChromeInterfaceFactory::Create( |
| - shell::Connection* connection, |
| - ash::sysui::mojom::WallpaperManagerRequest request) { |
| - if (!wallpaper_manager_) |
| - wallpaper_manager_.reset(new ChromeWallpaperManager); |
| - wallpaper_manager_->ProcessRequest(std::move(request)); |
| -} |
| + void BindRequest(ash::sysui::mojom::WallpaperManagerRequest request) { |
| + if (!wallpaper_manager_) |
| + wallpaper_manager_.reset(new ChromeWallpaperManager); |
| + wallpaper_manager_->ProcessRequest(std::move(request)); |
| + } |
| + |
| + void BindRequest(app_list::mojom::AppListPresenterRequest request) { |
| + if (!app_list_presenter_service_) |
| + app_list_presenter_service_.reset(new AppListPresenterService); |
| + app_list_presenter_bindings_.AddBinding(app_list_presenter_service_.get(), |
| + std::move(request)); |
| + } |
| + |
| + static base::LazyInstance<std::unique_ptr<FactoryImpl>>::Leaky factory_; |
| + |
| + base::ThreadChecker thread_checker_; |
| + |
| + std::unique_ptr<KeyboardUIService> keyboard_ui_service_; |
| + mojo::BindingSet<keyboard::mojom::Keyboard> keyboard_bindings_; |
| + std::unique_ptr<ChromeLaunchable> launchable_; |
| + std::unique_ptr<ChromeWallpaperManager> wallpaper_manager_; |
| + std::unique_ptr<AppListPresenterService> app_list_presenter_service_; |
| + mojo::BindingSet<app_list::mojom::AppListPresenter> |
| + app_list_presenter_bindings_; |
| -void ChromeInterfaceFactory::Create( |
| - shell::Connection* connection, |
| - mojo::InterfaceRequest<app_list::mojom::AppListPresenter> request) { |
| - if (!app_list_presenter_service_) |
| - app_list_presenter_service_.reset(new AppListPresenterService); |
| - app_list_presenter_bindings_.AddBinding(app_list_presenter_service_.get(), |
| - std::move(request)); |
| + DISALLOW_COPY_AND_ASSIGN(FactoryImpl); |
| +}; |
| + |
| +base::LazyInstance<std::unique_ptr<FactoryImpl>>::Leaky FactoryImpl::factory_ = |
| + LAZY_INSTANCE_INITIALIZER; |
| + |
| +} // namespace |
| + |
| +ChromeInterfaceFactory::ChromeInterfaceFactory() |
| + : main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} |
| + |
| +ChromeInterfaceFactory::~ChromeInterfaceFactory() {} |
| + |
| +bool ChromeInterfaceFactory::AcceptConnection(shell::Connection* connection, |
| + shell::Connector* connector) { |
| + FactoryImpl::AddFactory<keyboard::mojom::Keyboard>(connection, |
|
Ben Goodger (Google)
2016/07/05 21:39:45
my eye is telling me you should wrap these like th
Ken Rockot(use gerrit already)
2016/07/06 17:09:57
I agree, but this is the work of clang_format. I'v
|
| + main_thread_task_runner_); |
| + FactoryImpl::AddFactory<mash::mojom::Launchable>(connection, |
| + main_thread_task_runner_); |
| + FactoryImpl::AddFactory<ash::sysui::mojom::WallpaperManager>( |
| + connection, main_thread_task_runner_); |
| + FactoryImpl::AddFactory<app_list::mojom::AppListPresenter>( |
| + connection, main_thread_task_runner_); |
| + return true; |
| } |
| } // namespace chromeos |