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 "jni/Profile_jni.h"
     10 
     11 using base::android::AttachCurrentThread;
     12 
     13 namespace {
     14 const char kProfileAndroidKey[] = "profile_android";
     15 }  // namespace
     16 
     17 // static
     18 ProfileAndroid* ProfileAndroid::FromProfile(Profile* profile) {
     19   if (!profile)
     20     return NULL;
     21 
     22   ProfileAndroid* profile_android = static_cast<ProfileAndroid*>(
     23       profile->GetUserData(kProfileAndroidKey));
     24   if (!profile_android) {
     25     profile_android = new ProfileAndroid(profile);
     26     profile->SetUserData(kProfileAndroidKey, profile_android);
     27   }
     28   return profile_android;
     29 }
     30 
     31 // static
     32 Profile* ProfileAndroid::FromProfileAndroid(jobject obj) {
     33   if (!obj)
     34     return NULL;
     35 
     36   ProfileAndroid* profile_android = reinterpret_cast<ProfileAndroid*>(
     37       Java_Profile_getNativePointer(AttachCurrentThread(), obj));
     38   if (!profile_android)
     39     return NULL;
     40   return profile_android->profile_;
     41 }
     42 
     43 // static
     44 bool ProfileAndroid::RegisterProfileAndroid(JNIEnv* env) {
     45   return RegisterNativesImpl(env);
     46 }
     47 
     48 ProfileAndroid::ProfileAndroid(Profile* profile)
     49     : profile_(profile) {
     50   JNIEnv* env = AttachCurrentThread();
     51   base::android::ScopedJavaLocalRef<jobject> jprofile =
     52       Java_Profile_create(env, reinterpret_cast<int>(this));
     53   obj_.Reset(env, jprofile.obj());
     54 
     55 }
     56 
     57 ProfileAndroid::~ProfileAndroid() {
     58   Java_Profile_destroy(AttachCurrentThread(), obj_.obj());
     59 }
     60 
     61 base::android::ScopedJavaLocalRef<jobject> ProfileAndroid::GetJavaObject() {
     62   return base::android::ScopedJavaLocalRef<jobject>(obj_);
     63 }
     64