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" |
(...skipping 14 matching lines...) Expand all Loading... |
25 public: | 25 public: |
26 ServiceFactory(ServiceManager* manager, const GURL& url) | 26 ServiceFactory(ServiceManager* manager, const GURL& url) |
27 : manager_(manager), | 27 : manager_(manager), |
28 url_(url) { | 28 url_(url) { |
29 InterfacePipe<Shell> pipe; | 29 InterfacePipe<Shell> pipe; |
30 shell_client_.reset(pipe.handle_to_peer.Pass(), this, this); | 30 shell_client_.reset(pipe.handle_to_peer.Pass(), this, this); |
31 manager_->GetLoaderForURL(url)->LoadService(manager_, | 31 manager_->GetLoaderForURL(url)->LoadService(manager_, |
32 url, | 32 url, |
33 pipe.handle_to_self.Pass()); | 33 pipe.handle_to_self.Pass()); |
34 } | 34 } |
| 35 |
35 virtual ~ServiceFactory() {} | 36 virtual ~ServiceFactory() {} |
36 | 37 |
37 void ConnectToClient(ScopedMessagePipeHandle handle) { | 38 void ConnectToClient(ScopedMessagePipeHandle handle) { |
38 if (handle.is_valid()) { | 39 if (handle.is_valid()) { |
39 AllocationScope scope; | 40 AllocationScope scope; |
40 shell_client_->AcceptConnection(url_.spec(), handle.Pass()); | 41 shell_client_->AcceptConnection(url_.spec(), handle.Pass()); |
41 } | 42 } |
42 } | 43 } |
43 | 44 |
44 virtual void Connect(const String& url, | 45 virtual void Connect(const String& url, |
(...skipping 21 matching lines...) Expand all Loading... |
66 | 67 |
67 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { | 68 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { |
68 return manager_->url_to_service_factory_.find(url) != | 69 return manager_->url_to_service_factory_.find(url) != |
69 manager_->url_to_service_factory_.end(); | 70 manager_->url_to_service_factory_.end(); |
70 } | 71 } |
71 | 72 |
72 ServiceManager::ServiceManager() : default_loader_(NULL) { | 73 ServiceManager::ServiceManager() : default_loader_(NULL) { |
73 } | 74 } |
74 | 75 |
75 ServiceManager::~ServiceManager() { | 76 ServiceManager::~ServiceManager() { |
76 for (ServiceFactoryMap::iterator it = url_to_service_factory_.begin(); | 77 for (URLToServiceFactoryMap::iterator it = url_to_service_factory_.begin(); |
77 it != url_to_service_factory_.end(); ++it) { | 78 it != url_to_service_factory_.end(); ++it) { |
78 delete it->second; | 79 delete it->second; |
79 } | 80 } |
80 url_to_service_factory_.clear(); | 81 url_to_service_factory_.clear(); |
81 } | 82 } |
82 | 83 |
83 // static | 84 // static |
84 ServiceManager* ServiceManager::GetInstance() { | 85 ServiceManager* ServiceManager::GetInstance() { |
85 static base::LazyInstance<ServiceManager> instance = | 86 static base::LazyInstance<ServiceManager> instance = |
86 LAZY_INSTANCE_INITIALIZER; | 87 LAZY_INSTANCE_INITIALIZER; |
87 has_created_instance = true; | 88 has_created_instance = true; |
88 return &instance.Get(); | 89 return &instance.Get(); |
89 } | 90 } |
90 | 91 |
91 void ServiceManager::SetLoaderForURL(ServiceLoader* loader, const GURL& gurl) { | |
92 DCHECK(url_to_loader_.find(gurl) == url_to_loader_.end()); | |
93 url_to_loader_[gurl] = loader; | |
94 } | |
95 | |
96 ServiceLoader* ServiceManager::GetLoaderForURL(const GURL& gurl) { | |
97 LoaderMap::const_iterator it = url_to_loader_.find(gurl); | |
98 if (it != url_to_loader_.end()) | |
99 return it->second; | |
100 DCHECK(default_loader_); | |
101 return default_loader_; | |
102 } | |
103 | |
104 void ServiceManager::Connect(const GURL& url, | 92 void ServiceManager::Connect(const GURL& url, |
105 ScopedMessagePipeHandle client_handle) { | 93 ScopedMessagePipeHandle client_handle) { |
106 ServiceFactoryMap::const_iterator service_it = | 94 URLToServiceFactoryMap::const_iterator service_it = |
107 url_to_service_factory_.find(url); | 95 url_to_service_factory_.find(url); |
108 ServiceFactory* service_factory; | 96 ServiceFactory* service_factory; |
109 if (service_it != url_to_service_factory_.end()) { | 97 if (service_it != url_to_service_factory_.end()) { |
110 service_factory = service_it->second; | 98 service_factory = service_it->second; |
111 } else { | 99 } else { |
112 service_factory = new ServiceFactory(this, url); | 100 service_factory = new ServiceFactory(this, url); |
113 url_to_service_factory_[url] = service_factory; | 101 url_to_service_factory_[url] = service_factory; |
114 } | 102 } |
115 service_factory->ConnectToClient(client_handle.Pass()); | 103 service_factory->ConnectToClient(client_handle.Pass()); |
116 } | 104 } |
117 | 105 |
| 106 void ServiceManager::SetLoaderForURL(ServiceLoader* loader, const GURL& url) { |
| 107 DCHECK(url_to_loader_.find(url) == url_to_loader_.end()); |
| 108 url_to_loader_[url] = loader; |
| 109 } |
| 110 |
| 111 void ServiceManager::SetLoaderForScheme(ServiceLoader* loader, |
| 112 const std::string& scheme) { |
| 113 DCHECK(scheme_to_loader_.find(scheme) == scheme_to_loader_.end()); |
| 114 scheme_to_loader_[scheme] = loader; |
| 115 } |
| 116 |
| 117 ServiceLoader* ServiceManager::GetLoaderForURL(const GURL& url) { |
| 118 URLToLoaderMap::const_iterator url_it = url_to_loader_.find(url); |
| 119 if (url_it != url_to_loader_.end()) |
| 120 return url_it->second; |
| 121 SchemeToLoaderMap::const_iterator scheme_it = |
| 122 scheme_to_loader_.find(url.scheme()); |
| 123 if (scheme_it != scheme_to_loader_.end()) |
| 124 return scheme_it->second; |
| 125 DCHECK(default_loader_); |
| 126 return default_loader_; |
| 127 } |
| 128 |
118 void ServiceManager::OnServiceFactoryError(ServiceFactory* service_factory) { | 129 void ServiceManager::OnServiceFactoryError(ServiceFactory* service_factory) { |
119 const GURL url = service_factory->url(); | 130 const GURL url = service_factory->url(); |
120 ServiceFactoryMap::iterator it = url_to_service_factory_.find(url); | 131 URLToServiceFactoryMap::iterator it = url_to_service_factory_.find(url); |
121 DCHECK(it != url_to_service_factory_.end()); | 132 DCHECK(it != url_to_service_factory_.end()); |
122 delete it->second; | 133 delete it->second; |
123 url_to_service_factory_.erase(it); | 134 url_to_service_factory_.erase(it); |
124 GetLoaderForURL(url)->OnServiceError(this, url); | 135 GetLoaderForURL(url)->OnServiceError(this, url); |
125 } | 136 } |
126 | 137 |
127 } // namespace mojo | 138 } // namespace mojo |
OLD | NEW |