| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/shell/service_connector.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "mojo/public/bindings/allocation_scope.h" | |
| 9 #include "mojo/public/bindings/error_handler.h" | |
| 10 #include "mojo/public/bindings/remote_ptr.h" | |
| 11 #include "mojom/shell.h" | |
| 12 | |
| 13 namespace mojo { | |
| 14 namespace shell { | |
| 15 | |
| 16 class ServiceConnector::ServiceFactory : public Shell, public ErrorHandler { | |
| 17 public: | |
| 18 ServiceFactory(ServiceConnector* connector, const GURL& url) | |
| 19 : connector_(connector), | |
| 20 url_(url) { | |
| 21 InterfacePipe<Shell> pipe; | |
| 22 shell_client_.reset(pipe.handle_to_peer.Pass(), this, this); | |
| 23 connector_->GetLoaderForURL(url)->Load(url, pipe.handle_to_self.Pass()); | |
| 24 } | |
| 25 virtual ~ServiceFactory() {} | |
| 26 | |
| 27 void ConnectToClient(ScopedMessagePipeHandle handle) { | |
| 28 if (handle.is_valid()) { | |
| 29 AllocationScope scope; | |
| 30 shell_client_->AcceptConnection(url_.spec(), handle.Pass()); | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 virtual void Connect(const String& url, | |
| 35 ScopedMessagePipeHandle client_pipe) MOJO_OVERRIDE { | |
| 36 connector_->Connect(GURL(url.To<std::string>()), client_pipe.Pass()); | |
| 37 } | |
| 38 | |
| 39 virtual void OnError() MOJO_OVERRIDE { | |
| 40 connector_->RemoveServiceFactory(this); | |
| 41 } | |
| 42 | |
| 43 const GURL& url() const { return url_; } | |
| 44 | |
| 45 private: | |
| 46 ServiceConnector* const connector_; | |
| 47 const GURL url_; | |
| 48 RemotePtr<ShellClient> shell_client_; | |
| 49 DISALLOW_COPY_AND_ASSIGN(ServiceFactory); | |
| 50 }; | |
| 51 | |
| 52 ServiceConnector::Loader::Loader() {} | |
| 53 ServiceConnector::Loader::~Loader() {} | |
| 54 | |
| 55 bool ServiceConnector::TestAPI::HasFactoryForURL(const GURL& url) const { | |
| 56 return connector_->url_to_service_factory_.find(url) != | |
| 57 connector_->url_to_service_factory_.end(); | |
| 58 } | |
| 59 | |
| 60 ServiceConnector::ServiceConnector() : default_loader_(NULL) { | |
| 61 } | |
| 62 | |
| 63 ServiceConnector::~ServiceConnector() { | |
| 64 for (ServiceFactoryMap::iterator it = url_to_service_factory_.begin(); | |
| 65 it != url_to_service_factory_.end(); ++it) { | |
| 66 delete it->second; | |
| 67 } | |
| 68 url_to_service_factory_.clear(); | |
| 69 } | |
| 70 | |
| 71 void ServiceConnector::SetLoaderForURL(Loader* loader, const GURL& gurl) { | |
| 72 DCHECK(url_to_loader_.find(gurl) == url_to_loader_.end()); | |
| 73 url_to_loader_[gurl] = loader; | |
| 74 } | |
| 75 | |
| 76 ServiceConnector::Loader* ServiceConnector::GetLoaderForURL(const GURL& gurl) { | |
| 77 LoaderMap::const_iterator it = url_to_loader_.find(gurl); | |
| 78 if (it != url_to_loader_.end()) | |
| 79 return it->second; | |
| 80 DCHECK(default_loader_); | |
| 81 return default_loader_; | |
| 82 } | |
| 83 | |
| 84 void ServiceConnector::Connect(const GURL& url, | |
| 85 ScopedMessagePipeHandle client_handle) { | |
| 86 ServiceFactoryMap::const_iterator service_it = | |
| 87 url_to_service_factory_.find(url); | |
| 88 ServiceFactory* service_factory; | |
| 89 if (service_it != url_to_service_factory_.end()) { | |
| 90 service_factory = service_it->second; | |
| 91 } else { | |
| 92 service_factory = new ServiceFactory(this, url); | |
| 93 url_to_service_factory_[url] = service_factory; | |
| 94 } | |
| 95 service_factory->ConnectToClient(client_handle.Pass()); | |
| 96 } | |
| 97 | |
| 98 void ServiceConnector::RemoveServiceFactory(ServiceFactory* service_factory) { | |
| 99 ServiceFactoryMap::iterator it = | |
| 100 url_to_service_factory_.find(service_factory->url()); | |
| 101 DCHECK(it != url_to_service_factory_.end()); | |
| 102 delete it->second; | |
| 103 url_to_service_factory_.erase(it); | |
| 104 } | |
| 105 | |
| 106 } // namespace shell | |
| 107 } // namespace mojo | |
| OLD | NEW |