| 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<Shell> { | 23 class ServiceManager::ServiceFactory : public InterfaceImpl<ServiceProvider> { |
| 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()->AcceptConnection(url_.spec(), handle.Pass()); | 36 client()->ConnectToService(url_.spec(), handle.Pass()); |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 // Shell implementation: | 40 // ServiceProvider implementation: |
| 41 virtual void Connect(const String& url, | 41 virtual void ConnectToService(const String& url, |
| 42 ScopedMessagePipeHandle client_pipe) OVERRIDE { | 42 ScopedMessagePipeHandle client_pipe) OVERRIDE { |
| 43 manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass()); | 43 manager_->ConnectToService(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::TestShellConnection | 59 class ServiceManager::TestAPI::TestServiceProviderConnection |
| 60 : public InterfaceImpl<Shell> { | 60 : public InterfaceImpl<ServiceProvider> { |
| 61 public: | 61 public: |
| 62 explicit TestShellConnection(ServiceManager* manager) : manager_(manager) {} | 62 explicit TestServiceProviderConnection(ServiceManager* manager) |
| 63 virtual ~TestShellConnection() {} | 63 : manager_(manager) {} |
| 64 virtual ~TestServiceProviderConnection() {} |
| 64 | 65 |
| 65 virtual void OnConnectionError() OVERRIDE { | 66 virtual void OnConnectionError() OVERRIDE { |
| 66 // TODO(darin): How should we handle this error? | 67 // TODO(darin): How should we handle this error? |
| 67 } | 68 } |
| 68 | 69 |
| 69 // Shell: | 70 // ServiceProvider: |
| 70 virtual void Connect(const String& url, | 71 virtual void ConnectToService(const String& url, |
| 71 ScopedMessagePipeHandle client_pipe) OVERRIDE { | 72 ScopedMessagePipeHandle client_pipe) OVERRIDE { |
| 72 manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass()); | 73 manager_->ConnectToService(GURL(url.To<std::string>()), client_pipe.Pass()); |
| 73 } | 74 } |
| 74 | 75 |
| 75 private: | 76 private: |
| 76 ServiceManager* manager_; | 77 ServiceManager* manager_; |
| 77 | 78 |
| 78 DISALLOW_COPY_AND_ASSIGN(TestShellConnection); | 79 DISALLOW_COPY_AND_ASSIGN(TestServiceProviderConnection); |
| 79 }; | 80 }; |
| 80 | 81 |
| 81 // static | 82 // static |
| 82 ServiceManager::TestAPI::TestAPI(ServiceManager* manager) : manager_(manager) { | 83 ServiceManager::TestAPI::TestAPI(ServiceManager* manager) : manager_(manager) { |
| 83 } | 84 } |
| 84 | 85 |
| 85 ServiceManager::TestAPI::~TestAPI() { | 86 ServiceManager::TestAPI::~TestAPI() { |
| 86 } | 87 } |
| 87 | 88 |
| 88 bool ServiceManager::TestAPI::HasCreatedInstance() { | 89 bool ServiceManager::TestAPI::HasCreatedInstance() { |
| 89 return has_created_instance; | 90 return has_created_instance; |
| 90 } | 91 } |
| 91 | 92 |
| 92 ScopedMessagePipeHandle ServiceManager::TestAPI::GetShellHandle() { | 93 ScopedMessagePipeHandle ServiceManager::TestAPI::GetServiceProviderHandle() { |
| 93 MessagePipe pipe; | 94 MessagePipe pipe; |
| 94 shell_.reset( | 95 service_provider_.reset( |
| 95 BindToPipe(new TestShellConnection(manager_), pipe.handle0.Pass())); | 96 BindToPipe(new TestServiceProviderConnection(manager_), |
| 97 pipe.handle0.Pass())); |
| 96 return pipe.handle1.Pass(); | 98 return pipe.handle1.Pass(); |
| 97 } | 99 } |
| 98 | 100 |
| 99 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { | 101 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { |
| 100 return manager_->url_to_service_factory_.find(url) != | 102 return manager_->url_to_service_factory_.find(url) != |
| 101 manager_->url_to_service_factory_.end(); | 103 manager_->url_to_service_factory_.end(); |
| 102 } | 104 } |
| 103 | 105 |
| 104 ServiceManager::ServiceManager() | 106 ServiceManager::ServiceManager() |
| 105 : interceptor_(NULL) { | 107 : interceptor_(NULL) { |
| 106 } | 108 } |
| 107 | 109 |
| 108 ServiceManager::~ServiceManager() { | 110 ServiceManager::~ServiceManager() { |
| 109 STLDeleteValues(&url_to_service_factory_); | 111 STLDeleteValues(&url_to_service_factory_); |
| 110 STLDeleteValues(&url_to_loader_); | 112 STLDeleteValues(&url_to_loader_); |
| 111 STLDeleteValues(&scheme_to_loader_); | 113 STLDeleteValues(&scheme_to_loader_); |
| 112 } | 114 } |
| 113 | 115 |
| 114 // static | 116 // static |
| 115 ServiceManager* ServiceManager::GetInstance() { | 117 ServiceManager* ServiceManager::GetInstance() { |
| 116 static base::LazyInstance<ServiceManager> instance = | 118 static base::LazyInstance<ServiceManager> instance = |
| 117 LAZY_INSTANCE_INITIALIZER; | 119 LAZY_INSTANCE_INITIALIZER; |
| 118 has_created_instance = true; | 120 has_created_instance = true; |
| 119 return &instance.Get(); | 121 return &instance.Get(); |
| 120 } | 122 } |
| 121 | 123 |
| 122 void ServiceManager::Connect(const GURL& url, | 124 void ServiceManager::ConnectToService(const GURL& url, |
| 123 ScopedMessagePipeHandle client_handle) { | 125 ScopedMessagePipeHandle client_handle) { |
| 124 URLToServiceFactoryMap::const_iterator service_it = | 126 URLToServiceFactoryMap::const_iterator service_it = |
| 125 url_to_service_factory_.find(url); | 127 url_to_service_factory_.find(url); |
| 126 ServiceFactory* service_factory; | 128 ServiceFactory* service_factory; |
| 127 if (service_it != url_to_service_factory_.end()) { | 129 if (service_it != url_to_service_factory_.end()) { |
| 128 service_factory = service_it->second; | 130 service_factory = service_it->second; |
| 129 } else { | 131 } else { |
| 130 MessagePipe pipe; | 132 MessagePipe pipe; |
| 131 GetLoaderForURL(url)->LoadService(this, url, pipe.handle0.Pass()); | 133 GetLoaderForURL(url)->LoadService(this, url, pipe.handle0.Pass()); |
| 132 | 134 |
| 133 service_factory = | 135 service_factory = |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // Called from ~ServiceFactory, so we do not need to call Destroy here. | 181 // Called from ~ServiceFactory, so we do not need to call Destroy here. |
| 180 const GURL url = service_factory->url(); | 182 const GURL url = service_factory->url(); |
| 181 URLToServiceFactoryMap::iterator it = url_to_service_factory_.find(url); | 183 URLToServiceFactoryMap::iterator it = url_to_service_factory_.find(url); |
| 182 DCHECK(it != url_to_service_factory_.end()); | 184 DCHECK(it != url_to_service_factory_.end()); |
| 183 delete it->second; | 185 delete it->second; |
| 184 url_to_service_factory_.erase(it); | 186 url_to_service_factory_.erase(it); |
| 185 GetLoaderForURL(url)->OnServiceError(this, url); | 187 GetLoaderForURL(url)->OnServiceError(this, url); |
| 186 } | 188 } |
| 187 | 189 |
| 188 } // namespace mojo | 190 } // namespace mojo |
| OLD | NEW |