1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/shell/service_manager.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/scoped_native_library.h"
#include "base/threading/simple_thread.h"
#include "mojo/public/bindings/lib/remote_ptr.h"
#include "mojo/shell/context.h"
#include "mojo/shell/switches.h"
#include "mojom/shell.h"
typedef MojoResult (*MojoMainFunction)(MojoHandle pipe);
namespace mojo {
namespace shell {
class ServiceManager::Service : public ShellStub {
public:
Service(ServiceManager* manager, const GURL& url)
: manager_(manager),
url_(url) {
MessagePipe pipe;
shell_client_.reset(pipe.handle0.Pass());
shell_client_.SetPeer(this);
manager_->GetLoaderForURL(url)->Load(url, manager_, pipe.handle1.Pass());
}
virtual ~Service() {}
void ConnectToClient(ScopedMessagePipeHandle handle) {
if (handle.is_valid())
shell_client_->AcceptConnection(handle.Pass());
}
virtual void Connect(const String& url,
ScopedMessagePipeHandle client_pipe) MOJO_OVERRIDE {
manager_->Connect(GURL(url.To<std::string>()), client_pipe.Pass());
}
private:
ServiceManager* manager_;
GURL url_;
RemotePtr<ShellClient> shell_client_;
DISALLOW_COPY_AND_ASSIGN(Service);
};
class ServiceManager::DynamicLoader : public ServiceManager::Loader {
public:
explicit DynamicLoader(ServiceManager* manager) : manager_(manager) {}
virtual ~DynamicLoader() {}
private:
class Context : public mojo::shell::Loader::Delegate,
public base::DelegateSimpleThread::Delegate {
public:
Context(DynamicLoader* loader,
const GURL& url,
ScopedMessagePipeHandle service_handle)
: loader_(loader),
url_(url),
service_handle_(service_handle.Pass()),
weak_factory_(this) {
url_ = url;
if (url.scheme() == "mojo") {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
std::string origin =
command_line.GetSwitchValueASCII(switches::kOrigin);
#if defined(OS_WIN)
std::string lib(url.ExtractFileName() + ".dll");
#elif defined(OS_LINUX)
std::string lib("lib" + url.ExtractFileName() + ".so");
#elif defined(OS_MACOSX)
std::string lib("lib" + url.ExtractFileName() + ".dylib");
#else
std::string lib;
NOTREACHED() << "dynamic loading of services not supported";
return;
#endif
url_ = GURL(origin + std::string("/") + lib);
}
request_ = loader_->manager_->context_->loader()->Load(url_, this);
}
private:
friend class base::WeakPtrFactory<Context>;
// From Loader::Delegate.
virtual void DidCompleteLoad(const GURL& app_url,
const base::FilePath& app_path) OVERRIDE {
app_path_ = app_path;
ack_closure_ =
base::Bind(&ServiceManager::DynamicLoader::Context::AppCompleted,
weak_factory_.GetWeakPtr());
thread_.reset(new base::DelegateSimpleThread(this, "app_thread"));
thread_->Start();
}
// From base::DelegateSimpleThread::Delegate.
virtual void Run() OVERRIDE {
base::ScopedClosureRunner app_deleter(
base::Bind(base::IgnoreResult(&base::DeleteFile), app_path_, false));
std::string load_error;
base::ScopedNativeLibrary app_library(
base::LoadNativeLibrary(app_path_, &load_error));
if (!app_library.is_valid()) {
LOG(ERROR) << "Failed to load library: " << app_path_.value().c_str();
LOG(ERROR) << "error: " << load_error;
return;
}
MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>(
app_library.GetFunctionPointer("MojoMain"));
if (!main_function) {
LOG(ERROR) << "Entrypoint MojoMain not found.";
return;
}
MojoHandle handle = service_handle_.release().value();
// |MojoMain()| takes ownership of the app handle.
MojoResult result = main_function(handle);
if (result < MOJO_RESULT_OK) {
LOG(ERROR) << "MojoMain returned an error: " << result;
return;
}
loader_->manager_->context_->task_runners()->ui_runner()->PostTask(
FROM_HERE,
ack_closure_);
}
void AppCompleted() {
thread_->Join();
thread_.reset();
loader_->url_to_context_.erase(url_);
delete this;
}
DynamicLoader* loader_;
GURL url_;
base::FilePath app_path_;
base::Closure ack_closure_;
scoped_ptr<mojo::shell::Loader::Job> request_;
scoped_ptr<base::DelegateSimpleThread> thread_;
ScopedMessagePipeHandle service_handle_;
base::WeakPtrFactory<Context> weak_factory_;
};
virtual void Load(const GURL& url,
ServiceManager* manager,
ScopedMessagePipeHandle service_handle)
MOJO_OVERRIDE {
DCHECK(url_to_context_.find(url) == url_to_context_.end());
url_to_context_[url] = new Context(this, url, service_handle.Pass());
}
typedef std::map<GURL, Context*> ContextMap;
ContextMap url_to_context_;
ServiceManager* manager_;
};
ServiceManager::Loader::Loader() {}
ServiceManager::Loader::~Loader() {}
ServiceManager::ServiceManager(Context* context)
: context_(context),
default_loader_(new DynamicLoader(this)) {
}
ServiceManager::~ServiceManager() {
}
void ServiceManager::SetLoaderForURL(Loader* loader, const GURL& gurl) {
DCHECK(url_to_loader_.find(gurl) == url_to_loader_.end());
url_to_loader_[gurl] = loader;
}
ServiceManager::Loader* ServiceManager::GetLoaderForURL(const GURL& gurl) {
LoaderMap::const_iterator it = url_to_loader_.find(gurl);
if (it != url_to_loader_.end())
return it->second;
return default_loader_.get();
}
void ServiceManager::Connect(const GURL& url,
ScopedMessagePipeHandle client_handle) {
ServiceMap::const_iterator service_it = url_to_service_.find(url);
Service* service;
if (service_it != url_to_service_.end()) {
service = service_it->second;
} else {
service = new Service(this, url);
url_to_service_[url] = service;
}
service->ConnectToClient(client_handle.Pass());
}
} // namespace shell
} // namespace mojo
|