Home | History | Annotate | Download | only in profiles
      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_manager.h"
     10 #include "jni/Profile_jni.h"
     11 
     12 using base::android::AttachCurrentThread;
     13 
     14 namespace {
     15 const char kProfileAndroidKey[] = "profile_android";
     16 }  // namespace
     17 
     18 // static
     19 ProfileAndroid* ProfileAndroid::FromProfile(Profile* profile) {
     20   if (!profile)
     21     return NULL;
     22 
     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);
     28   }
     29   return profile_android;
     30 }
     31 
     32 // static
     33 Profile* ProfileAndroid::FromProfileAndroid(jobject obj) {
     34   if (!obj)
     35     return NULL;
     36 
     37   ProfileAndroid* profile_android = reinterpret_cast<ProfileAndroid*>(
     38       Java_Profile_getNativePointer(AttachCurrentThread(), obj));
     39   if (!profile_android)
     40     return NULL;
     41   return profile_android->profile_;
     42 }
     43 
     44 // static
     45 bool ProfileAndroid::RegisterProfileAndroid(JNIEnv* env) {
     46   return RegisterNativesImpl(env);
     47 }
     48 
     49 // static
     50 jobject ProfileAndroid::GetLastUsedProfile(JNIEnv* env, jclass clazz) {
     51   Profile* profile = ProfileManager::GetLastUsedProfile();
     52   if (profile == NULL) {
     53     NOTREACHED() << "Profile not found.";
     54     return NULL;
     55   }
     56 
     57   ProfileAndroid* profile_android = ProfileAndroid::FromProfile(profile);
     58   if (profile_android == NULL) {
     59     NOTREACHED() << "ProfileAndroid not found.";
     60     return NULL;
     61   }
     62 
     63   return profile_android->obj_.obj();
     64 }
     65 
     66 // static
     67 jobject GetLastUsedProfile(JNIEnv* env, jclass clazz) {
     68   return ProfileAndroid::GetLastUsedProfile(env, clazz);
     69 }
     70 
     71 ProfileAndroid::ProfileAndroid(Profile* profile)
     72     : profile_(profile) {
     73   JNIEnv* env = AttachCurrentThread();
     74   base::android::ScopedJavaLocalRef<jobject> jprofile =
     75       Java_Profile_create(env, reinterpret_cast<intptr_t>(this));
     76   obj_.Reset(env, jprofile.obj());
     77 
     78 }
     79 
     80 ProfileAndroid::~ProfileAndroid() {
     81   Java_Profile_destroy(AttachCurrentThread(), obj_.obj());
     82 }
     83 
     84 base::android::ScopedJavaLocalRef<jobject> ProfileAndroid::GetJavaObject() {
     85   return base::android::ScopedJavaLocalRef<jobject>(obj_);
     86 }
     87