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