Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / app / android / child_process_service.cc
1 // Copyright (c) 2012 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 "content/app/android/child_process_service.h"
6
7 #include <android/native_window_jni.h>
8 #include <cpu-features.h>
9
10 #include "base/android/jni_array.h"
11 #include "base/android/library_loader/library_loader_hooks.h"
12 #include "base/android/memory_pressure_listener_android.h"
13 #include "base/logging.h"
14 #include "base/posix/global_descriptors.h"
15 #include "content/child/child_thread.h"
16 #include "content/common/android/surface_texture_peer.h"
17 #include "content/common/gpu/gpu_surface_lookup.h"
18 #include "content/public/app/android_library_loader_hooks.h"
19 #include "content/public/common/content_descriptors.h"
20 #include "ipc/ipc_descriptors.h"
21 #include "jni/ChildProcessService_jni.h"
22 #include "ui/gl/android/scoped_java_surface.h"
23
24 using base::android::AttachCurrentThread;
25 using base::android::CheckException;
26 using base::android::JavaIntArrayToIntVector;
27
28 namespace content {
29
30 namespace {
31
32 class SurfaceTexturePeerChildImpl : public content::SurfaceTexturePeer,
33                                     public content::GpuSurfaceLookup {
34  public:
35   // |service| is the instance of
36   // org.chromium.content.app.ChildProcessService.
37   explicit SurfaceTexturePeerChildImpl(
38       const base::android::ScopedJavaLocalRef<jobject>& service)
39       : service_(service) {
40     GpuSurfaceLookup::InitInstance(this);
41   }
42
43   virtual ~SurfaceTexturePeerChildImpl() {
44     GpuSurfaceLookup::InitInstance(NULL);
45   }
46
47   virtual void EstablishSurfaceTexturePeer(
48       base::ProcessHandle pid,
49       scoped_refptr<gfx::SurfaceTexture> surface_texture,
50       int primary_id,
51       int secondary_id) OVERRIDE {
52     JNIEnv* env = base::android::AttachCurrentThread();
53     content::Java_ChildProcessService_establishSurfaceTexturePeer(
54         env, service_.obj(), pid,
55         surface_texture->j_surface_texture().obj(), primary_id,
56         secondary_id);
57     CheckException(env);
58   }
59
60   virtual gfx::AcceleratedWidget AcquireNativeWidget(int surface_id) OVERRIDE {
61     JNIEnv* env = base::android::AttachCurrentThread();
62     gfx::ScopedJavaSurface surface(
63         content::Java_ChildProcessService_getViewSurface(
64         env, service_.obj(), surface_id));
65
66     if (surface.j_surface().is_null())
67       return NULL;
68
69     ANativeWindow* native_window = ANativeWindow_fromSurface(
70         env, surface.j_surface().obj());
71
72     return native_window;
73   }
74
75  private:
76   // The instance of org.chromium.content.app.ChildProcessService.
77   base::android::ScopedJavaGlobalRef<jobject> service_;
78
79   DISALLOW_COPY_AND_ASSIGN(SurfaceTexturePeerChildImpl);
80 };
81
82 // Chrome actually uses the renderer code path for all of its child
83 // processes such as renderers, plugins, etc.
84 void InternalInitChildProcess(const std::vector<int>& file_ids,
85                               const std::vector<int>& file_fds,
86                               JNIEnv* env,
87                               jclass clazz,
88                               jobject context,
89                               jobject service_in,
90                               jint cpu_count,
91                               jlong cpu_features) {
92   base::android::ScopedJavaLocalRef<jobject> service(env, service_in);
93
94   // Set the CPU properties.
95   android_setCpu(cpu_count, cpu_features);
96   // Register the file descriptors.
97   // This includes the IPC channel, the crash dump signals and resource related
98   // files.
99   DCHECK(file_fds.size() == file_ids.size());
100   for (size_t i = 0; i < file_ids.size(); ++i)
101     base::GlobalDescriptors::GetInstance()->Set(file_ids[i], file_fds[i]);
102
103   content::SurfaceTexturePeer::InitInstance(
104       new SurfaceTexturePeerChildImpl(service));
105
106   base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(env);
107 }
108
109 }  // namespace <anonymous>
110
111 void InitChildProcess(JNIEnv* env,
112                       jclass clazz,
113                       jobject context,
114                       jobject service,
115                       jintArray j_file_ids,
116                       jintArray j_file_fds,
117                       jint cpu_count,
118                       jlong cpu_features) {
119   std::vector<int> file_ids;
120   std::vector<int> file_fds;
121   JavaIntArrayToIntVector(env, j_file_ids, &file_ids);
122   JavaIntArrayToIntVector(env, j_file_fds, &file_fds);
123
124   InternalInitChildProcess(
125       file_ids, file_fds, env, clazz, context, service,
126       cpu_count, cpu_features);
127 }
128
129 void ExitChildProcess(JNIEnv* env, jclass clazz) {
130   VLOG(0) << "ChildProcessService: Exiting child process.";
131   base::android::LibraryLoaderExitHook();
132   _exit(0);
133 }
134
135 bool RegisterChildProcessService(JNIEnv* env) {
136   return RegisterNativesImpl(env);
137 }
138
139 void ShutdownMainThread(JNIEnv* env, jobject obj) {
140   ChildThread::ShutdownThread();
141 }
142
143 }  // namespace content