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 ScopedJavaLocalRef
<jobject
> ProfileAndroid::GetLastUsedProfile(JNIEnv
* env
,
53 Profile
* profile
= ProfileManager::GetLastUsedProfile();
54 if (profile
== NULL
) {
55 NOTREACHED() << "Profile not found.";
56 return ScopedJavaLocalRef
<jobject
>();
59 ProfileAndroid
* profile_android
= ProfileAndroid::FromProfile(profile
);
60 if (profile_android
== NULL
) {
61 NOTREACHED() << "ProfileAndroid not found.";
62 return ScopedJavaLocalRef
<jobject
>();
65 return ScopedJavaLocalRef
<jobject
>(profile_android
->obj_
);
68 void ProfileAndroid::DestroyWhenAppropriate(JNIEnv
* env
, jobject obj
) {
69 // Don't delete the Profile directly because the corresponding
70 // RenderViewHost might not be deleted yet.
71 ProfileDestroyer::DestroyProfileWhenAppropriate(profile_
);
74 base::android::ScopedJavaLocalRef
<jobject
> ProfileAndroid::GetOriginalProfile(
75 JNIEnv
* env
, jobject obj
) {
76 ProfileAndroid
* original_profile
= ProfileAndroid::FromProfile(
77 profile_
->GetOriginalProfile());
78 DCHECK(original_profile
);
79 return original_profile
->GetJavaObject();
82 base::android::ScopedJavaLocalRef
<jobject
>
83 ProfileAndroid::GetOffTheRecordProfile(JNIEnv
* env
, jobject obj
) {
84 ProfileAndroid
* otr_profile
= ProfileAndroid::FromProfile(
85 profile_
->GetOffTheRecordProfile());
87 return otr_profile
->GetJavaObject();
90 jboolean
ProfileAndroid::HasOffTheRecordProfile(JNIEnv
* env
, jobject obj
) {
91 return profile_
->HasOffTheRecordProfile();
94 jboolean
ProfileAndroid::IsOffTheRecord(JNIEnv
* env
, jobject obj
) {
95 return profile_
->IsOffTheRecord();
99 ScopedJavaLocalRef
<jobject
> GetLastUsedProfile(
101 const JavaParamRef
<jclass
>& clazz
) {
102 return ProfileAndroid::GetLastUsedProfile(env
, clazz
);
105 ProfileAndroid::ProfileAndroid(Profile
* profile
)
106 : profile_(profile
) {
107 JNIEnv
* env
= AttachCurrentThread();
108 base::android::ScopedJavaLocalRef
<jobject
> jprofile
=
109 Java_Profile_create(env
, reinterpret_cast<intptr_t>(this));
110 obj_
.Reset(env
, jprofile
.obj());
113 ProfileAndroid::~ProfileAndroid() {
114 Java_Profile_onNativeDestroyed(AttachCurrentThread(), obj_
.obj());
117 base::android::ScopedJavaLocalRef
<jobject
> ProfileAndroid::GetJavaObject() {
118 return base::android::ScopedJavaLocalRef
<jobject
>(obj_
);