OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/profiles/profile_android.h" | 5 #include "chrome/browser/profiles/profile_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" |
| 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/prerender/prerender_manager.h" |
| 11 #include "chrome/browser/prerender/prerender_manager_factory.h" |
8 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/profiles/profile_manager.h" |
9 #include "jni/Profile_jni.h" | 14 #include "jni/Profile_jni.h" |
10 | 15 |
11 using base::android::AttachCurrentThread; | 16 using base::android::AttachCurrentThread; |
12 | 17 |
13 namespace { | 18 namespace { |
14 const char kProfileAndroidKey[] = "profile_android"; | 19 const char kProfileAndroidKey[] = "profile_android"; |
15 } // namespace | 20 } // namespace |
16 | 21 |
17 // static | 22 // static |
18 ProfileAndroid* ProfileAndroid::FromProfile(Profile* profile) { | 23 ProfileAndroid* ProfileAndroid::FromProfile(Profile* profile) { |
19 if (!profile) | 24 if (!profile) |
20 return NULL; | 25 return NULL; |
21 | 26 |
22 ProfileAndroid* profile_android = static_cast<ProfileAndroid*>( | 27 ProfileAndroid* profile_android = static_cast<ProfileAndroid*>( |
23 profile->GetUserData(kProfileAndroidKey)); | 28 profile->GetUserData(kProfileAndroidKey)); |
24 if (!profile_android) { | 29 if (!profile_android) { |
25 profile_android = new ProfileAndroid(profile); | 30 profile_android = new ProfileAndroid(profile); |
26 profile->SetUserData(kProfileAndroidKey, profile_android); | 31 profile->SetUserData(kProfileAndroidKey, profile_android); |
27 } | 32 } |
28 return profile_android; | 33 return profile_android; |
29 } | 34 } |
30 | 35 |
31 // static | 36 // static |
| 37 static jint GetDefaultProfile(JNIEnv* env, jclass clazz) { |
| 38 ProfileAndroid* default_profile = ProfileAndroid::FromProfile( |
| 39 g_browser_process->profile_manager()->GetDefaultProfile()); |
| 40 return reinterpret_cast<jint>(default_profile); |
| 41 } |
| 42 |
| 43 // static |
32 Profile* ProfileAndroid::FromProfileAndroid(jobject obj) { | 44 Profile* ProfileAndroid::FromProfileAndroid(jobject obj) { |
33 if (!obj) | 45 if (!obj) |
34 return NULL; | 46 return NULL; |
35 | 47 |
36 ProfileAndroid* profile_android = reinterpret_cast<ProfileAndroid*>( | 48 ProfileAndroid* profile_android = reinterpret_cast<ProfileAndroid*>( |
37 Java_Profile_getNativePointer(AttachCurrentThread(), obj)); | 49 Java_Profile_getNativePointer(AttachCurrentThread(), obj)); |
38 if (!profile_android) | 50 if (!profile_android) |
39 return NULL; | 51 return NULL; |
40 return profile_android->profile_; | 52 return profile_android->profile_; |
41 } | 53 } |
42 | 54 |
| 55 bool ProfileAndroid::HasPrerenderedUrl(JNIEnv* env, jobject obj, jstring url) { |
| 56 GURL gurl(base::android::ConvertJavaStringToUTF8(env, url)); |
| 57 prerender::PrerenderManager* prerender_manager = |
| 58 prerender::PrerenderManagerFactory::GetForProfile(profile_); |
| 59 if (!prerender_manager) return false; |
| 60 |
| 61 std::vector<content::WebContents*> contents = |
| 62 prerender_manager->GetAllPrerenderingContents(); |
| 63 prerender::PrerenderContents* prerender_contents; |
| 64 for (size_t i = 0; i < contents.size(); ++i) { |
| 65 prerender_contents = prerender_manager-> |
| 66 GetPrerenderContents(contents.at(i)); |
| 67 if (prerender_contents->prerender_url() == |
| 68 gurl && prerender_contents->has_finished_loading()) { |
| 69 return true; |
| 70 } |
| 71 } |
| 72 return false; |
| 73 } |
| 74 |
43 // static | 75 // static |
44 bool ProfileAndroid::RegisterProfileAndroid(JNIEnv* env) { | 76 bool ProfileAndroid::RegisterProfileAndroid(JNIEnv* env) { |
45 return RegisterNativesImpl(env); | 77 return RegisterNativesImpl(env); |
46 } | 78 } |
47 | 79 |
48 ProfileAndroid::ProfileAndroid(Profile* profile) | 80 ProfileAndroid::ProfileAndroid(Profile* profile) |
49 : profile_(profile) { | 81 : profile_(profile) { |
50 JNIEnv* env = AttachCurrentThread(); | 82 JNIEnv* env = AttachCurrentThread(); |
51 base::android::ScopedJavaLocalRef<jobject> jprofile = | 83 base::android::ScopedJavaLocalRef<jobject> jprofile = |
52 Java_Profile_create(env, reinterpret_cast<int>(this)); | 84 Java_Profile_create(env, reinterpret_cast<int>(this)); |
53 obj_.Reset(env, jprofile.obj()); | 85 obj_.Reset(env, jprofile.obj()); |
54 | 86 |
55 } | 87 } |
56 | 88 |
57 ProfileAndroid::~ProfileAndroid() { | 89 ProfileAndroid::~ProfileAndroid() { |
58 Java_Profile_destroy(AttachCurrentThread(), obj_.obj()); | 90 Java_Profile_destroy(AttachCurrentThread(), obj_.obj()); |
59 } | 91 } |
60 | 92 |
61 base::android::ScopedJavaLocalRef<jobject> ProfileAndroid::GetJavaObject() { | 93 base::android::ScopedJavaLocalRef<jobject> ProfileAndroid::GetJavaObject() { |
62 return base::android::ScopedJavaLocalRef<jobject>(obj_); | 94 return base::android::ScopedJavaLocalRef<jobject>(obj_); |
63 } | 95 } |
OLD | NEW |