File: content_child_process_service_delegate.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (166 lines) | stat: -rw-r--r-- 6,206 bytes parent folder | download | duplicates (3)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <cpu-features.h>

#include "base/android/jni_array.h"
#include "base/android/library_loader/library_loader_hooks.h"
#include "base/android/memory_pressure_listener_android.h"
#include "base/android/unguessable_token_android.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/unguessable_token.h"
#include "components/input/android/input_token_forwarder.h"
#include "content/child/child_thread_impl.h"
#include "content/common/android/surface_wrapper.h"
#include "content/common/shared_file_util.h"
#include "content/public/common/content_descriptors.h"
#include "content/public/common/content_switches.h"
#include "gpu/command_buffer/service/texture_owner.h"
#include "gpu/ipc/common/gpu_surface_lookup.h"
#include "ui/gl/android/scoped_java_surface.h"
#include "ui/gl/android/scoped_java_surface_control.h"
#include "ui/gl/android/surface_texture.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "content/public/android/content_app_jni/ContentChildProcessServiceDelegate_jni.h"

using base::android::AttachCurrentThread;
using base::android::JavaParamRef;

namespace content {

namespace {

// TODO(sievers): Use two different implementations of this depending on if
// we're in a renderer or gpu process.
class ChildProcessSurfaceManager : public gpu::GpuSurfaceLookup,
                                   public input::InputTokenForwarder {
 public:
  ChildProcessSurfaceManager() {}

  ChildProcessSurfaceManager(const ChildProcessSurfaceManager&) = delete;
  ChildProcessSurfaceManager& operator=(const ChildProcessSurfaceManager&) =
      delete;

  ~ChildProcessSurfaceManager() override {}

  // |service_impl| is the instance of
  // org.chromium.content.app.ChildProcessService.
  void SetServiceImpl(const base::android::JavaRef<jobject>& service_impl) {
    service_impl_.Reset(service_impl);
  }

  // Overridden from GpuSurfaceLookup:
  gpu::SurfaceRecord AcquireJavaSurface(int surface_id) override {
    JNIEnv* env = base::android::AttachCurrentThread();
    base::android::ScopedJavaLocalRef<jobject> surface_wrapper =
        content::Java_ContentChildProcessServiceDelegate_getViewSurface(
            env, service_impl_, surface_id);
    if (!surface_wrapper)
      return gpu::SurfaceRecord(gl::ScopedJavaSurface(),
                                /*can_be_used_with_surface_control=*/false);

    bool can_be_used_with_surface_control =
        JNI_SurfaceWrapper_canBeUsedWithSurfaceControl(env, surface_wrapper);
    bool wraps_surface =
        JNI_SurfaceWrapper_getWrapsSurface(env, surface_wrapper);

    if (wraps_surface) {
      gl::ScopedJavaSurface surface(
          content::JNI_SurfaceWrapper_takeSurface(env, surface_wrapper),
          /*auto_release=*/true);
      DCHECK(!surface.j_surface().is_null());
      return gpu::SurfaceRecord(
          std::move(surface), can_be_used_with_surface_control,
          content::JNI_SurfaceWrapper_getBrowserInputToken(env,
                                                           surface_wrapper));
    } else {
      gl::ScopedJavaSurfaceControl surface_control(
          JNI_SurfaceWrapper_takeSurfaceControl(env, surface_wrapper),
          /*release_on_destroy=*/true);
      return gpu::SurfaceRecord(std::move(surface_control));
    }
  }

  // input::InputTokenForwarder overrides.
  void ForwardVizInputTransferToken(
      int surface_id,
      base::android::ScopedJavaGlobalRef<jobject> viz_input_token) override {
    JNIEnv* env = base::android::AttachCurrentThread();
    content::Java_ContentChildProcessServiceDelegate_forwardInputTransferToken(
        env, service_impl_, surface_id, viz_input_token);
  }

 private:
  friend struct base::LazyInstanceTraitsBase<ChildProcessSurfaceManager>;
  // The instance of org.chromium.content.app.ChildProcessService.
  base::android::ScopedJavaGlobalRef<jobject> service_impl_;
};

base::LazyInstance<ChildProcessSurfaceManager>::Leaky
    g_child_process_surface_manager = LAZY_INSTANCE_INITIALIZER;

// Chrome actually uses the renderer code path for all of its child
// processes such as renderers, plugins, etc.
void JNI_ContentChildProcessServiceDelegate_InternalInitChildProcess(
    JNIEnv* env,
    const JavaParamRef<jobject>& service_impl,
    jint cpu_count,
    jlong cpu_features) {
  // Set the CPU properties.
  android_setCpu(cpu_count, cpu_features);

  g_child_process_surface_manager.Get().SetServiceImpl(service_impl);

  gpu::GpuSurfaceLookup::InitInstance(
      g_child_process_surface_manager.Pointer());
  input::InputTokenForwarder::SetInstance(
      g_child_process_surface_manager.Pointer());
}

}  // namespace

void JNI_ContentChildProcessServiceDelegate_InitChildProcess(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj,
    jint cpu_count,
    jlong cpu_features) {
  JNI_ContentChildProcessServiceDelegate_InternalInitChildProcess(
      env, obj, cpu_count, cpu_features);
}

void JNI_ContentChildProcessServiceDelegate_InitMemoryPressureListener(
    JNIEnv* env) {
  base::android::MemoryPressureListenerAndroid::Initialize(env);
}

void JNI_ContentChildProcessServiceDelegate_RetrieveFileDescriptorsIdsToKeys(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj) {
  std::map<int, std::string> ids_to_keys;
  std::string file_switch_value =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kSharedFiles);

  std::vector<int> ids;
  std::vector<std::string> keys;
  if (!file_switch_value.empty()) {
    std::optional<std::map<int, std::string>> ids_to_keys_from_command_line =
        ParseSharedFileSwitchValue(file_switch_value);
    if (ids_to_keys_from_command_line) {
      for (auto iter : *ids_to_keys_from_command_line) {
        ids.push_back(iter.first);
        keys.push_back(iter.second);
      }
    }
  }

  Java_ContentChildProcessServiceDelegate_setFileDescriptorsIdsToKeys(
      env, obj, base::android::ToJavaIntArray(env, ids),
      base::android::ToJavaArrayOfStrings(env, keys));
}

}  // namespace content