Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / mojo / shell / in_process_dynamic_service_runner.cc
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/in_process_dynamic_service_runner.h"
6
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "mojo/public/platform/native/gles2_impl_chromium_sync_point_thunks.h"
13 #include "mojo/public/platform/native/gles2_impl_chromium_texture_mailbox_thunks.h"
14 #include "mojo/public/platform/native/gles2_impl_thunks.h"
15 #include "mojo/public/platform/native/gles2_thunks.h"
16 #include "mojo/public/platform/native/system_thunks.h"
17
18 namespace mojo {
19 namespace shell {
20
21 namespace {
22
23 template <typename Thunks>
24 bool SetThunks(Thunks (*make_thunks)(),
25                const char* function_name,
26                base::ScopedNativeLibrary* library) {
27   typedef size_t (*SetThunksFn)(const Thunks* thunks);
28   SetThunksFn set_thunks =
29       reinterpret_cast<SetThunksFn>(library->GetFunctionPointer(function_name));
30   if (!set_thunks)
31     return false;
32   Thunks thunks = make_thunks();
33   size_t expected_size = set_thunks(&thunks);
34   if (expected_size > sizeof(Thunks)) {
35     LOG(ERROR) << "Invalid app library: expected " << function_name
36                << " to return thunks of size: " << expected_size;
37     return false;
38   }
39   return true;
40 }
41 }
42
43 InProcessDynamicServiceRunner::InProcessDynamicServiceRunner(
44     Context* context)
45     : keep_alive_(context) {
46 }
47
48 InProcessDynamicServiceRunner::~InProcessDynamicServiceRunner() {
49   if (thread_) {
50     DCHECK(thread_->HasBeenStarted());
51     DCHECK(!thread_->HasBeenJoined());
52     thread_->Join();
53   }
54
55   // It is important to let the thread exit before unloading the DSO because
56   // the library may have registered thread-local data and destructors to run
57   // on thread termination.
58   app_library_.Reset(base::NativeLibrary());
59 }
60
61 void InProcessDynamicServiceRunner::Start(
62     const base::FilePath& app_path,
63     ScopedMessagePipeHandle service_handle,
64     const base::Closure& app_completed_callback) {
65   app_path_ = app_path;
66
67   DCHECK(!service_handle_.is_valid());
68   service_handle_ = service_handle.Pass();
69
70   DCHECK(app_completed_callback_runner_.is_null());
71   app_completed_callback_runner_ = base::Bind(&base::TaskRunner::PostTask,
72                                               base::MessageLoopProxy::current(),
73                                               FROM_HERE,
74                                               app_completed_callback);
75
76   DCHECK(!thread_);
77   thread_.reset(new base::DelegateSimpleThread(this, "app_thread"));
78   thread_->Start();
79 }
80
81 void InProcessDynamicServiceRunner::Run() {
82   DVLOG(2) << "Loading/running Mojo app in process from library: "
83            << app_path_.value();
84
85   do {
86     base::NativeLibraryLoadError error;
87     app_library_.Reset(base::LoadNativeLibrary(app_path_, &error));
88     if (!app_library_.is_valid()) {
89       LOG(ERROR) << "Failed to load app library (error: " << error.ToString()
90                  << ")";
91       break;
92     }
93
94     if (!SetThunks(
95             &MojoMakeSystemThunks, "MojoSetSystemThunks", &app_library_)) {
96       // In the component build, Mojo Apps link against mojo_system_impl.
97 #if !defined(COMPONENT_BUILD)
98       // Strictly speaking this is not required, but it's very unusual to have
99       // an app that doesn't require the basic system library.
100       LOG(WARNING) << "MojoSetSystemThunks not found in app library";
101 #endif
102     }
103
104     if (SetThunks(&MojoMakeGLES2ControlThunks,
105                   "MojoSetGLES2ControlThunks",
106                   &app_library_)) {
107       // If we have the control thunks, we probably also have the
108       // GLES2 implementation thunks.
109       if (!SetThunks(&MojoMakeGLES2ImplThunks,
110                      "MojoSetGLES2ImplThunks",
111                      &app_library_)) {
112         // In the component build, Mojo Apps link against mojo_gles2_impl.
113 #if !defined(COMPONENT_BUILD)
114         // Warn on this really weird case: The library requires the GLES2
115         // control functions, but doesn't require the GLES2 implementation.
116         LOG(WARNING) << "App library has MojoSetGLES2ControlThunks, but "
117                         "doesn't have MojoSetGLES2ImplThunks.";
118 #endif
119       }
120
121       // If the application is using GLES2 extension points, register those
122       // thunks. Applications may use or not use any of these, so don't warn if
123       // they are missing.
124       SetThunks(MojoMakeGLES2ImplChromiumTextureMailboxThunks,
125                 "MojoSetGLES2ImplChromiumTextureMailboxThunks",
126                 &app_library_);
127       SetThunks(MojoMakeGLES2ImplChromiumSyncPointThunks,
128                 "MojoSetGLES2ImplChromiumSyncPointThunks",
129                 &app_library_);
130     }
131     // Unlike system thunks, we don't warn on a lack of GLES2 thunks because
132     // not everything is a visual app.
133
134     typedef MojoResult (*MojoMainFunction)(MojoHandle);
135     MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>(
136         app_library_.GetFunctionPointer("MojoMain"));
137     if (!main_function) {
138       LOG(ERROR) << "Entrypoint MojoMain not found";
139       break;
140     }
141
142     // |MojoMain()| takes ownership of the service handle.
143     MojoResult result = main_function(service_handle_.release().value());
144     if (result < MOJO_RESULT_OK)
145       LOG(ERROR) << "MojoMain returned an error: " << result;
146   } while (false);
147
148   bool success = app_completed_callback_runner_.Run();
149   app_completed_callback_runner_.Reset();
150   LOG_IF(ERROR, !success) << "Failed post run app_completed_callback";
151 }
152
153 }  // namespace shell
154 }  // namespace mojo