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