OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/android/precache/precache_launcher.h" | 5 #include "chrome/browser/android/precache/precache_launcher.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_weak_ref.h" | 10 #include "base/android/jni_weak_ref.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/history/history_service_factory.h" | 15 #include "chrome/browser/history/history_service_factory.h" |
16 #include "chrome/browser/precache/precache_manager_factory.h" | |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/profiles/profile_manager.h" | 18 #include "chrome/browser/profiles/profile_manager.h" |
18 #include "chrome/browser/sync/profile_sync_service.h" | |
19 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
20 #include "components/keyed_service/core/service_access_type.h" | 19 #include "components/keyed_service/core/service_access_type.h" |
21 #include "components/precache/content/precache_manager.h" | 20 #include "components/precache/content/precache_manager.h" |
22 #include "components/precache/content/precache_manager_factory.h" | |
23 #include "jni/PrecacheLauncher_jni.h" | 21 #include "jni/PrecacheLauncher_jni.h" |
24 | 22 |
25 using base::android::AttachCurrentThread; | 23 using base::android::AttachCurrentThread; |
26 using precache::PrecacheManager; | 24 using precache::PrecacheManager; |
27 | 25 |
28 namespace history { | 26 namespace history { |
29 class HistoryService; | 27 class HistoryService; |
30 } | 28 } |
31 | 29 |
32 namespace { | 30 namespace { |
33 | 31 |
34 // Get the profile that should be used for precaching. | 32 // Get the profile that should be used for precaching. |
35 Profile* GetProfile() { | 33 Profile* GetProfile() { |
36 Profile* profile = g_browser_process->profile_manager()->GetLastUsedProfile() | 34 Profile* profile = g_browser_process->profile_manager()->GetLastUsedProfile() |
37 ->GetOriginalProfile(); | 35 ->GetOriginalProfile(); |
38 DCHECK(profile); | 36 DCHECK(profile); |
39 DCHECK(g_browser_process->profile_manager()->IsValidProfile(profile)); | 37 DCHECK(g_browser_process->profile_manager()->IsValidProfile(profile)); |
40 return profile; | 38 return profile; |
41 } | 39 } |
42 | 40 |
43 // Get the PrecacheManager for the given |profile|. | 41 // Get the PrecacheManager for the given |profile|. |
44 PrecacheManager* GetPrecacheManager(Profile* profile) { | 42 PrecacheManager* GetPrecacheManager(Profile* profile) { |
45 PrecacheManager* precache_manager = | 43 PrecacheManager* precache_manager = |
46 precache::PrecacheManagerFactory::GetForBrowserContext(profile); | 44 precache::PrecacheManagerFactory::GetForBrowserContext(profile); |
47 DCHECK(precache_manager); | 45 DCHECK(precache_manager); |
bengr
2015/06/23 22:16:19
I'm 99.999% sure this will never be called from in
twifkak
2015/06/23 23:54:51
Okay, added a null check at line 68.
| |
48 return precache_manager; | 46 return precache_manager; |
49 } | 47 } |
50 | 48 |
51 bool IsSyncTabsEnabled(Profile* profile) { | |
52 ProfileSyncService* sync = ProfileSyncServiceFactory::GetForProfile(profile); | |
53 return sync->CanSyncStart() && | |
54 sync->GetPreferredDataTypes().Has(syncer::PROXY_TABS); | |
55 } | |
56 | |
57 } // namespace | 49 } // namespace |
58 | 50 |
59 PrecacheLauncher::PrecacheLauncher(JNIEnv* env, jobject obj) | 51 PrecacheLauncher::PrecacheLauncher(JNIEnv* env, jobject obj) |
60 : weak_java_precache_launcher_(env, obj), weak_factory_(this) {} | 52 : weak_java_precache_launcher_(env, obj), weak_factory_(this) {} |
61 | 53 |
62 PrecacheLauncher::~PrecacheLauncher() {} | 54 PrecacheLauncher::~PrecacheLauncher() {} |
63 | 55 |
64 void PrecacheLauncher::Destroy(JNIEnv* env, jobject obj) { | 56 void PrecacheLauncher::Destroy(JNIEnv* env, jobject obj) { |
65 delete this; | 57 delete this; |
66 } | 58 } |
67 | 59 |
68 void PrecacheLauncher::Start(JNIEnv* env, jobject obj) { | 60 void PrecacheLauncher::Start(JNIEnv* env, jobject obj) { |
69 // TODO(bengr): Add integration tests for the whole feature. | 61 // TODO(bengr): Add integration tests for the whole feature. |
70 Profile* profile = GetProfile(); | 62 Profile* profile = GetProfile(); |
71 PrecacheManager* precache_manager = GetPrecacheManager(profile); | 63 PrecacheManager* precache_manager = GetPrecacheManager(profile); |
72 | 64 |
73 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( | 65 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( |
74 profile, ServiceAccessType::IMPLICIT_ACCESS); | 66 profile, ServiceAccessType::IMPLICIT_ACCESS); |
75 | 67 |
76 if (hs == nullptr || !IsSyncTabsEnabled(profile)) { | 68 if (hs == nullptr || !precache_manager->IsPrecachingAllowed()) { |
77 Java_PrecacheLauncher_onPrecacheCompletedCallback( | 69 Java_PrecacheLauncher_onPrecacheCompletedCallback( |
78 env, weak_java_precache_launcher_.get(env).obj()); | 70 env, weak_java_precache_launcher_.get(env).obj()); |
79 return; | 71 return; |
80 } | 72 } |
81 | 73 |
82 precache_manager->StartPrecaching( | 74 precache_manager->StartPrecaching( |
83 base::Bind(&PrecacheLauncher::OnPrecacheCompleted, | 75 base::Bind(&PrecacheLauncher::OnPrecacheCompleted, |
84 weak_factory_.GetWeakPtr()), | 76 weak_factory_.GetWeakPtr()), |
85 *hs); | 77 *hs); |
86 } | 78 } |
(...skipping 15 matching lines...) Expand all Loading... | |
102 return reinterpret_cast<intptr_t>(new PrecacheLauncher(env, obj)); | 94 return reinterpret_cast<intptr_t>(new PrecacheLauncher(env, obj)); |
103 } | 95 } |
104 | 96 |
105 static jboolean IsPrecachingEnabled(JNIEnv* env, jclass clazz) { | 97 static jboolean IsPrecachingEnabled(JNIEnv* env, jclass clazz) { |
106 return PrecacheManager::IsPrecachingEnabled(); | 98 return PrecacheManager::IsPrecachingEnabled(); |
107 } | 99 } |
108 | 100 |
109 bool RegisterPrecacheLauncher(JNIEnv* env) { | 101 bool RegisterPrecacheLauncher(JNIEnv* env) { |
110 return RegisterNativesImpl(env); | 102 return RegisterNativesImpl(env); |
111 } | 103 } |
OLD | NEW |