Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: mojo/shell/service_connector.cc

Issue 137623017: Cleanup Service<> and ServiceFactory<> when clients go away. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
(...skipping 28 matching lines...) Expand all
73 ServiceFactory* service_factory; 80 ServiceFactory* service_factory;
74 if (service_it != url_to_service_factory_.end()) { 81 if (service_it != url_to_service_factory_.end()) {
75 service_factory = service_it->second; 82 service_factory = service_it->second;
76 } else { 83 } else {
77 service_factory = new ServiceFactory(this, url); 84 service_factory = new ServiceFactory(this, url);
78 url_to_service_factory_[url] = service_factory; 85 url_to_service_factory_[url] = service_factory;
79 } 86 }
80 service_factory->ConnectToClient(client_handle.Pass()); 87 service_factory->ConnectToClient(client_handle.Pass());
81 } 88 }
82 89
90 bool ServiceConnector::HasFactoryForURL(const GURL& url) const {
91 return url_to_service_factory_.find(url) != url_to_service_factory_.end();
92 }
93
94 void ServiceConnector::RemoveServiceFactory(ServiceFactory* service_factory) {
95 ServiceFactoryMap::iterator it =
96 url_to_service_factory_.find(service_factory->url());
97 DCHECK(it != url_to_service_factory_.end());
98 delete it->second;
99 url_to_service_factory_.erase(it);
100 }
101
83 } // namespace shell 102 } // namespace shell
84 } // namespace mojo 103 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698