BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_android.cc
blob9c92901c841ae166c8147e4f8671edf06281fa6a
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;
15 namespace {
16 const char kProfileAndroidKey[] = "profile_android";
17 } // namespace
19 // static
20 ProfileAndroid* ProfileAndroid::FromProfile(Profile* profile) {
21 if (!profile)
22 return NULL;
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;
33 // static
34 Profile* ProfileAndroid::FromProfileAndroid(jobject obj) {
35 if (!obj)
36 return NULL;
38 ProfileAndroid* profile_android = reinterpret_cast<ProfileAndroid*>(
39 Java_Profile_getNativePointer(AttachCurrentThread(), obj));
40 if (!profile_android)
41 return NULL;
42 return profile_android->profile_;
45 // static
46 bool ProfileAndroid::RegisterProfileAndroid(JNIEnv* env) {
47 return RegisterNativesImpl(env);
50 // static
51 ScopedJavaLocalRef<jobject> ProfileAndroid::GetLastUsedProfile(JNIEnv* env,
52 jclass clazz) {
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());
86 DCHECK(otr_profile);
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();
98 // static
99 ScopedJavaLocalRef<jobject> GetLastUsedProfile(
100 JNIEnv* env,
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_);