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_manager.h"
10 #include "jni/Profile_jni.h"
12 using base::android::AttachCurrentThread
;
15 const char kProfileAndroidKey
[] = "profile_android";
19 ProfileAndroid
* ProfileAndroid::FromProfile(Profile
* profile
) {
23 ProfileAndroid
* profile_android
= static_cast<ProfileAndroid
*>(
24 profile
->GetUserData(kProfileAndroidKey
));
25 if (!profile_android
) {
26 profile_android
= new ProfileAndroid(profile
);
27 profile
->SetUserData(kProfileAndroidKey
, profile_android
);
29 return profile_android
;
33 Profile
* ProfileAndroid::FromProfileAndroid(jobject obj
) {
37 ProfileAndroid
* profile_android
= reinterpret_cast<ProfileAndroid
*>(
38 Java_Profile_getNativePointer(AttachCurrentThread(), obj
));
41 return profile_android
->profile_
;
45 bool ProfileAndroid::RegisterProfileAndroid(JNIEnv
* env
) {
46 return RegisterNativesImpl(env
);
50 jobject
ProfileAndroid::GetLastUsedProfile(JNIEnv
* env
, jclass clazz
) {
51 Profile
* profile
= ProfileManager::GetLastUsedProfile();
52 if (profile
== NULL
) {
53 NOTREACHED() << "Profile not found.";
57 ProfileAndroid
* profile_android
= ProfileAndroid::FromProfile(profile
);
58 if (profile_android
== NULL
) {
59 NOTREACHED() << "ProfileAndroid not found.";
63 return profile_android
->obj_
.obj();
66 base::android::ScopedJavaLocalRef
<jobject
> ProfileAndroid::GetOriginalProfile(
67 JNIEnv
* env
, jobject obj
) {
68 ProfileAndroid
* original_profile
= ProfileAndroid::FromProfile(
69 profile_
->GetOriginalProfile());
70 DCHECK(original_profile
);
71 return original_profile
->GetJavaObject();
74 base::android::ScopedJavaLocalRef
<jobject
>
75 ProfileAndroid::GetOffTheRecordProfile(JNIEnv
* env
, jobject obj
) {
76 ProfileAndroid
* otr_profile
= ProfileAndroid::FromProfile(
77 profile_
->GetOffTheRecordProfile());
79 return otr_profile
->GetJavaObject();
82 jboolean
ProfileAndroid::HasOffTheRecordProfile(JNIEnv
* env
, jobject obj
) {
83 return profile_
->HasOffTheRecordProfile();
86 jboolean
ProfileAndroid::IsOffTheRecord(JNIEnv
* env
, jobject obj
) {
87 return profile_
->IsOffTheRecord();
91 jobject
GetLastUsedProfile(JNIEnv
* env
, jclass clazz
) {
92 return ProfileAndroid::GetLastUsedProfile(env
, clazz
);
95 ProfileAndroid::ProfileAndroid(Profile
* profile
)
97 JNIEnv
* env
= AttachCurrentThread();
98 base::android::ScopedJavaLocalRef
<jobject
> jprofile
=
99 Java_Profile_create(env
, reinterpret_cast<intptr_t>(this));
100 obj_
.Reset(env
, jprofile
.obj());
103 ProfileAndroid::~ProfileAndroid() {
104 Java_Profile_destroy(AttachCurrentThread(), obj_
.obj());
107 base::android::ScopedJavaLocalRef
<jobject
> ProfileAndroid::GetJavaObject() {
108 return base::android::ScopedJavaLocalRef
<jobject
>(obj_
);