Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / profile_android.cc
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.
4
5 #include "chrome/browser/profiles/profile_android.h"
6
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"
12
13 using base::android::AttachCurrentThread;
14
15 namespace {
16 const char kProfileAndroidKey[] = "profile_android";
17 }  // namespace
18
19 // static
20 ProfileAndroid* ProfileAndroid::FromProfile(Profile* profile) {
21   if (!profile)
22     return NULL;
23
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);
29   }
30   return profile_android;
31 }
32
33 // static
34 Profile* ProfileAndroid::FromProfileAndroid(jobject obj) {
35   if (!obj)
36     return NULL;
37
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_;
43 }
44
45 // static
46 bool ProfileAndroid::RegisterProfileAndroid(JNIEnv* env) {
47   return RegisterNativesImpl(env);
48 }
49
50 // static
51 jobject ProfileAndroid::GetLastUsedProfile(JNIEnv* env, jclass clazz) {
52   Profile* profile = ProfileManager::GetLastUsedProfile();
53   if (profile == NULL) {
54     NOTREACHED() << "Profile not found.";
55     return NULL;
56   }
57
58   ProfileAndroid* profile_android = ProfileAndroid::FromProfile(profile);
59   if (profile_android == NULL) {
60     NOTREACHED() << "ProfileAndroid not found.";
61     return NULL;
62   }
63
64   return profile_android->obj_.obj();
65 }
66
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_);
71 }
72
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();
79 }
80
81 base::android::ScopedJavaLocalRef<jobject>
82 ProfileAndroid::GetOffTheRecordProfile(JNIEnv* env, jobject obj) {
83   ProfileAndroid* otr_profile = ProfileAndroid::FromProfile(
84       profile_->GetOffTheRecordProfile());
85   DCHECK(otr_profile);
86   return otr_profile->GetJavaObject();
87 }
88
89 jboolean ProfileAndroid::HasOffTheRecordProfile(JNIEnv* env, jobject obj) {
90   return profile_->HasOffTheRecordProfile();
91 }
92
93 jboolean ProfileAndroid::IsOffTheRecord(JNIEnv* env, jobject obj) {
94   return profile_->IsOffTheRecord();
95 }
96
97 // static
98 jobject GetLastUsedProfile(JNIEnv* env, jclass clazz) {
99   return ProfileAndroid::GetLastUsedProfile(env, clazz);
100 }
101
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());
108 }
109
110 ProfileAndroid::~ProfileAndroid() {
111   Java_Profile_onNativeDestroyed(AttachCurrentThread(), obj_.obj());
112 }
113
114 base::android::ScopedJavaLocalRef<jobject> ProfileAndroid::GetJavaObject() {
115   return base::android::ScopedJavaLocalRef<jobject>(obj_);
116 }