| 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/public/cpp/bindings/error_handler.h" | 14 #include "mojo/public/cpp/bindings/error_handler.h" |
| 15 #include "mojo/public/cpp/bindings/remote_ptr.h" | |
| 16 #include "mojo/service_manager/service_loader.h" | 15 #include "mojo/service_manager/service_loader.h" |
| 17 | 16 |
| 18 namespace mojo { | 17 namespace mojo { |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 // Used by TestAPI. | 20 // Used by TestAPI. |
| 22 bool has_created_instance = false; | 21 bool has_created_instance = false; |
| 23 } | 22 } |
| 24 | 23 |
| 25 class ServiceManager::ServiceFactory : public Shell, public ErrorHandler { | 24 class ServiceManager::ServiceFactory : public Shell { |
| 26 public: | 25 public: |
| 27 ServiceFactory(ServiceManager* manager, const GURL& url) | 26 ServiceFactory(ServiceManager* manager, const GURL& url) |
| 28 : manager_(manager), | 27 : manager_(manager), |
| 29 url_(url) { | 28 url_(url), |
| 30 InterfacePipe<Shell> pipe; | 29 client_(NULL) { |
| 31 shell_client_.reset(pipe.handle_to_peer.Pass(), this, this); | |
| 32 manager_->GetLoaderForURL(url)->LoadService(manager_, | |
| 33 url, | |
| 34 pipe.handle_to_self.Pass()); | |
| 35 } | 30 } |
| 36 | 31 |
| 37 virtual ~ServiceFactory() {} | 32 virtual ~ServiceFactory() { |
| 33 if (manager_) |
| 34 manager_->RemoveServiceFactory(this); |
| 35 } |
| 36 |
| 37 void Orphan() { |
| 38 manager_ = NULL; |
| 39 } |
| 38 | 40 |
| 39 void ConnectToClient(ScopedMessagePipeHandle handle) { | 41 void ConnectToClient(ScopedMessagePipeHandle handle) { |
| 40 if (handle.is_valid()) { | 42 if (!handle.is_valid()) { |
| 41 AllocationScope scope; | 43 assert(false); |
| 42 shell_client_->AcceptConnection(url_.spec(), handle.Pass()); | 44 return; |
| 43 } | 45 } |
| 46 AllocationScope scope; |
| 47 client_->AcceptConnection(url_.spec(), handle.Pass()); |
| 48 } |
| 49 |
| 50 // Shell implementation: |
| 51 |
| 52 virtual void SetClient(ShellClient* client) OVERRIDE { |
| 53 client_ = client; |
| 44 } | 54 } |
| 45 | 55 |
| 46 virtual void Connect(const String& url, | 56 virtual void Connect(const String& url, |
| 47 ScopedMessagePipeHandle client_pipe) OVERRIDE { | 57 ScopedMessagePipeHandle client_pipe) OVERRIDE { |
| 48 manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass()); | 58 if (manager_) |
| 49 } | 59 manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass()); |
| 50 | |
| 51 virtual void OnError() OVERRIDE { | |
| 52 manager_->OnServiceFactoryError(this); | |
| 53 } | 60 } |
| 54 | 61 |
| 55 const GURL& url() const { return url_; } | 62 const GURL& url() const { return url_; } |
| 56 | 63 |
| 57 private: | 64 private: |
| 58 ServiceManager* const manager_; | 65 ServiceManager* manager_; |
| 59 const GURL url_; | 66 const GURL url_; |
| 60 RemotePtr<ShellClient> shell_client_; | 67 ShellClient* client_; |
| 61 | 68 |
| 62 DISALLOW_COPY_AND_ASSIGN(ServiceFactory); | 69 DISALLOW_COPY_AND_ASSIGN(ServiceFactory); |
| 63 }; | 70 }; |
| 64 | 71 |
| 65 class ServiceManager::TestAPI::TestShellConnection | 72 class ServiceManager::TestAPI::TestShellConnection |
| 66 : public Shell, | 73 : public Shell, |
| 67 public ErrorHandler { | 74 public ErrorHandler { |
| 68 public: | 75 public: |
| 69 explicit TestShellConnection(ServiceManager* manager) : manager_(manager) { | 76 explicit TestShellConnection(ServiceManager* manager) |
| 77 : manager_(manager), |
| 78 client_(NULL) { |
| 79 #if 0 |
| 70 InterfacePipe<Shell> pipe; | 80 InterfacePipe<Shell> pipe; |
| 71 shell_client_.reset(pipe.handle_to_peer.Pass(), this, this); | 81 shell_client_.reset(pipe.handle_to_peer.Pass(), this, this); |
| 72 shell_handle_ = pipe.handle_to_self.Pass(); | 82 shell_handle_ = pipe.handle_to_self.Pass(); |
| 83 #endif |
| 73 } | 84 } |
| 74 virtual ~TestShellConnection() {} | 85 virtual ~TestShellConnection() {} |
| 75 | 86 |
| 76 ScopedShellHandle GetShellHandle() { | 87 ScopedMessagePipeHandle GetShellHandle() { |
| 77 return shell_handle_.Pass(); | 88 return shell_handle_.Pass(); |
| 78 } | 89 } |
| 79 | 90 |
| 80 // Shell: | 91 // Shell: |
| 92 virtual void SetClient(ShellClient* client) OVERRIDE { |
| 93 client_ = client; |
| 94 } |
| 81 virtual void Connect(const String& url, | 95 virtual void Connect(const String& url, |
| 82 ScopedMessagePipeHandle client_pipe) OVERRIDE { | 96 ScopedMessagePipeHandle client_pipe) OVERRIDE { |
| 83 manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass()); | 97 manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass()); |
| 84 } | 98 } |
| 85 | 99 |
| 86 virtual void OnError() OVERRIDE { | 100 virtual void OnError() OVERRIDE { |
| 87 } | 101 } |
| 88 | 102 |
| 89 private: | 103 private: |
| 90 ServiceManager* manager_; | 104 ServiceManager* manager_; |
| 91 RemotePtr<ShellClient> shell_client_; | 105 //XXX RemotePtr<ShellClient> shell_client_; |
| 92 ScopedShellHandle shell_handle_; | 106 ScopedMessagePipeHandle shell_handle_; |
| 107 ShellClient* client_; |
| 93 | 108 |
| 94 DISALLOW_COPY_AND_ASSIGN(TestShellConnection); | 109 DISALLOW_COPY_AND_ASSIGN(TestShellConnection); |
| 95 }; | 110 }; |
| 96 | 111 |
| 97 // static | 112 // static |
| 98 ServiceManager::TestAPI::TestAPI(ServiceManager* manager) : manager_(manager) { | 113 ServiceManager::TestAPI::TestAPI(ServiceManager* manager) : manager_(manager) { |
| 99 } | 114 } |
| 100 | 115 |
| 101 ServiceManager::TestAPI::~TestAPI() { | 116 ServiceManager::TestAPI::~TestAPI() { |
| 102 } | 117 } |
| 103 | 118 |
| 104 bool ServiceManager::TestAPI::HasCreatedInstance() { | 119 bool ServiceManager::TestAPI::HasCreatedInstance() { |
| 105 return has_created_instance; | 120 return has_created_instance; |
| 106 } | 121 } |
| 107 | 122 |
| 108 ScopedShellHandle ServiceManager::TestAPI::GetShellHandle() { | 123 ScopedMessagePipeHandle ServiceManager::TestAPI::GetShellHandle() { |
| 109 if (!shell_connection_.get()) | 124 if (!shell_connection_.get()) |
| 110 shell_connection_.reset(new TestShellConnection(manager_)); | 125 shell_connection_.reset(new TestShellConnection(manager_)); |
| 111 return shell_connection_->GetShellHandle().Pass(); | 126 return shell_connection_->GetShellHandle().Pass(); |
| 112 } | 127 } |
| 113 | 128 |
| 114 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { | 129 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { |
| 115 return manager_->url_to_service_factory_.find(url) != | 130 return manager_->url_to_service_factory_.find(url) != |
| 116 manager_->url_to_service_factory_.end(); | 131 manager_->url_to_service_factory_.end(); |
| 117 } | 132 } |
| 118 | 133 |
| 119 ServiceManager::ServiceManager() | 134 ServiceManager::ServiceManager() |
| 120 : interceptor_(NULL) { | 135 : interceptor_(NULL) { |
| 121 } | 136 } |
| 122 | 137 |
| 123 ServiceManager::~ServiceManager() { | 138 ServiceManager::~ServiceManager() { |
| 124 STLDeleteValues(&url_to_service_factory_); | 139 // Make sure any lingering service factories no longer reference us. |
| 140 for (URLToServiceFactoryMap::iterator it = url_to_service_factory_.begin(); |
| 141 it != url_to_service_factory_.end(); ++it) { |
| 142 it->second->Orphan(); |
| 143 } |
| 144 |
| 125 STLDeleteValues(&url_to_loader_); | 145 STLDeleteValues(&url_to_loader_); |
| 126 STLDeleteValues(&scheme_to_loader_); | 146 STLDeleteValues(&scheme_to_loader_); |
| 127 } | 147 } |
| 128 | 148 |
| 129 // static | 149 // static |
| 130 ServiceManager* ServiceManager::GetInstance() { | 150 ServiceManager* ServiceManager::GetInstance() { |
| 131 static base::LazyInstance<ServiceManager> instance = | 151 static base::LazyInstance<ServiceManager> instance = |
| 132 LAZY_INSTANCE_INITIALIZER; | 152 LAZY_INSTANCE_INITIALIZER; |
| 133 has_created_instance = true; | 153 has_created_instance = true; |
| 134 return &instance.Get(); | 154 return &instance.Get(); |
| 135 } | 155 } |
| 136 | 156 |
| 137 void ServiceManager::Connect(const GURL& url, | 157 void ServiceManager::Connect(const GURL& url, |
| 138 ScopedMessagePipeHandle client_handle) { | 158 ScopedMessagePipeHandle client_handle) { |
| 139 URLToServiceFactoryMap::const_iterator service_it = | 159 URLToServiceFactoryMap::const_iterator service_it = |
| 140 url_to_service_factory_.find(url); | 160 url_to_service_factory_.find(url); |
| 141 ServiceFactory* service_factory; | 161 ServiceFactory* service_factory; |
| 142 if (service_it != url_to_service_factory_.end()) { | 162 if (service_it != url_to_service_factory_.end()) { |
| 143 service_factory = service_it->second; | 163 service_factory = service_it->second; |
| 144 } else { | 164 } else { |
| 145 service_factory = new ServiceFactory(this, url); | 165 service_factory = new ServiceFactory(this, url); |
| 166 |
| 167 GetLoaderForURL(url)->LoadService( |
| 168 this, url, BindToPipe<Shell>(service_factory).Pass()); |
| 169 |
| 170 // NOTE: The lifetime of the service_factory instance is now bound to the |
| 171 // MessagePipe passed to LoadService. We retain a raw pointer to |
| 172 // service_factory so that we can continue communicating with it. |
| 173 |
| 146 url_to_service_factory_[url] = service_factory; | 174 url_to_service_factory_[url] = service_factory; |
| 147 } | 175 } |
| 148 if (interceptor_) { | 176 if (interceptor_) { |
| 149 service_factory->ConnectToClient( | 177 service_factory->ConnectToClient( |
| 150 interceptor_->OnConnectToClient(url, client_handle.Pass())); | 178 interceptor_->OnConnectToClient(url, client_handle.Pass())); |
| 151 } else { | 179 } else { |
| 152 service_factory->ConnectToClient(client_handle.Pass()); | 180 service_factory->ConnectToClient(client_handle.Pass()); |
| 153 } | 181 } |
| 154 } | 182 } |
| 155 | 183 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 178 if (url_it != url_to_loader_.end()) | 206 if (url_it != url_to_loader_.end()) |
| 179 return url_it->second; | 207 return url_it->second; |
| 180 SchemeToLoaderMap::const_iterator scheme_it = | 208 SchemeToLoaderMap::const_iterator scheme_it = |
| 181 scheme_to_loader_.find(url.scheme()); | 209 scheme_to_loader_.find(url.scheme()); |
| 182 if (scheme_it != scheme_to_loader_.end()) | 210 if (scheme_it != scheme_to_loader_.end()) |
| 183 return scheme_it->second; | 211 return scheme_it->second; |
| 184 DCHECK(default_loader_); | 212 DCHECK(default_loader_); |
| 185 return default_loader_.get(); | 213 return default_loader_.get(); |
| 186 } | 214 } |
| 187 | 215 |
| 188 void ServiceManager::OnServiceFactoryError(ServiceFactory* service_factory) { | 216 void ServiceManager::RemoveServiceFactory(ServiceFactory* service_factory) { |
| 189 const GURL url = service_factory->url(); | 217 const GURL url = service_factory->url(); |
| 190 URLToServiceFactoryMap::iterator it = url_to_service_factory_.find(url); | 218 URLToServiceFactoryMap::iterator it = url_to_service_factory_.find(url); |
| 191 DCHECK(it != url_to_service_factory_.end()); | 219 DCHECK(it != url_to_service_factory_.end()); |
| 192 delete it->second; | |
| 193 url_to_service_factory_.erase(it); | 220 url_to_service_factory_.erase(it); |
| 194 GetLoaderForURL(url)->OnServiceError(this, url); | 221 GetLoaderForURL(url)->OnServiceError(this, url); |
| 195 } | 222 } |
| 196 | 223 |
| 197 } // namespace mojo | 224 } // namespace mojo |
| OLD | NEW |