Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / mojo / runner / android / main.cc
blob2b4de3f70fa9820849c2739473135d2f9f40c242
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
3 // found in the LICENSE file.
5 #include "mojo/runner/android/main.h"
7 #include "base/android/fifo_utils.h"
8 #include "base/android/jni_android.h"
9 #include "base/android/jni_array.h"
10 #include "base/android/jni_string.h"
11 #include "base/at_exit.h"
12 #include "base/bind.h"
13 #include "base/command_line.h"
14 #include "base/debug/stack_trace.h"
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/lazy_instance.h"
18 #include "base/logging.h"
19 #include "base/macros.h"
20 #include "base/message_loop/message_loop.h"
21 #include "base/run_loop.h"
22 #include "build/build_config.h"
23 #include "components/view_manager/android_loader.h"
24 #include "jni/ShellMain_jni.h"
25 #include "mojo/message_pump/message_pump_mojo.h"
26 #include "mojo/runner/android/android_handler_loader.h"
27 #include "mojo/runner/android/background_application_loader.h"
28 #include "mojo/runner/android/context_init.h"
29 #include "mojo/runner/android/ui_application_loader_android.h"
30 #include "mojo/runner/child_process.h"
31 #include "mojo/runner/context.h"
32 #include "mojo/runner/init.h"
33 #include "mojo/shell/application_loader.h"
34 #include "ui/gl/gl_surface_egl.h"
36 using base::LazyInstance;
38 namespace mojo {
39 namespace runner {
41 namespace {
43 // Tag for logging.
44 const char kLogTag[] = "chromium";
46 // Command line argument for the communication fifo.
47 const char kFifoPath[] = "fifo-path";
49 LazyInstance<scoped_ptr<base::MessageLoop>> g_java_message_loop =
50 LAZY_INSTANCE_INITIALIZER;
52 LazyInstance<scoped_ptr<Context>> g_context = LAZY_INSTANCE_INITIALIZER;
54 LazyInstance<base::android::ScopedJavaGlobalRef<jobject>> g_main_activiy =
55 LAZY_INSTANCE_INITIALIZER;
57 void ConfigureAndroidServices(Context* context) {
58 context->application_manager()->SetLoaderForURL(
59 make_scoped_ptr(new UIApplicationLoader(
60 make_scoped_ptr(new view_manager::AndroidLoader()),
61 g_java_message_loop.Get().get())),
62 GURL("mojo:view_manager"));
64 // Android handler is bundled with the Mojo shell, because it uses the
65 // MojoShell application as the JNI bridge to bootstrap execution of other
66 // Android Mojo apps that need JNI.
67 context->application_manager()->SetLoaderForURL(
68 make_scoped_ptr(new BackgroundApplicationLoader(
69 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler",
70 base::MessageLoop::TYPE_DEFAULT)),
71 GURL("mojo:android_handler"));
74 void ExitShell() {
75 Java_ShellMain_finishActivity(base::android::AttachCurrentThread(),
76 g_main_activiy.Get().obj());
77 exit(0);
80 // Initialize stdout redirection if the command line switch is present.
81 void InitializeRedirection() {
82 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(kFifoPath))
83 return;
85 base::FilePath fifo_path =
86 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(kFifoPath);
87 base::FilePath directory = fifo_path.DirName();
88 CHECK(base::CreateDirectoryAndGetError(directory, nullptr))
89 << "Unable to create directory: " << directory.value();
90 unlink(fifo_path.value().c_str());
91 CHECK(base::android::CreateFIFO(fifo_path, 0666))
92 << "Unable to create fifo: " << fifo_path.value();
93 CHECK(base::android::RedirectStream(stdout, fifo_path, "w"))
94 << "Failed to redirect stdout to file: " << fifo_path.value();
95 CHECK(dup2(STDOUT_FILENO, STDERR_FILENO) != -1)
96 << "Unable to redirect stderr to stdout.";
99 } // namespace
101 static void Init(JNIEnv* env,
102 const JavaParamRef<jclass>& clazz,
103 const JavaParamRef<jobject>& activity,
104 const JavaParamRef<jstring>& mojo_shell_path,
105 const JavaParamRef<jobjectArray>& jparameters,
106 const JavaParamRef<jstring>& j_local_apps_directory,
107 const JavaParamRef<jstring>& j_tmp_dir) {
108 g_main_activiy.Get().Reset(env, activity);
110 // Setting the TMPDIR environment variable so that applications can use it.
111 // TODO(qsr) We will need our subprocesses to inherit this.
112 int return_value =
113 setenv("TMPDIR",
114 base::android::ConvertJavaStringToUTF8(env, j_tmp_dir).c_str(), 1);
115 DCHECK_EQ(return_value, 0);
117 base::android::InitApplicationContext(env, activity);
119 std::vector<std::string> parameters;
120 parameters.push_back(
121 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path));
122 base::android::AppendJavaStringArrayToStringVector(env, jparameters,
123 &parameters);
124 base::CommandLine::Init(0, nullptr);
125 base::CommandLine::ForCurrentProcess()->InitFromArgv(parameters);
127 InitializeLogging();
128 mojo::runner::WaitForDebuggerIfNecessary();
130 InitializeRedirection();
132 // We want ~MessageLoop to happen prior to ~Context. Initializing
133 // LazyInstances is akin to stack-allocating objects; their destructors
134 // will be invoked first-in-last-out.
135 Context* shell_context = new Context();
136 shell_context->SetShellFileRoot(base::FilePath(
137 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory)));
138 g_context.Get().reset(shell_context);
140 g_java_message_loop.Get().reset(new base::MessageLoopForUI);
141 base::MessageLoopForUI::current()->Start();
143 ConfigureAndroidServices(shell_context);
144 shell_context->Init();
146 // This is done after the main message loop is started since it may post
147 // tasks. This is consistent with the ordering from the desktop version of
148 // this file (../desktop/launcher_process.cc).
149 InitContext(shell_context);
151 // TODO(abarth): At which point should we switch to cross-platform
152 // initialization?
154 gfx::GLSurface::InitializeOneOff();
157 static void Start(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
158 #if defined(MOJO_SHELL_DEBUG_URL)
159 base::CommandLine::ForCurrentProcess()->AppendArg(MOJO_SHELL_DEBUG_URL);
160 // Sleep for 5 seconds to give the debugger a chance to attach.
161 sleep(5);
162 #endif
164 Context* context = g_context.Pointer()->get();
165 context->RunCommandLineApplication(base::Bind(ExitShell));
168 static void AddApplicationURL(JNIEnv* env,
169 const JavaParamRef<jclass>& clazz,
170 const JavaParamRef<jstring>& jurl) {
171 base::CommandLine::ForCurrentProcess()->AppendArg(
172 base::android::ConvertJavaStringToUTF8(env, jurl));
175 bool RegisterShellMain(JNIEnv* env) {
176 return RegisterNativesImpl(env);
179 Context* GetContext() {
180 return g_context.Get().get();
183 } // namespace runner
184 } // namespace mojo
186 int main(int argc, char** argv) {
187 base::AtExitManager at_exit;
188 base::CommandLine::Init(argc, argv);
190 #if !defined(OFFICIAL_BUILD)
191 base::debug::EnableInProcessStackDumping();
192 #endif
194 mojo::runner::InitializeLogging();
195 return mojo::runner::ChildProcessMain();