summaryrefslogtreecommitdiffstats
path: root/content/browser/android/sandboxed_process_launcher.cc
blob: f46f2bbb4d06fad518fb40175a7205c8c202a5d2 (plain)
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
// Copyright (c) 2012 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 "content/browser/android/sandboxed_process_launcher.h"

#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "jni/SandboxedProcessLauncher_jni.h"

using base::android::AttachCurrentThread;
using base::android::ToJavaArrayOfStrings;
using base::android::ScopedJavaLocalRef;
using base::GlobalDescriptors;
using content::StartSandboxedProcessCallback;

namespace content {

// Called from SandboxedProcessLauncher.java when the SandboxedProcess was
// started.
// |client_context| is the pointer to StartSandboxedProcessCallback which was
// passed in from StartSandboxedProcess.
// |handle| is the processID of the child process as originated in Java.
static void OnSandboxedProcessStarted(JNIEnv*,
                                      jclass,
                                      jint client_context,
                                      jint handle) {
  StartSandboxedProcessCallback* callback =
      reinterpret_cast<StartSandboxedProcessCallback*>(client_context);
  callback->Run(static_cast<base::ProcessHandle>(handle));
  delete callback;
}

void StartSandboxedProcess(
    const CommandLine::StringVector& argv,
    int ipc_fd,
    const GlobalDescriptors::Mapping& files_to_register,
    const StartSandboxedProcessCallback& callback) {
  JNIEnv* env = AttachCurrentThread();
  DCHECK(env);

  // Create the Command line String[]
  ScopedJavaLocalRef<jobjectArray> j_argv = ToJavaArrayOfStrings(env, argv);

  ScopedJavaLocalRef<jintArray> j_file_to_register_id_files(env,
      env->NewIntArray(files_to_register.size() * 2));
  scoped_array<jint> file_to_register_id_files(
      new jint[files_to_register.size() * 2]);
  for (size_t i = 0; i < files_to_register.size(); ++i) {
    const GlobalDescriptors::KeyFDPair& id_file = files_to_register[i];
    file_to_register_id_files[2 * i] = id_file.first;
    file_to_register_id_files[(2 * i) + 1] = id_file.second;
  }
  env->SetIntArrayRegion(j_file_to_register_id_files.obj(),
                         0, files_to_register.size() * 2,
                         file_to_register_id_files.get());
  Java_SandboxedProcessLauncher_start(env,
          base::android::GetApplicationContext(),
          static_cast<jobjectArray>(j_argv.obj()),
          static_cast<jint>(ipc_fd),
          j_file_to_register_id_files.obj(),
          reinterpret_cast<jint>(new StartSandboxedProcessCallback(callback)));
}

void StopSandboxedProcess(base::ProcessHandle handle) {
  JNIEnv* env = AttachCurrentThread();
  DCHECK(env);
  Java_SandboxedProcessLauncher_stop(env, static_cast<jint>(handle));
}

bool RegisterSandboxedProcessLauncher(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

}  // namespace content