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
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/profiles/profile.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/profiles/profile_key_android.h"
#include "chrome/common/chrome_constants.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/browser/profiles/android/jni_headers/Profile_jni.h"
using jni_zero::AttachCurrentThread;
using jni_zero::JavaParamRef;
using jni_zero::JavaRef;
using jni_zero::ScopedJavaLocalRef;
// static
Profile* Profile::FromJavaObject(const JavaRef<jobject>& obj) {
if (!obj) {
return nullptr;
}
return reinterpret_cast<Profile*>(
Java_Profile_getNativePointer(AttachCurrentThread(), obj));
}
void Profile::InitJavaObject() {
// Java side assumes |this| can also be used as a BrowserContext*.
CHECK(
reinterpret_cast<intptr_t>(this) ==
reinterpret_cast<intptr_t>(static_cast<content::BrowserContext*>(this)));
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> j_otr_profile_id =
otr_profile_id_.has_value()
? otr_profile_id_->ConvertToJavaOTRProfileID(env)
: nullptr;
j_obj_ = Java_Profile_Constructor(env, reinterpret_cast<intptr_t>(this),
j_otr_profile_id);
}
void Profile::NotifyJavaOnProfileWillBeDestroyed() {
Java_Profile_onProfileWillBeDestroyed(AttachCurrentThread(), j_obj_);
}
void Profile::DestroyJavaObject() {
Java_Profile_onNativeDestroyed(AttachCurrentThread(), j_obj_);
}
ScopedJavaLocalRef<jobject> Profile::GetJavaObject() const {
return ScopedJavaLocalRef<jobject>(AttachCurrentThread(), j_obj_);
}
ScopedJavaLocalRef<jobject> JNI_Profile_GetOriginalProfile(JNIEnv* env,
jlong ptr) {
Profile* self = reinterpret_cast<Profile*>(ptr);
Profile* original_profile = self->GetOriginalProfile();
DCHECK(original_profile);
return original_profile->GetJavaObject();
}
jboolean JNI_Profile_IsInitialProfile(JNIEnv* env, jlong ptr) {
Profile* self = reinterpret_cast<Profile*>(ptr);
return self->GetBaseName().value() == chrome::kInitialProfile;
}
ScopedJavaLocalRef<jobject> JNI_Profile_GetOffTheRecordProfile(
JNIEnv* env,
jlong ptr,
const JavaParamRef<jobject>& j_otr_profile_id,
const jboolean j_create_if_needed) {
Profile* self = reinterpret_cast<Profile*>(ptr);
Profile::OTRProfileID otr_profile_id =
Profile::OTRProfileID::ConvertFromJavaOTRProfileID(env, j_otr_profile_id);
Profile* otr_profile =
self->GetOffTheRecordProfile(otr_profile_id, j_create_if_needed);
if (!j_create_if_needed && !otr_profile) {
return nullptr;
}
DCHECK(otr_profile);
return otr_profile->GetJavaObject();
}
ScopedJavaLocalRef<jobject> JNI_Profile_GetPrimaryOtrProfile(
JNIEnv* env,
jlong ptr,
const jboolean j_create_if_needed) {
Profile* self = reinterpret_cast<Profile*>(ptr);
Profile* otr_profile = self->GetPrimaryOTRProfile(j_create_if_needed);
if (!j_create_if_needed && !otr_profile) {
return nullptr;
}
DCHECK(otr_profile);
return otr_profile->GetJavaObject();
}
jboolean JNI_Profile_HasOffTheRecordProfile(
JNIEnv* env,
jlong ptr,
const JavaParamRef<jobject>& j_otr_profile_id) {
Profile* self = reinterpret_cast<Profile*>(ptr);
Profile::OTRProfileID otr_profile_id =
Profile::OTRProfileID::ConvertFromJavaOTRProfileID(env, j_otr_profile_id);
return self->HasOffTheRecordProfile(otr_profile_id);
}
jboolean JNI_Profile_HasPrimaryOtrProfile(JNIEnv* env, jlong ptr) {
Profile* self = reinterpret_cast<Profile*>(ptr);
return self->HasPrimaryOTRProfile();
}
ScopedJavaLocalRef<jobject> JNI_Profile_GetProfileKey(JNIEnv* env, jlong ptr) {
Profile* self = reinterpret_cast<Profile*>(ptr);
ProfileKeyAndroid* profile_key =
self->GetProfileKey()->GetProfileKeyAndroid();
DCHECK(profile_key);
return profile_key->GetJavaObject();
}
jboolean JNI_Profile_IsChild(JNIEnv* env, jlong ptr) {
Profile* self = reinterpret_cast<Profile*>(ptr);
return self->IsChild();
}
void JNI_Profile_Wipe(JNIEnv* env, jlong ptr) {
Profile* self = reinterpret_cast<Profile*>(ptr);
self->Wipe();
}
ScopedJavaLocalRef<jobject> JNI_Profile_FromWebContents(
JNIEnv* env,
const JavaParamRef<jobject>& jweb_contents) {
auto* web_contents = content::WebContents::FromJavaWebContents(jweb_contents);
if (!web_contents) {
return nullptr;
}
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
if (!profile) {
return nullptr;
}
return profile->GetJavaObject();
}
|