| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stdio.h> |
| 6 |
| 5 #include "mojo/service_manager/service_manager.h" | 7 #include "mojo/service_manager/service_manager.h" |
| 6 | 8 |
| 7 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "base/macros.h" | 11 #include "base/macros.h" |
| 10 #include "mojo/public/bindings/allocation_scope.h" | 12 #include "mojo/public/bindings/allocation_scope.h" |
| 11 #include "mojo/public/bindings/error_handler.h" | 13 #include "mojo/public/bindings/error_handler.h" |
| 12 #include "mojo/public/bindings/remote_ptr.h" | 14 #include "mojo/public/bindings/remote_ptr.h" |
| 13 #include "mojo/service_manager/service_loader.h" | 15 #include "mojo/service_manager/service_loader.h" |
| 14 | 16 |
| 15 namespace mojo { | 17 namespace mojo { |
| 16 | 18 |
| 19 namespace { |
| 20 // Used by TestAPI. |
| 21 bool has_created_instance = false; |
| 22 } |
| 23 |
| 17 class ServiceManager::ServiceFactory : public Shell, public ErrorHandler { | 24 class ServiceManager::ServiceFactory : public Shell, public ErrorHandler { |
| 18 public: | 25 public: |
| 19 ServiceFactory(ServiceManager* manager, const GURL& url) | 26 ServiceFactory(ServiceManager* manager, const GURL& url) |
| 20 : manager_(manager), | 27 : manager_(manager), |
| 21 url_(url) { | 28 url_(url) { |
| 22 InterfacePipe<Shell> pipe; | 29 InterfacePipe<Shell> pipe; |
| 23 shell_client_.reset(pipe.handle_to_peer.Pass(), this, this); | 30 shell_client_.reset(pipe.handle_to_peer.Pass(), this, this); |
| 24 manager_->GetLoaderForURL(url)->LoadService(manager_, | 31 manager_->GetLoaderForURL(url)->LoadService(manager_, |
| 25 url, | 32 url, |
| 26 pipe.handle_to_self.Pass()); | 33 pipe.handle_to_self.Pass()); |
| 27 } | 34 } |
| 28 virtual ~ServiceFactory() {} | 35 virtual ~ServiceFactory() {} |
| 29 | 36 |
| 30 void ConnectToClient(ScopedMessagePipeHandle handle) { | 37 void ConnectToClient(ScopedMessagePipeHandle handle) { |
| 31 if (handle.is_valid()) { | 38 if (handle.is_valid()) { |
| 32 AllocationScope scope; | 39 AllocationScope scope; |
| 33 shell_client_->AcceptConnection(url_.spec(), handle.Pass()); | 40 shell_client_->AcceptConnection(url_.spec(), handle.Pass()); |
| 34 } | 41 } |
| 35 } | 42 } |
| 36 | 43 |
| 37 virtual void Connect(const String& url, | 44 virtual void Connect(const String& url, |
| 38 ScopedMessagePipeHandle client_pipe) OVERRIDE { | 45 ScopedMessagePipeHandle client_pipe) OVERRIDE { |
| 39 manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass()); | 46 manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass()); |
| 40 } | 47 } |
| 41 | 48 |
| 42 virtual void OnError() OVERRIDE { | 49 virtual void OnError() OVERRIDE { |
| 43 manager_->RemoveServiceFactory(this); | 50 manager_->OnServiceFactoryError(this); |
| 44 } | 51 } |
| 45 | 52 |
| 46 const GURL& url() const { return url_; } | 53 const GURL& url() const { return url_; } |
| 47 | 54 |
| 48 private: | 55 private: |
| 49 ServiceManager* const manager_; | 56 ServiceManager* const manager_; |
| 50 const GURL url_; | 57 const GURL url_; |
| 51 RemotePtr<ShellClient> shell_client_; | 58 RemotePtr<ShellClient> shell_client_; |
| 52 DISALLOW_COPY_AND_ASSIGN(ServiceFactory); | 59 DISALLOW_COPY_AND_ASSIGN(ServiceFactory); |
| 53 }; | 60 }; |
| 54 | 61 |
| 62 // static |
| 63 bool ServiceManager::TestAPI::HasCreatedInstance() { |
| 64 return has_created_instance; |
| 65 } |
| 66 |
| 55 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { | 67 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { |
| 56 return manager_->url_to_service_factory_.find(url) != | 68 return manager_->url_to_service_factory_.find(url) != |
| 57 manager_->url_to_service_factory_.end(); | 69 manager_->url_to_service_factory_.end(); |
| 58 } | 70 } |
| 59 | 71 |
| 60 ServiceManager::ServiceManager() : default_loader_(NULL) { | 72 ServiceManager::ServiceManager() : default_loader_(NULL) { |
| 61 } | 73 } |
| 62 | 74 |
| 63 ServiceManager::~ServiceManager() { | 75 ServiceManager::~ServiceManager() { |
| 64 for (ServiceFactoryMap::iterator it = url_to_service_factory_.begin(); | 76 for (ServiceFactoryMap::iterator it = url_to_service_factory_.begin(); |
| 65 it != url_to_service_factory_.end(); ++it) { | 77 it != url_to_service_factory_.end(); ++it) { |
| 66 delete it->second; | 78 delete it->second; |
| 67 } | 79 } |
| 68 url_to_service_factory_.clear(); | 80 url_to_service_factory_.clear(); |
| 69 } | 81 } |
| 70 | 82 |
| 71 // static | 83 // static |
| 72 ServiceManager* GetInstance() { | 84 ServiceManager* ServiceManager::GetInstance() { |
| 73 static base::LazyInstance<ServiceManager> instance = | 85 static base::LazyInstance<ServiceManager> instance = |
| 74 LAZY_INSTANCE_INITIALIZER; | 86 LAZY_INSTANCE_INITIALIZER; |
| 87 has_created_instance = true; |
| 75 return &instance.Get(); | 88 return &instance.Get(); |
| 76 } | 89 } |
| 77 | 90 |
| 78 void ServiceManager::SetLoaderForURL(ServiceLoader* loader, const GURL& gurl) { | 91 void ServiceManager::SetLoaderForURL(ServiceLoader* loader, const GURL& gurl) { |
| 79 DCHECK(url_to_loader_.find(gurl) == url_to_loader_.end()); | 92 DCHECK(url_to_loader_.find(gurl) == url_to_loader_.end()); |
| 80 url_to_loader_[gurl] = loader; | 93 url_to_loader_[gurl] = loader; |
| 81 } | 94 } |
| 82 | 95 |
| 83 ServiceLoader* ServiceManager::GetLoaderForURL(const GURL& gurl) { | 96 ServiceLoader* ServiceManager::GetLoaderForURL(const GURL& gurl) { |
| 84 LoaderMap::const_iterator it = url_to_loader_.find(gurl); | 97 LoaderMap::const_iterator it = url_to_loader_.find(gurl); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 95 ServiceFactory* service_factory; | 108 ServiceFactory* service_factory; |
| 96 if (service_it != url_to_service_factory_.end()) { | 109 if (service_it != url_to_service_factory_.end()) { |
| 97 service_factory = service_it->second; | 110 service_factory = service_it->second; |
| 98 } else { | 111 } else { |
| 99 service_factory = new ServiceFactory(this, url); | 112 service_factory = new ServiceFactory(this, url); |
| 100 url_to_service_factory_[url] = service_factory; | 113 url_to_service_factory_[url] = service_factory; |
| 101 } | 114 } |
| 102 service_factory->ConnectToClient(client_handle.Pass()); | 115 service_factory->ConnectToClient(client_handle.Pass()); |
| 103 } | 116 } |
| 104 | 117 |
| 105 void ServiceManager::RemoveServiceFactory(ServiceFactory* service_factory) { | 118 void ServiceManager::OnServiceFactoryError(ServiceFactory* service_factory) { |
| 106 ServiceFactoryMap::iterator it = | 119 const GURL url = service_factory->url(); |
| 107 url_to_service_factory_.find(service_factory->url()); | 120 ServiceFactoryMap::iterator it = url_to_service_factory_.find(url); |
| 108 DCHECK(it != url_to_service_factory_.end()); | 121 DCHECK(it != url_to_service_factory_.end()); |
| 109 delete it->second; | 122 delete it->second; |
| 110 url_to_service_factory_.erase(it); | 123 url_to_service_factory_.erase(it); |
| 124 GetLoaderForURL(url)->OnServiceError(this, url); |
| 111 } | 125 } |
| 112 | 126 |
| 113 } // namespace mojo | 127 } // namespace mojo |
| OLD | NEW |