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