OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "chrome/browser/chromeos/chrome_interface_factory.h" | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 #include "ash/common/mojo_interface_factory.h" | |
11 #include "ash/public/interfaces/shutdown.mojom.h" | |
12 #include "base/lazy_instance.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/threading/thread_checker.h" | |
16 #include "base/threading/thread_task_runner_handle.h" | |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/browser/profiles/profile_manager.h" | |
19 #include "chrome/browser/ui/ash/ash_util.h" | |
20 #include "chrome/browser/ui/ash/keyboard_ui_service.h" | |
21 #include "chrome/browser/ui/browser_commands.h" | |
22 #include "chrome/browser/ui/browser_finder.h" | |
23 #include "content/public/common/service_manager_connection.h" | |
24 #include "mash/public/interfaces/launchable.mojom.h" | |
25 #include "mojo/public/cpp/bindings/binding_set.h" | |
26 #include "services/service_manager/public/cpp/connection.h" | |
27 #include "services/service_manager/public/cpp/interface_registry.h" | |
28 #include "ui/keyboard/keyboard.mojom.h" | |
29 | |
30 namespace chromeos { | |
31 | |
32 namespace { | |
33 | |
34 class ChromeLaunchable : public mash::mojom::Launchable { | |
35 public: | |
36 ChromeLaunchable() {} | |
37 ~ChromeLaunchable() override {} | |
38 | |
39 void ProcessRequest(mash::mojom::LaunchableRequest request) { | |
40 bindings_.AddBinding(this, std::move(request)); | |
41 } | |
42 | |
43 private: | |
44 void CreateNewWindowImpl(bool is_incognito) { | |
45 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
46 chrome::NewEmptyWindow(is_incognito ? profile->GetOffTheRecordProfile() | |
47 : profile); | |
48 } | |
49 | |
50 void CreateNewTab() { chrome::NewTab(chrome::FindLastActive()); } | |
51 | |
52 // mash::mojom::Launchable: | |
53 void Launch(uint32_t what, mash::mojom::LaunchMode how) override { | |
54 if (how != mash::mojom::LaunchMode::MAKE_NEW) { | |
55 LOG(ERROR) << "Unable to handle Launch request with how = " << how; | |
56 return; | |
57 } | |
58 switch (what) { | |
59 case mash::mojom::kWindow: | |
60 CreateNewWindowImpl(false /* is_incognito */); | |
61 break; | |
62 case mash::mojom::kIncognitoWindow: | |
63 CreateNewWindowImpl(true /* is_incognito */); | |
64 break; | |
65 default: | |
66 NOTREACHED(); | |
67 } | |
68 } | |
69 | |
70 mojo::BindingSet<mash::mojom::Launchable> bindings_; | |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(ChromeLaunchable); | |
73 }; | |
74 | |
75 class FactoryImpl { | |
76 public: | |
77 FactoryImpl() {} | |
78 ~FactoryImpl() {} | |
79 | |
80 template <typename Interface> | |
81 static void AddFactory( | |
82 service_manager::InterfaceRegistry* registry, | |
83 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | |
84 registry->AddInterface<Interface>( | |
85 base::Bind(&FactoryImpl::CallMainThreadFactory<Interface>), | |
86 task_runner); | |
87 } | |
88 | |
89 private: | |
90 static FactoryImpl* Get() { | |
91 if (!factory_.Get()) | |
92 factory_.Get() = base::MakeUnique<FactoryImpl>(); | |
93 return factory_.Get().get(); | |
94 } | |
95 | |
96 template <typename Interface> | |
97 static void CallMainThreadFactory(mojo::InterfaceRequest<Interface> request) { | |
98 Get()->BindRequest(std::move(request)); | |
99 } | |
100 | |
101 void BindRequest(keyboard::mojom::KeyboardRequest request) { | |
102 if (!keyboard_ui_service_) | |
103 keyboard_ui_service_ = base::MakeUnique<KeyboardUIService>(); | |
104 keyboard_bindings_.AddBinding(keyboard_ui_service_.get(), | |
105 std::move(request)); | |
106 } | |
107 | |
108 void BindRequest(mash::mojom::LaunchableRequest request) { | |
109 if (!launchable_) | |
110 launchable_ = base::MakeUnique<ChromeLaunchable>(); | |
111 launchable_->ProcessRequest(std::move(request)); | |
112 } | |
113 | |
114 static base::LazyInstance<std::unique_ptr<FactoryImpl>>::Leaky factory_; | |
115 | |
116 std::unique_ptr<KeyboardUIService> keyboard_ui_service_; | |
117 mojo::BindingSet<keyboard::mojom::Keyboard> keyboard_bindings_; | |
118 std::unique_ptr<ChromeLaunchable> launchable_; | |
119 | |
120 DISALLOW_COPY_AND_ASSIGN(FactoryImpl); | |
121 }; | |
122 | |
123 base::LazyInstance<std::unique_ptr<FactoryImpl>>::Leaky FactoryImpl::factory_ = | |
124 LAZY_INSTANCE_INITIALIZER; | |
125 | |
126 } // namespace | |
127 | |
128 ChromeInterfaceFactory::ChromeInterfaceFactory() | |
129 : main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} | |
130 | |
131 ChromeInterfaceFactory::~ChromeInterfaceFactory() {} | |
132 | |
133 bool ChromeInterfaceFactory::OnConnect( | |
134 const service_manager::Identity& remote_identity, | |
135 service_manager::InterfaceRegistry* registry, | |
136 service_manager::Connector* connector) { | |
137 // TODO(jamescook): Only register the interfaces needed for a particular | |
138 // |remote_identity|. For example, a connection from service:ash needs these, | |
139 // but a connection from service:content_gpu does not. | |
140 FactoryImpl::AddFactory<keyboard::mojom::Keyboard>(registry, | |
141 main_thread_task_runner_); | |
142 FactoryImpl::AddFactory<mash::mojom::Launchable>(registry, | |
143 main_thread_task_runner_); | |
144 | |
145 // In classic ash, the browser process provides ash services to itself. | |
146 if (!chrome::IsRunningInMash()) { | |
147 ash::mojo_interface_factory::RegisterInterfaces(registry, | |
148 main_thread_task_runner_); | |
149 } | |
150 | |
151 return true; | |
152 } | |
153 | |
154 } // namespace chromeos | |
OLD | NEW |