| 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> | 5 #include <stdio.h> |
| 6 | 6 |
| 7 #include "mojo/service_manager/service_manager.h" | 7 #include "mojo/service_manager/service_manager.h" |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "mojo/public/cpp/bindings/allocation_scope.h" | 13 #include "mojo/public/cpp/bindings/allocation_scope.h" |
| 14 #include "mojo/service_manager/service_loader.h" | 14 #include "mojo/service_manager/service_loader.h" |
| 15 | 15 |
| 16 namespace mojo { | 16 namespace mojo { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 // Used by TestAPI. | 19 // Used by TestAPI. |
| 20 bool has_created_instance = false; | 20 bool has_created_instance = false; |
| 21 } | 21 } |
| 22 | 22 |
| 23 class ServiceManager::ServiceFactory : public InterfaceImpl<ServiceProvider> { | 23 class ServiceManager::ServiceFactory : public InterfaceImpl<Shell> { |
| 24 public: | 24 public: |
| 25 ServiceFactory(ServiceManager* manager, const GURL& url) | 25 ServiceFactory(ServiceManager* manager, const GURL& url) |
| 26 : manager_(manager), | 26 : manager_(manager), |
| 27 url_(url) { | 27 url_(url) { |
| 28 } | 28 } |
| 29 | 29 |
| 30 virtual ~ServiceFactory() { | 30 virtual ~ServiceFactory() { |
| 31 } | 31 } |
| 32 | 32 |
| 33 void ConnectToClient(ScopedMessagePipeHandle handle) { | 33 void ConnectToClient(ScopedMessagePipeHandle handle) { |
| 34 if (handle.is_valid()) { | 34 if (handle.is_valid()) { |
| 35 AllocationScope scope; | 35 AllocationScope scope; |
| 36 client()->ConnectToService(url_.spec(), handle.Pass()); | 36 client()->AcceptConnection(url_.spec(), handle.Pass()); |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 // ServiceProvider implementation: | 40 // Shell implementation: |
| 41 virtual void ConnectToService(const String& url, | 41 virtual void Connect(const String& url, |
| 42 ScopedMessagePipeHandle client_pipe) OVERRIDE { | 42 ScopedMessagePipeHandle client_pipe) OVERRIDE { |
| 43 manager_->ConnectToService(GURL(url.To<std::string>()), client_pipe.Pass()); | 43 manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass()); |
| 44 } | 44 } |
| 45 | 45 |
| 46 const GURL& url() const { return url_; } | 46 const GURL& url() const { return url_; } |
| 47 | 47 |
| 48 private: | 48 private: |
| 49 virtual void OnConnectionError() OVERRIDE { | 49 virtual void OnConnectionError() OVERRIDE { |
| 50 manager_->OnServiceFactoryError(this); | 50 manager_->OnServiceFactoryError(this); |
| 51 } | 51 } |
| 52 | 52 |
| 53 ServiceManager* const manager_; | 53 ServiceManager* const manager_; |
| 54 const GURL url_; | 54 const GURL url_; |
| 55 | 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(ServiceFactory); | 56 DISALLOW_COPY_AND_ASSIGN(ServiceFactory); |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 class ServiceManager::TestAPI::TestServiceProviderConnection | 59 class ServiceManager::TestAPI::TestShellConnection |
| 60 : public InterfaceImpl<ServiceProvider> { | 60 : public InterfaceImpl<Shell> { |
| 61 public: | 61 public: |
| 62 explicit TestServiceProviderConnection(ServiceManager* manager) | 62 explicit TestShellConnection(ServiceManager* manager) : manager_(manager) {} |
| 63 : manager_(manager) {} | 63 virtual ~TestShellConnection() {} |
| 64 virtual ~TestServiceProviderConnection() {} | |
| 65 | 64 |
| 66 virtual void OnConnectionError() OVERRIDE { | 65 virtual void OnConnectionError() OVERRIDE { |
| 67 // TODO(darin): How should we handle this error? | 66 // TODO(darin): How should we handle this error? |
| 68 } | 67 } |
| 69 | 68 |
| 70 // ServiceProvider: | 69 // Shell: |
| 71 virtual void ConnectToService(const String& url, | 70 virtual void Connect(const String& url, |
| 72 ScopedMessagePipeHandle client_pipe) OVERRIDE { | 71 ScopedMessagePipeHandle client_pipe) OVERRIDE { |
| 73 manager_->ConnectToService(GURL(url.To<std::string>()), client_pipe.Pass()); | 72 manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass()); |
| 74 } | 73 } |
| 75 | 74 |
| 76 private: | 75 private: |
| 77 ServiceManager* manager_; | 76 ServiceManager* manager_; |
| 78 | 77 |
| 79 DISALLOW_COPY_AND_ASSIGN(TestServiceProviderConnection); | 78 DISALLOW_COPY_AND_ASSIGN(TestShellConnection); |
| 80 }; | 79 }; |
| 81 | 80 |
| 82 // static | 81 // static |
| 83 ServiceManager::TestAPI::TestAPI(ServiceManager* manager) : manager_(manager) { | 82 ServiceManager::TestAPI::TestAPI(ServiceManager* manager) : manager_(manager) { |
| 84 } | 83 } |
| 85 | 84 |
| 86 ServiceManager::TestAPI::~TestAPI() { | 85 ServiceManager::TestAPI::~TestAPI() { |
| 87 } | 86 } |
| 88 | 87 |
| 89 bool ServiceManager::TestAPI::HasCreatedInstance() { | 88 bool ServiceManager::TestAPI::HasCreatedInstance() { |
| 90 return has_created_instance; | 89 return has_created_instance; |
| 91 } | 90 } |
| 92 | 91 |
| 93 ScopedMessagePipeHandle ServiceManager::TestAPI::GetServiceProviderHandle() { | 92 ScopedMessagePipeHandle ServiceManager::TestAPI::GetShellHandle() { |
| 94 MessagePipe pipe; | 93 MessagePipe pipe; |
| 95 service_provider_.reset( | 94 shell_.reset( |
| 96 BindToPipe(new TestServiceProviderConnection(manager_), | 95 BindToPipe(new TestShellConnection(manager_), pipe.handle0.Pass())); |
| 97 pipe.handle0.Pass())); | |
| 98 return pipe.handle1.Pass(); | 96 return pipe.handle1.Pass(); |
| 99 } | 97 } |
| 100 | 98 |
| 101 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { | 99 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { |
| 102 return manager_->url_to_service_factory_.find(url) != | 100 return manager_->url_to_service_factory_.find(url) != |
| 103 manager_->url_to_service_factory_.end(); | 101 manager_->url_to_service_factory_.end(); |
| 104 } | 102 } |
| 105 | 103 |
| 106 ServiceManager::ServiceManager() | 104 ServiceManager::ServiceManager() |
| 107 : interceptor_(NULL) { | 105 : interceptor_(NULL) { |
| 108 } | 106 } |
| 109 | 107 |
| 110 ServiceManager::~ServiceManager() { | 108 ServiceManager::~ServiceManager() { |
| 111 STLDeleteValues(&url_to_service_factory_); | 109 STLDeleteValues(&url_to_service_factory_); |
| 112 STLDeleteValues(&url_to_loader_); | 110 STLDeleteValues(&url_to_loader_); |
| 113 STLDeleteValues(&scheme_to_loader_); | 111 STLDeleteValues(&scheme_to_loader_); |
| 114 } | 112 } |
| 115 | 113 |
| 116 // static | 114 // static |
| 117 ServiceManager* ServiceManager::GetInstance() { | 115 ServiceManager* ServiceManager::GetInstance() { |
| 118 static base::LazyInstance<ServiceManager> instance = | 116 static base::LazyInstance<ServiceManager> instance = |
| 119 LAZY_INSTANCE_INITIALIZER; | 117 LAZY_INSTANCE_INITIALIZER; |
| 120 has_created_instance = true; | 118 has_created_instance = true; |
| 121 return &instance.Get(); | 119 return &instance.Get(); |
| 122 } | 120 } |
| 123 | 121 |
| 124 void ServiceManager::ConnectToService(const GURL& url, | 122 void ServiceManager::Connect(const GURL& url, |
| 125 ScopedMessagePipeHandle client_handle) { | 123 ScopedMessagePipeHandle client_handle) { |
| 126 URLToServiceFactoryMap::const_iterator service_it = | 124 URLToServiceFactoryMap::const_iterator service_it = |
| 127 url_to_service_factory_.find(url); | 125 url_to_service_factory_.find(url); |
| 128 ServiceFactory* service_factory; | 126 ServiceFactory* service_factory; |
| 129 if (service_it != url_to_service_factory_.end()) { | 127 if (service_it != url_to_service_factory_.end()) { |
| 130 service_factory = service_it->second; | 128 service_factory = service_it->second; |
| 131 } else { | 129 } else { |
| 132 MessagePipe pipe; | 130 MessagePipe pipe; |
| 133 GetLoaderForURL(url)->LoadService(this, url, pipe.handle0.Pass()); | 131 GetLoaderForURL(url)->LoadService(this, url, pipe.handle0.Pass()); |
| 134 | 132 |
| 135 service_factory = | 133 service_factory = |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 // Called from ~ServiceFactory, so we do not need to call Destroy here. | 179 // Called from ~ServiceFactory, so we do not need to call Destroy here. |
| 182 const GURL url = service_factory->url(); | 180 const GURL url = service_factory->url(); |
| 183 URLToServiceFactoryMap::iterator it = url_to_service_factory_.find(url); | 181 URLToServiceFactoryMap::iterator it = url_to_service_factory_.find(url); |
| 184 DCHECK(it != url_to_service_factory_.end()); | 182 DCHECK(it != url_to_service_factory_.end()); |
| 185 delete it->second; | 183 delete it->second; |
| 186 url_to_service_factory_.erase(it); | 184 url_to_service_factory_.erase(it); |
| 187 GetLoaderForURL(url)->OnServiceError(this, url); | 185 GetLoaderForURL(url)->OnServiceError(this, url); |
| 188 } | 186 } |
| 189 | 187 |
| 190 } // namespace mojo | 188 } // namespace mojo |
| OLD | NEW |