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
android_webview / browser / tracing / aw_tracing_controller.cc [blame]
// 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 "android_webview/browser/tracing/aw_tracing_controller.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/functional/bind.h"
#include "base/memory/ref_counted_memory.h"
#include "base/trace_event/trace_config.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/tracing_controller.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "android_webview/browser_jni_headers/AwTracingController_jni.h"
using base::android::JavaParamRef;
namespace {
class AwTraceDataEndpoint
: public content::TracingController::TraceDataEndpoint {
public:
using ReceivedChunkCallback =
base::RepeatingCallback<void(std::unique_ptr<std::string>)>;
static scoped_refptr<content::TracingController::TraceDataEndpoint> Create(
ReceivedChunkCallback received_chunk_callback,
base::OnceClosure completed_callback) {
return new AwTraceDataEndpoint(std::move(received_chunk_callback),
std::move(completed_callback));
}
void ReceivedTraceFinalContents() override {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, std::move(completed_callback_));
}
void ReceiveTraceChunk(std::unique_ptr<std::string> chunk) override {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(received_chunk_callback_, std::move(chunk)));
}
explicit AwTraceDataEndpoint(ReceivedChunkCallback received_chunk_callback,
base::OnceClosure completed_callback)
: received_chunk_callback_(std::move(received_chunk_callback)),
completed_callback_(std::move(completed_callback)) {}
AwTraceDataEndpoint(const AwTraceDataEndpoint&) = delete;
AwTraceDataEndpoint& operator=(const AwTraceDataEndpoint&) = delete;
private:
~AwTraceDataEndpoint() override {}
ReceivedChunkCallback received_chunk_callback_;
base::OnceClosure completed_callback_;
};
} // namespace
namespace android_webview {
static jlong JNI_AwTracingController_Init(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
AwTracingController* controller = new AwTracingController(env, obj);
return reinterpret_cast<intptr_t>(controller);
}
AwTracingController::AwTracingController(JNIEnv* env,
const jni_zero::JavaRef<jobject>& obj)
: weak_java_object_(env, obj) {}
AwTracingController::~AwTracingController() {}
bool AwTracingController::Start(JNIEnv* env,
const JavaParamRef<jobject>& obj,
std::string& categories,
jint jmode) {
base::trace_event::TraceConfig trace_config(
categories, static_cast<base::trace_event::TraceRecordMode>(jmode));
return content::TracingController::GetInstance()->StartTracing(
trace_config, content::TracingController::StartTracingDoneCallback());
}
bool AwTracingController::StopAndFlush(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
// privacy_filtering_enabled=true is required for filtering out potential PII.
return content::TracingController::GetInstance()->StopTracing(
AwTraceDataEndpoint::Create(
base::BindRepeating(&AwTracingController::OnTraceDataReceived,
weak_factory_.GetWeakPtr()),
base::BindOnce(&AwTracingController::OnTraceDataComplete,
weak_factory_.GetWeakPtr())),
/*agent_label=*/"",
/*privacy_filtering_enabled=*/true);
}
void AwTracingController::OnTraceDataComplete() {
JNIEnv* env = base::android::AttachCurrentThread();
base::android::ScopedJavaLocalRef<jobject> obj = weak_java_object_.get(env);
if (obj.obj()) {
Java_AwTracingController_onTraceDataComplete(env, obj);
}
}
void AwTracingController::OnTraceDataReceived(
std::unique_ptr<std::string> chunk) {
JNIEnv* env = base::android::AttachCurrentThread();
base::android::ScopedJavaLocalRef<jobject> obj = weak_java_object_.get(env);
if (obj.obj()) {
base::android::ScopedJavaLocalRef<jbyteArray> java_trace_data =
base::android::ToJavaByteArray(env, *chunk);
Java_AwTracingController_onTraceDataChunkReceived(env, obj,
java_trace_data);
}
}
bool AwTracingController::IsTracing(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
return content::TracingController::GetInstance()->IsTracing();
}
} // namespace android_webview