1 // Copyright 2013 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.
5 #include "chrome/browser/profiles/profile_android.h"
7 #include "base/android/jni_android.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/profiles/profile_destroyer.h"
10 #include "chrome/browser/profiles/profile_manager.h"
11 #include "jni/Profile_jni.h"
13 using base::android::AttachCurrentThread
;
16 const char kProfileAndroidKey
[] = "profile_android";
20 ProfileAndroid
* ProfileAndroid::FromProfile(Profile
* profile
) {
24 ProfileAndroid
* profile_android
= static_cast<ProfileAndroid
*>(
25 profile
->GetUserData(kProfileAndroidKey
));
26 if (!profile_android
) {
27 profile_android
= new ProfileAndroid(profile
);
28 profile
->SetUserData(kProfileAndroidKey
, profile_android
);
30 return profile_android
;
34 Profile
* ProfileAndroid::FromProfileAndroid(jobject obj
) {
38 ProfileAndroid
* profile_android
= reinterpret_cast<ProfileAndroid
*>(
39 Java_Profile_getNativePointer(AttachCurrentThread(), obj
));
42 return profile_android
->profile_
;
46 bool ProfileAndroid::RegisterProfileAndroid(JNIEnv
* env
) {
47 return RegisterNativesImpl(env
);
51 jobject
ProfileAndroid::GetLastUsedProfile(JNIEnv
* env
, jclass clazz
) {
52 Profile
* profile
= ProfileManager::GetLastUsedProfile();
53 if (profile
== NULL
) {
54 NOTREACHED() << "Profile not found.";
58 ProfileAndroid
* profile_android
= ProfileAndroid::FromProfile(profile
);
59 if (profile_android
== NULL
) {
60 NOTREACHED() << "ProfileAndroid not found.";
64 return profile_android
->obj_
.obj();
67 void ProfileAndroid::DestroyWhenAppropriate(JNIEnv
* env
, jobject obj
) {
68 // Don't delete the Profile directly because the corresponding
69 // RenderViewHost might not be deleted yet.
70 ProfileDestroyer::DestroyProfileWhenAppropriate(profile_
);
73 base::android::ScopedJavaLocalRef
<jobject
> ProfileAndroid::GetOriginalProfile(
74 JNIEnv
* env
, jobject obj
) {
75 ProfileAndroid
* original_profile
= ProfileAndroid::FromProfile(
76 profile_
->GetOriginalProfile());
77 DCHECK(original_profile
);
78 return original_profile
->GetJavaObject();
81 base::android::ScopedJavaLocalRef
<jobject
>
82 ProfileAndroid::GetOffTheRecordProfile(JNIEnv
* env
, jobject obj
) {
83 ProfileAndroid
* otr_profile
= ProfileAndroid::FromProfile(
84 profile_
->GetOffTheRecordProfile());
86 return otr_profile
->GetJavaObject();
89 jboolean
ProfileAndroid::HasOffTheRecordProfile(JNIEnv
* env
, jobject obj
) {
90 return profile_
->HasOffTheRecordProfile();
93 jboolean
ProfileAndroid::IsOffTheRecord(JNIEnv
* env
, jobject obj
) {
94 return profile_
->IsOffTheRecord();
98 jobject
GetLastUsedProfile(JNIEnv
* env
, jclass clazz
) {
99 return ProfileAndroid::GetLastUsedProfile(env
, clazz
);
102 ProfileAndroid::ProfileAndroid(Profile
* profile
)
103 : profile_(profile
) {
104 JNIEnv
* env
= AttachCurrentThread();
105 base::android::ScopedJavaLocalRef
<jobject
> jprofile
=
106 Java_Profile_create(env
, reinterpret_cast<intptr_t>(this));
107 obj_
.Reset(env
, jprofile
.obj());
110 ProfileAndroid::~ProfileAndroid() {
111 Java_Profile_onNativeDestroyed(AttachCurrentThread(), obj_
.obj());
114 base::android::ScopedJavaLocalRef
<jobject
> ProfileAndroid::GetJavaObject() {
115 return base::android::ScopedJavaLocalRef
<jobject
>(obj_
);