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

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

Issue 122173004: Add test for ServiceManager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix compile error Created 6 years, 11 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
(Empty)
1 #include "mojo/shell/dynamic_service_loader.h"
2
3 #include "base/callback_helpers.h"
4 #include "base/command_line.h"
5 #include "base/file_util.h"
6 #include "base/scoped_native_library.h"
7 #include "base/threading/simple_thread.h"
8 #include "mojo/shell/context.h"
9 #include "mojo/shell/switches.h"
10
11 typedef MojoResult (*MojoMainFunction)(MojoHandle pipe);
12
13 namespace mojo {
14 namespace shell {
15
16 class DynamicServiceLoader::LoadContext
17 : public mojo::shell::Loader::Delegate,
18 public base::DelegateSimpleThread::Delegate {
19 public:
20 LoadContext(DynamicServiceLoader* loader,
21 const GURL& url,
22 ScopedMessagePipeHandle service_handle)
23 : loader_(loader),
24 url_(url),
25 service_handle_(service_handle.Pass()),
26 weak_factory_(this) {
27 url_ = url;
28 if (url.scheme() == "mojo") {
29 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
30 std::string origin =
31 command_line.GetSwitchValueASCII(switches::kOrigin);
32 #if defined(OS_WIN)
33 std::string lib(url.ExtractFileName() + ".dll");
34 #elif defined(OS_LINUX)
35 std::string lib("lib" + url.ExtractFileName() + ".so");
36 #elif defined(OS_MACOSX)
37 std::string lib("lib" + url.ExtractFileName() + ".dylib");
38 #else
39 std::string lib;
40 NOTREACHED() << "dynamic loading of services not supported";
41 return;
42 #endif
43 url_ = GURL(origin + std::string("/") + lib);
44 }
45 request_ = loader_->context_->loader()->Load(url_, this);
46 }
47
48 private:
49 friend class base::WeakPtrFactory<LoadContext>;
50
51 // From Loader::Delegate.
52 virtual void DidCompleteLoad(const GURL& app_url,
53 const base::FilePath& app_path) OVERRIDE {
54 app_path_ = app_path;
55 ack_closure_ =
56 base::Bind(&DynamicServiceLoader::LoadContext::AppCompleted,
57 weak_factory_.GetWeakPtr());
58 thread_.reset(new base::DelegateSimpleThread(this, "app_thread"));
59 thread_->Start();
60 }
61
62 // From base::DelegateSimpleThread::Delegate.
63 virtual void Run() OVERRIDE {
64 base::ScopedClosureRunner app_deleter(
65 base::Bind(base::IgnoreResult(&base::DeleteFile), app_path_, false));
66 std::string load_error;
67 base::ScopedNativeLibrary app_library(
68 base::LoadNativeLibrary(app_path_, &load_error));
69 if (!app_library.is_valid()) {
70 LOG(ERROR) << "Failed to load library: " << app_path_.value().c_str();
71 LOG(ERROR) << "error: " << load_error;
72 return;
73 }
74
75 MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>(
76 app_library.GetFunctionPointer("MojoMain"));
77 if (!main_function) {
78 LOG(ERROR) << "Entrypoint MojoMain not found.";
79 return;
80 }
81
82 MojoHandle handle = service_handle_.release().value();
83 // |MojoMain()| takes ownership of the app handle.
84 MojoResult result = main_function(handle);
85 if (result < MOJO_RESULT_OK) {
86 LOG(ERROR) << "MojoMain returned an error: " << result;
87 return;
88 }
89 loader_->context_->task_runners()->ui_runner()->PostTask(
90 FROM_HERE,
91 ack_closure_);
92 }
93
94 void AppCompleted() {
95 thread_->Join();
96 thread_.reset();
97 loader_->url_to_load_context_.erase(url_);
98 delete this;
99 }
100
101 DynamicServiceLoader* loader_;
102 GURL url_;
103 base::FilePath app_path_;
104 base::Closure ack_closure_;
105 scoped_ptr<mojo::shell::Loader::Job> request_;
106 scoped_ptr<base::DelegateSimpleThread> thread_;
107 ScopedMessagePipeHandle service_handle_;
108 base::WeakPtrFactory<LoadContext> weak_factory_;
109 };
110
111 DynamicServiceLoader::DynamicServiceLoader(Context* context)
112 : context_(context) {
113 }
114 DynamicServiceLoader::~DynamicServiceLoader() {}
115
116 void DynamicServiceLoader::Load(const GURL& url,
117 ScopedMessagePipeHandle service_handle) {
118 DCHECK(url_to_load_context_.find(url) == url_to_load_context_.end());
119 url_to_load_context_[url] = new LoadContext(this, url, service_handle.Pass());
120 }
121
122 } // namespace shell
123 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698