| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/android/precache/precache_launcher.h" | |
| 6 | |
| 7 #include <jni.h> | |
| 8 | |
| 9 #include "base/android/jni_android.h" | |
| 10 #include "base/android/jni_weak_ref.h" | |
| 11 #include "base/bind.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/prefs/pref_service.h" | |
| 14 #include "chrome/browser/browser_process.h" | |
| 15 #include "chrome/browser/history/top_sites_factory.h" | |
| 16 #include "chrome/browser/precache/most_visited_urls_provider.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | |
| 18 #include "chrome/browser/profiles/profile_manager.h" | |
| 19 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_
names.h" | |
| 20 #include "components/history/core/browser/top_sites.h" | |
| 21 #include "components/precache/content/precache_manager.h" | |
| 22 #include "components/precache/content/precache_manager_factory.h" | |
| 23 #include "jni/PrecacheLauncher_jni.h" | |
| 24 | |
| 25 using base::android::AttachCurrentThread; | |
| 26 using precache::PrecacheManager; | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 // Get the profile that should be used for precaching. | |
| 31 Profile* GetProfile() { | |
| 32 Profile* profile = g_browser_process->profile_manager()->GetLastUsedProfile() | |
| 33 ->GetOriginalProfile(); | |
| 34 DCHECK(profile); | |
| 35 DCHECK(g_browser_process->profile_manager()->IsValidProfile(profile)); | |
| 36 return profile; | |
| 37 } | |
| 38 | |
| 39 // Get the PrecacheManager for the given |profile|. | |
| 40 PrecacheManager* GetPrecacheManager(Profile* profile) { | |
| 41 PrecacheManager* precache_manager = | |
| 42 precache::PrecacheManagerFactory::GetForBrowserContext(profile); | |
| 43 DCHECK(precache_manager); | |
| 44 return precache_manager; | |
| 45 } | |
| 46 | |
| 47 bool IsDataReductionProxyEnabled(Profile* profile) { | |
| 48 // TODO(bengr): Use DataReductionProxySettings instead once it is safe to | |
| 49 // instantiate from here. | |
| 50 PrefService* prefs = profile->GetPrefs(); | |
| 51 return prefs && prefs->GetBoolean( | |
| 52 data_reduction_proxy::prefs::kDataReductionProxyEnabled); | |
| 53 } | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 PrecacheLauncher::PrecacheLauncher(JNIEnv* env, jobject obj) | |
| 58 : weak_java_precache_launcher_(env, obj), weak_factory_(this) {} | |
| 59 | |
| 60 PrecacheLauncher::~PrecacheLauncher() {} | |
| 61 | |
| 62 void PrecacheLauncher::Destroy(JNIEnv* env, jobject obj) { | |
| 63 delete this; | |
| 64 } | |
| 65 | |
| 66 void PrecacheLauncher::Start(JNIEnv* env, jobject obj) { | |
| 67 // TODO(bengr): Add integration tests for the whole feature. | |
| 68 Profile* profile = GetProfile(); | |
| 69 PrecacheManager* precache_manager = GetPrecacheManager(profile); | |
| 70 scoped_refptr<history::TopSites> ts = TopSitesFactory::GetForProfile(profile); | |
| 71 precache::MostVisitedURLsProvider url_list_provider(ts.get()); | |
| 72 | |
| 73 if (!IsDataReductionProxyEnabled(profile)) { | |
| 74 Java_PrecacheLauncher_onPrecacheCompletedCallback( | |
| 75 env, weak_java_precache_launcher_.get(env).obj()); | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 precache_manager->StartPrecaching( | |
| 80 base::Bind(&PrecacheLauncher::OnPrecacheCompleted, | |
| 81 weak_factory_.GetWeakPtr()), | |
| 82 &url_list_provider); | |
| 83 } | |
| 84 | |
| 85 void PrecacheLauncher::Cancel(JNIEnv* env, jobject obj) { | |
| 86 Profile* profile = GetProfile(); | |
| 87 PrecacheManager* precache_manager = GetPrecacheManager(profile); | |
| 88 | |
| 89 precache_manager->CancelPrecaching(); | |
| 90 } | |
| 91 | |
| 92 void PrecacheLauncher::OnPrecacheCompleted() { | |
| 93 JNIEnv* env = AttachCurrentThread(); | |
| 94 Java_PrecacheLauncher_onPrecacheCompletedCallback( | |
| 95 env, weak_java_precache_launcher_.get(env).obj()); | |
| 96 } | |
| 97 | |
| 98 static jlong Init(JNIEnv* env, jobject obj) { | |
| 99 return reinterpret_cast<intptr_t>(new PrecacheLauncher(env, obj)); | |
| 100 } | |
| 101 | |
| 102 static jboolean IsPrecachingEnabled(JNIEnv* env, jclass clazz) { | |
| 103 return PrecacheManager::IsPrecachingEnabled(); | |
| 104 } | |
| 105 | |
| 106 bool RegisterPrecacheLauncher(JNIEnv* env) { | |
| 107 return RegisterNativesImpl(env); | |
| 108 } | |
| OLD | NEW |