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.
5 #include "chrome/browser/android/precache/precache_launcher.h"
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/precache/precache_manager_factory.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "components/precache/content/precache_manager.h"
19 #include "jni/PrecacheLauncher_jni.h"
21 using base::android::AttachCurrentThread
;
22 using precache::PrecacheManager
;
26 // Get the profile that should be used for precaching.
27 Profile
* GetProfile() {
28 Profile
* profile
= g_browser_process
->profile_manager()->GetLastUsedProfile()
29 ->GetOriginalProfile();
31 DCHECK(g_browser_process
->profile_manager()->IsValidProfile(profile
));
35 // Get the PrecacheManager for the given |profile|.
36 PrecacheManager
* GetPrecacheManager(Profile
* profile
) {
37 PrecacheManager
* precache_manager
=
38 precache::PrecacheManagerFactory::GetForBrowserContext(profile
);
39 DCHECK(precache_manager
);
40 return precache_manager
;
45 PrecacheLauncher::PrecacheLauncher(JNIEnv
* env
, jobject obj
)
46 : weak_java_precache_launcher_(env
, obj
), weak_factory_(this) {}
48 PrecacheLauncher::~PrecacheLauncher() {}
50 void PrecacheLauncher::Destroy(JNIEnv
* env
, jobject obj
) {
54 void PrecacheLauncher::Start(JNIEnv
* env
, jobject obj
) {
55 // TODO(bengr): Add integration tests for the whole feature.
56 Profile
* profile
= GetProfile();
58 PrecacheManager
* precache_manager
= GetPrecacheManager(profile
);
60 if (precache_manager
== nullptr) {
61 OnPrecacheCompleted(false);
65 precache_manager
->StartPrecaching(base::Bind(
66 &PrecacheLauncher::OnPrecacheCompleted
, weak_factory_
.GetWeakPtr()));
69 void PrecacheLauncher::Cancel(JNIEnv
* env
, jobject obj
) {
70 Profile
* profile
= GetProfile();
71 PrecacheManager
* precache_manager
= GetPrecacheManager(profile
);
73 precache_manager
->CancelPrecaching();
76 void PrecacheLauncher::OnPrecacheCompleted(bool try_again_soon
) {
77 JNIEnv
* env
= AttachCurrentThread();
78 Java_PrecacheLauncher_onPrecacheCompletedCallback(
79 env
, weak_java_precache_launcher_
.get(env
).obj(),
80 try_again_soon
? JNI_TRUE
: JNI_FALSE
);
83 static jlong
Init(JNIEnv
* env
, jobject obj
) {
84 return reinterpret_cast<intptr_t>(new PrecacheLauncher(env
, obj
));
87 // Must be run on the UI thread.
88 static jboolean
ShouldRun(JNIEnv
* env
, jobject obj
) {
89 Profile
* profile
= GetProfile();
90 PrecacheManager
* precache_manager
= GetPrecacheManager(profile
);
91 return precache_manager
&& (precache_manager
->IsInExperimentGroup() ||
92 precache_manager
->IsInControlGroup());
95 bool RegisterPrecacheLauncher(JNIEnv
* env
) {
96 return RegisterNativesImpl(env
);