| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "components/mus/ws/window_manager_factory_registry.h" | 5 #include "components/mus/ws/window_manager_factory_registry.h" |
| 6 | 6 |
| 7 #include "components/mus/ws/connection_manager.h" | 7 #include "components/mus/ws/connection_manager.h" |
| 8 #include "components/mus/ws/user_id_tracker_observer.h" |
| 9 #include "components/mus/ws/window_manager_factory_registry_observer.h" |
| 8 #include "components/mus/ws/window_manager_factory_service.h" | 10 #include "components/mus/ws/window_manager_factory_service.h" |
| 9 | 11 |
| 10 namespace mus { | 12 namespace mus { |
| 11 namespace ws { | 13 namespace ws { |
| 12 | 14 |
| 13 WindowManagerFactoryRegistry::WindowManagerFactoryRegistry( | 15 WindowManagerFactoryRegistry::WindowManagerFactoryRegistry( |
| 14 ConnectionManager* connection_manager) | 16 ConnectionManager* connection_manager, |
| 15 : connection_manager_(connection_manager) {} | 17 UserIdTracker* id_tracker) |
| 18 : id_tracker_(id_tracker), connection_manager_(connection_manager) { |
| 19 id_tracker_->AddObserver(this); |
| 20 } |
| 16 | 21 |
| 17 WindowManagerFactoryRegistry::~WindowManagerFactoryRegistry() {} | 22 WindowManagerFactoryRegistry::~WindowManagerFactoryRegistry() { |
| 23 id_tracker_->RemoveObserver(this); |
| 24 } |
| 18 | 25 |
| 19 void WindowManagerFactoryRegistry::Register( | 26 void WindowManagerFactoryRegistry::Register( |
| 20 uint32_t user_id, | 27 UserId user_id, |
| 21 mojo::InterfaceRequest<mojom::WindowManagerFactoryService> request) { | 28 mojo::InterfaceRequest<mojom::WindowManagerFactoryService> request) { |
| 22 for (auto& service_ptr : services_) { | 29 if (ContainsServiceForUser(user_id)) |
| 23 if (service_ptr->user_id() == user_id) { | 30 return; |
| 24 LOG(ERROR) << "WindowManagerFactoryService already registered for " | |
| 25 << user_id; | |
| 26 return; | |
| 27 } | |
| 28 } | |
| 29 | 31 |
| 30 scoped_ptr<WindowManagerFactoryService> service( | 32 scoped_ptr<WindowManagerFactoryService> service( |
| 31 new WindowManagerFactoryService(this, user_id, std::move(request))); | 33 new WindowManagerFactoryService(this, user_id, std::move(request))); |
| 32 services_.push_back(std::move(service)); | 34 AddServiceImpl(std::move(service)); |
| 33 } | 35 } |
| 34 | 36 |
| 35 std::vector<WindowManagerFactoryService*> | 37 std::vector<WindowManagerFactoryService*> |
| 36 WindowManagerFactoryRegistry::GetServices() { | 38 WindowManagerFactoryRegistry::GetServices() { |
| 37 std::vector<WindowManagerFactoryService*> result; | 39 std::vector<WindowManagerFactoryService*> result; |
| 38 for (auto& service_ptr : services_) | 40 for (auto& service_ptr : services_) |
| 39 result.push_back(service_ptr.get()); | 41 result.push_back(service_ptr.get()); |
| 40 return result; | 42 return result; |
| 41 } | 43 } |
| 42 | 44 |
| 45 void WindowManagerFactoryRegistry::AddObserver( |
| 46 WindowManagerFactoryRegistryObserver* observer) { |
| 47 observers_.AddObserver(observer); |
| 48 } |
| 49 |
| 50 void WindowManagerFactoryRegistry::RemoveObserver( |
| 51 WindowManagerFactoryRegistryObserver* observer) { |
| 52 observers_.RemoveObserver(observer); |
| 53 } |
| 54 |
| 55 void WindowManagerFactoryRegistry::AddServiceImpl( |
| 56 scoped_ptr<WindowManagerFactoryService> service) { |
| 57 services_.push_back(std::move(service)); |
| 58 } |
| 59 |
| 60 bool WindowManagerFactoryRegistry::ContainsServiceForUser( |
| 61 UserId user_id) const { |
| 62 for (auto& service_ptr : services_) { |
| 63 if (service_ptr->user_id() == user_id) { |
| 64 LOG(ERROR) << "WindowManagerFactoryService already registered for " |
| 65 << user_id; |
| 66 return true; |
| 67 } |
| 68 } |
| 69 return false; |
| 70 } |
| 71 |
| 43 void WindowManagerFactoryRegistry::OnWindowManagerFactoryConnectionLost( | 72 void WindowManagerFactoryRegistry::OnWindowManagerFactoryConnectionLost( |
| 44 WindowManagerFactoryService* service) { | 73 WindowManagerFactoryService* service) { |
| 45 for (auto it = services_.begin(); it != services_.end(); ++it) { | 74 for (auto it = services_.begin(); it != services_.end(); ++it) { |
| 46 if (it->get() == service) { | 75 if (it->get() == service) { |
| 47 services_.erase(it); | 76 services_.erase(it); |
| 48 return; | 77 return; |
| 49 } | 78 } |
| 50 } | 79 } |
| 51 } | 80 } |
| 52 | 81 |
| 53 void WindowManagerFactoryRegistry::OnWindowManagerFactorySet() { | 82 void WindowManagerFactoryRegistry::OnWindowManagerFactorySet( |
| 54 connection_manager_->OnWindowManagerFactorySet(); | 83 WindowManagerFactoryService* service) { |
| 84 DCHECK(service->window_manager_factory()); |
| 85 const bool is_first_valid_factory = !got_valid_factory_; |
| 86 got_valid_factory_ = true; |
| 87 FOR_EACH_OBSERVER(WindowManagerFactoryRegistryObserver, observers_, |
| 88 OnWindowManagerFactorySet(service)); |
| 89 |
| 90 // Notify after other observers as ConnectionManager triggers other |
| 91 // observers being added, which will have already processed the add. |
| 92 if (is_first_valid_factory) |
| 93 connection_manager_->OnFirstWindowManagerFactorySet(); |
| 94 } |
| 95 |
| 96 void WindowManagerFactoryRegistry::OnActiveUserIdChanged(UserId id) {} |
| 97 |
| 98 void WindowManagerFactoryRegistry::OnUserIdAdded(UserId id) {} |
| 99 |
| 100 void WindowManagerFactoryRegistry::OnUserIdRemoved(UserId id) { |
| 101 for (auto iter = services_.begin(); iter != services_.end(); ++iter) { |
| 102 if ((*iter)->user_id() == id) { |
| 103 services_.erase(iter); |
| 104 return; |
| 105 } |
| 106 } |
| 55 } | 107 } |
| 56 | 108 |
| 57 } // namespace ws | 109 } // namespace ws |
| 58 } // namespace mus | 110 } // namespace mus |
| OLD | NEW |