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

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: Update new copyrights 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
« no previous file with comments | « mojo/shell/dynamic_service_loader.h ('k') | mojo/shell/service_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "mojo/shell/dynamic_service_loader.h"
6
7 #include "base/callback_helpers.h"
8 #include "base/command_line.h"
9 #include "base/file_util.h"
10 #include "base/scoped_native_library.h"
11 #include "base/threading/simple_thread.h"
12 #include "mojo/shell/context.h"
13 #include "mojo/shell/switches.h"
14
15 typedef MojoResult (*MojoMainFunction)(MojoHandle pipe);
16
17 namespace mojo {
18 namespace shell {
19
20 class DynamicServiceLoader::LoadContext
21 : public mojo::shell::Loader::Delegate,
22 public base::DelegateSimpleThread::Delegate {
23 public:
24 LoadContext(DynamicServiceLoader* loader,
25 const GURL& url,
26 ScopedMessagePipeHandle service_handle)
27 : loader_(loader),
28 url_(url),
29 service_handle_(service_handle.Pass()),
30 weak_factory_(this) {
31 url_ = url;
32 if (url.scheme() == "mojo") {
33 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
34 std::string origin =
35 command_line.GetSwitchValueASCII(switches::kOrigin);
36 #if defined(OS_WIN)
37 std::string lib(url.ExtractFileName() + ".dll");
38 #elif defined(OS_LINUX)
39 std::string lib("lib" + url.ExtractFileName() + ".so");
40 #elif defined(OS_MACOSX)
41 std::string lib("lib" + url.ExtractFileName() + ".dylib");
42 #else
43 std::string lib;
44 NOTREACHED() << "dynamic loading of services not supported";
45 return;
46 #endif
47 url_ = GURL(origin + std::string("/") + lib);
48 }
49 request_ = loader_->context_->loader()->Load(url_, this);
50 }
51
52 private:
53 friend class base::WeakPtrFactory<LoadContext>;
54
55 // From Loader::Delegate.
56 virtual void DidCompleteLoad(const GURL& app_url,
57 const base::FilePath& app_path) OVERRIDE {
58 app_path_ = app_path;
59 ack_closure_ =
60 base::Bind(&DynamicServiceLoader::LoadContext::AppCompleted,
61 weak_factory_.GetWeakPtr());
62 thread_.reset(new base::DelegateSimpleThread(this, "app_thread"));
63 thread_->Start();
64 }
65
66 // From base::DelegateSimpleThread::Delegate.
67 virtual void Run() OVERRIDE {
68 base::ScopedClosureRunner app_deleter(
69 base::Bind(base::IgnoreResult(&base::DeleteFile), app_path_, false));
70 std::string load_error;
71 base::ScopedNativeLibrary app_library(
72 base::LoadNativeLibrary(app_path_, &load_error));
73 if (!app_library.is_valid()) {
74 LOG(ERROR) << "Failed to load library: " << app_path_.value().c_str();
75 LOG(ERROR) << "error: " << load_error;
76 return;
77 }
78
79 MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>(
80 app_library.GetFunctionPointer("MojoMain"));
81 if (!main_function) {
82 LOG(ERROR) << "Entrypoint MojoMain not found.";
83 return;
84 }
85
86 MojoHandle handle = service_handle_.release().value();
87 // |MojoMain()| takes ownership of the app handle.
88 MojoResult result = main_function(handle);
89 if (result < MOJO_RESULT_OK) {
90 LOG(ERROR) << "MojoMain returned an error: " << result;
91 return;
92 }
93 loader_->context_->task_runners()->ui_runner()->PostTask(
94 FROM_HERE,
95 ack_closure_);
96 }
97
98 void AppCompleted() {
99 thread_->Join();
100 thread_.reset();
101 loader_->url_to_load_context_.erase(url_);
102 delete this;
103 }
104
105 DynamicServiceLoader* loader_;
106 GURL url_;
107 base::FilePath app_path_;
108 base::Closure ack_closure_;
109 scoped_ptr<mojo::shell::Loader::Job> request_;
110 scoped_ptr<base::DelegateSimpleThread> thread_;
111 ScopedMessagePipeHandle service_handle_;
112 base::WeakPtrFactory<LoadContext> weak_factory_;
113 };
114
115 DynamicServiceLoader::DynamicServiceLoader(Context* context)
116 : context_(context) {
117 }
118 DynamicServiceLoader::~DynamicServiceLoader() {}
119
120 void DynamicServiceLoader::Load(const GURL& url,
121 ScopedMessagePipeHandle service_handle) {
122 DCHECK(url_to_load_context_.find(url) == url_to_load_context_.end());
123 url_to_load_context_[url] = new LoadContext(this, url, service_handle.Pass());
124 }
125
126 } // namespace shell
127 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/shell/dynamic_service_loader.h ('k') | mojo/shell/service_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698