1 // Copyright 2015 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 "android_webview/browser/aw_render_process.h"
6 #include "android_webview/browser_jni_headers/AwDebug_jni.h"
7 #include "android_webview/common/crash_reporter/crash_keys.h"
8 #include "base/android/jni_android.h"
9 #include "base/android/jni_string.h"
10 #include "base/no_destructor.h"
11 #include "components/crash/core/common/crash_key.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/render_process_host_creation_observer.h"
14 
15 using content::RenderProcessHost;
16 
17 namespace android_webview {
18 
19 class AwDebugCpuAffinity : public content::RenderProcessHostCreationObserver {
20  public:
AwDebugCpuAffinity()21   AwDebugCpuAffinity() {
22     for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
23          !i.IsAtEnd(); i.Advance()) {
24       RenderProcessHost* process_host = i.GetCurrentValue();
25       if (process_host) {
26         SetAffinityForProcessHost(process_host);
27       }
28     }
29   }
30 
31   // content::RenderProcessHostCreationObserver:
OnRenderProcessHostCreated(RenderProcessHost * process_host)32   void OnRenderProcessHostCreated(RenderProcessHost* process_host) override {
33     SetAffinityForProcessHost(process_host);
34   }
35 
36  private:
SetAffinityForProcessHost(RenderProcessHost * process_host)37   void SetAffinityForProcessHost(RenderProcessHost* process_host) {
38     process_host->PostTaskWhenProcessIsReady(base::BindOnce(
39         &AwRenderProcess::SetCpuAffinityToLittleCores,
40         base::Unretained(
41             AwRenderProcess::GetInstanceForRenderProcessHost(process_host))));
42   }
43 };
44 
JNI_AwDebug_SetCpuAffinityToLittleCores(JNIEnv * env)45 static void JNI_AwDebug_SetCpuAffinityToLittleCores(JNIEnv* env) {
46   static base::NoDestructor<AwDebugCpuAffinity> aw_debug_cpu_affinity;
47 }
48 
JNI_AwDebug_SetSupportLibraryWebkitVersionCrashKey(JNIEnv * env,const base::android::JavaParamRef<jstring> & version)49 static void JNI_AwDebug_SetSupportLibraryWebkitVersionCrashKey(
50     JNIEnv* env,
51     const base::android::JavaParamRef<jstring>& version) {
52   static ::crash_reporter::CrashKeyString<32> crash_key(
53       crash_keys::kSupportLibraryWebkitVersion);
54   crash_key.Set(ConvertJavaStringToUTF8(env, version));
55 }
56 
57 }  // namespace android_webview
58