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> |
| 6 |
5 #include "mojo/service_manager/service_manager.h" | 7 #include "mojo/service_manager/service_manager.h" |
6 | 8 |
7 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/macros.h" | 11 #include "base/macros.h" |
10 #include "mojo/public/bindings/allocation_scope.h" | 12 #include "mojo/public/bindings/allocation_scope.h" |
11 #include "mojo/public/bindings/error_handler.h" | 13 #include "mojo/public/bindings/error_handler.h" |
12 #include "mojo/public/bindings/remote_ptr.h" | 14 #include "mojo/public/bindings/remote_ptr.h" |
13 #include "mojo/service_manager/service_loader.h" | 15 #include "mojo/service_manager/service_loader.h" |
14 | 16 |
15 namespace mojo { | 17 namespace mojo { |
16 | 18 |
| 19 namespace { |
| 20 // Used by TestAPI. |
| 21 bool has_created_instance = false; |
| 22 } |
| 23 |
17 class ServiceManager::ServiceFactory : public Shell, public ErrorHandler { | 24 class ServiceManager::ServiceFactory : public Shell, public ErrorHandler { |
18 public: | 25 public: |
19 ServiceFactory(ServiceManager* manager, const GURL& url) | 26 ServiceFactory(ServiceManager* manager, const GURL& url) |
20 : manager_(manager), | 27 : manager_(manager), |
21 url_(url) { | 28 url_(url) { |
22 InterfacePipe<Shell> pipe; | 29 InterfacePipe<Shell> pipe; |
23 shell_client_.reset(pipe.handle_to_peer.Pass(), this, this); | 30 shell_client_.reset(pipe.handle_to_peer.Pass(), this, this); |
24 manager_->GetLoaderForURL(url)->LoadService(manager_, | 31 manager_->GetLoaderForURL(url)->LoadService(manager_, |
25 url, | 32 url, |
26 pipe.handle_to_self.Pass()); | 33 pipe.handle_to_self.Pass()); |
(...skipping 18 matching lines...) Expand all Loading... |
45 | 52 |
46 const GURL& url() const { return url_; } | 53 const GURL& url() const { return url_; } |
47 | 54 |
48 private: | 55 private: |
49 ServiceManager* const manager_; | 56 ServiceManager* const manager_; |
50 const GURL url_; | 57 const GURL url_; |
51 RemotePtr<ShellClient> shell_client_; | 58 RemotePtr<ShellClient> shell_client_; |
52 DISALLOW_COPY_AND_ASSIGN(ServiceFactory); | 59 DISALLOW_COPY_AND_ASSIGN(ServiceFactory); |
53 }; | 60 }; |
54 | 61 |
| 62 // static |
| 63 bool ServiceManager::TestAPI::HasCreatedInstance() { |
| 64 return has_created_instance; |
| 65 } |
| 66 |
55 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { | 67 bool ServiceManager::TestAPI::HasFactoryForURL(const GURL& url) const { |
56 return manager_->url_to_service_factory_.find(url) != | 68 return manager_->url_to_service_factory_.find(url) != |
57 manager_->url_to_service_factory_.end(); | 69 manager_->url_to_service_factory_.end(); |
58 } | 70 } |
59 | 71 |
60 ServiceManager::ServiceManager() : default_loader_(NULL) { | 72 ServiceManager::ServiceManager() : default_loader_(NULL) { |
61 } | 73 } |
62 | 74 |
63 ServiceManager::~ServiceManager() { | 75 ServiceManager::~ServiceManager() { |
64 for (ServiceFactoryMap::iterator it = url_to_service_factory_.begin(); | 76 for (ServiceFactoryMap::iterator it = url_to_service_factory_.begin(); |
65 it != url_to_service_factory_.end(); ++it) { | 77 it != url_to_service_factory_.end(); ++it) { |
66 delete it->second; | 78 delete it->second; |
67 } | 79 } |
68 url_to_service_factory_.clear(); | 80 url_to_service_factory_.clear(); |
69 } | 81 } |
70 | 82 |
71 // static | 83 // static |
72 ServiceManager* GetInstance() { | 84 ServiceManager* ServiceManager::GetInstance() { |
73 static base::LazyInstance<ServiceManager> instance = | 85 static base::LazyInstance<ServiceManager> instance = |
74 LAZY_INSTANCE_INITIALIZER; | 86 LAZY_INSTANCE_INITIALIZER; |
| 87 has_created_instance = true; |
75 return &instance.Get(); | 88 return &instance.Get(); |
76 } | 89 } |
77 | 90 |
78 void ServiceManager::SetLoaderForURL(ServiceLoader* loader, const GURL& gurl) { | 91 void ServiceManager::SetLoaderForURL(ServiceLoader* loader, const GURL& gurl) { |
79 DCHECK(url_to_loader_.find(gurl) == url_to_loader_.end()); | 92 DCHECK(url_to_loader_.find(gurl) == url_to_loader_.end()); |
80 url_to_loader_[gurl] = loader; | 93 url_to_loader_[gurl] = loader; |
81 } | 94 } |
82 | 95 |
83 ServiceLoader* ServiceManager::GetLoaderForURL(const GURL& gurl) { | 96 ServiceLoader* ServiceManager::GetLoaderForURL(const GURL& gurl) { |
84 LoaderMap::const_iterator it = url_to_loader_.find(gurl); | 97 LoaderMap::const_iterator it = url_to_loader_.find(gurl); |
(...skipping 19 matching lines...) Expand all Loading... |
104 | 117 |
105 void ServiceManager::RemoveServiceFactory(ServiceFactory* service_factory) { | 118 void ServiceManager::RemoveServiceFactory(ServiceFactory* service_factory) { |
106 ServiceFactoryMap::iterator it = | 119 ServiceFactoryMap::iterator it = |
107 url_to_service_factory_.find(service_factory->url()); | 120 url_to_service_factory_.find(service_factory->url()); |
108 DCHECK(it != url_to_service_factory_.end()); | 121 DCHECK(it != url_to_service_factory_.end()); |
109 delete it->second; | 122 delete it->second; |
110 url_to_service_factory_.erase(it); | 123 url_to_service_factory_.erase(it); |
111 } | 124 } |
112 | 125 |
113 } // namespace mojo | 126 } // namespace mojo |
OLD | NEW |