jkarlin | db18522 | 2015-05-29 12:02:00 | [diff] [blame] | 1 | // Copyright 2015 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 | |
iclelland | 8467f46 | 2015-10-21 14:03:13 | [diff] [blame] | 5 | #include "chrome/browser/android/background_sync_launcher_android.h" |
jkarlin | db18522 | 2015-05-29 12:02:00 | [diff] [blame] | 6 | |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
| 9 | #include "base/android/callback_android.h" |
| 10 | #include "base/barrier_closure.h" |
Mugdha Lakhani | eef7642 | 2019-03-22 18:04:49 | [diff] [blame] | 11 | #include "base/bind.h" |
Mugdha Lakhani | fe82692b | 2019-02-08 13:08:40 | [diff] [blame] | 12 | #include "base/feature_list.h" |
Andrew Grieve | 4a42c22e | 2019-06-24 16:14:29 | [diff] [blame] | 13 | #include "chrome/android/chrome_jni_headers/BackgroundSyncBackgroundTaskScheduler_jni.h" |
| 14 | #include "chrome/android/chrome_jni_headers/BackgroundSyncBackgroundTask_jni.h" |
Mugdha Lakhani | ec9c3ae7 | 2019-09-20 09:40:34 | [diff] [blame] | 15 | #include "chrome/android/chrome_jni_headers/GooglePlayServicesChecker_jni.h" |
Mugdha Lakhani | eab4b18 | 2019-06-25 10:52:16 | [diff] [blame] | 16 | #include "chrome/android/chrome_jni_headers/PeriodicBackgroundSyncChromeWakeUpTask_jni.h" |
Henrique Nakashima | 476b0b5 | 2020-01-24 22:28:03 | [diff] [blame] | 17 | #include "chrome/browser/flags/android/chrome_feature_list.h" |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 18 | #include "chrome/browser/profiles/profile_manager.h" |
| 19 | #include "content/public/browser/background_sync_context.h" |
Mugdha Lakhani | e07dd9e | 2019-07-18 17:25:23 | [diff] [blame] | 20 | #include "content/public/browser/background_sync_parameters.h" |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 21 | #include "content/public/browser/browser_context.h" |
jkarlin | db18522 | 2015-05-29 12:02:00 | [diff] [blame] | 22 | #include "content/public/browser/browser_thread.h" |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 23 | #include "content/public/browser/storage_partition.h" |
jkarlin | db18522 | 2015-05-29 12:02:00 | [diff] [blame] | 24 | |
iclelland | 8467f46 | 2015-10-21 14:03:13 | [diff] [blame] | 25 | using content::BrowserThread; |
jkarlin | db18522 | 2015-05-29 12:02:00 | [diff] [blame] | 26 | |
| 27 | namespace { |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 28 | base::LazyInstance<BackgroundSyncLauncherAndroid>::DestructorAtExit |
| 29 | g_background_sync_launcher = LAZY_INSTANCE_INITIALIZER; |
iclelland | d054a6f | 2016-01-11 20:16:14 | [diff] [blame] | 30 | |
| 31 | // Disables the Play Services version check for testing on Chromium build bots. |
| 32 | // TODO(iclelland): Remove this once the bots have their play services package |
| 33 | // updated before every test run. (https://crbug.com/514449) |
| 34 | bool disable_play_services_version_check_for_tests = false; |
| 35 | |
Mugdha Lakhani | e07dd9e | 2019-07-18 17:25:23 | [diff] [blame] | 36 | // Returns 0 to create a ONE_SHOT_SYNC_CHROME_WAKE_UP task, or 1 to create a |
| 37 | // PERIODIC_SYNC_CHROME_WAKE_UP task, based on |sync_type|. |
| 38 | int GetBackgroundTaskType(blink::mojom::BackgroundSyncType sync_type) { |
| 39 | return static_cast<int>(sync_type); |
| 40 | } |
| 41 | |
iclelland | d054a6f | 2016-01-11 20:16:14 | [diff] [blame] | 42 | } // namespace |
jkarlin | db18522 | 2015-05-29 12:02:00 | [diff] [blame] | 43 | |
| 44 | // static |
Mugdha Lakhani | eab4b18 | 2019-06-25 10:52:16 | [diff] [blame] | 45 | void JNI_BackgroundSyncBackgroundTask_FireOneShotBackgroundSyncEvents( |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 46 | JNIEnv* env, |
| 47 | const base::android::JavaParamRef<jobject>& j_runnable) { |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 48 | |
Mugdha Lakhani | eab4b18 | 2019-06-25 10:52:16 | [diff] [blame] | 49 | BackgroundSyncLauncherAndroid::Get()->FireBackgroundSyncEvents( |
| 50 | blink::mojom::BackgroundSyncType::ONE_SHOT, j_runnable); |
| 51 | } |
| 52 | |
| 53 | void JNI_PeriodicBackgroundSyncChromeWakeUpTask_FirePeriodicBackgroundSyncEvents( |
| 54 | JNIEnv* env, |
| 55 | const base::android::JavaParamRef<jobject>& j_runnable) { |
| 56 | BackgroundSyncLauncherAndroid::Get()->FireBackgroundSyncEvents( |
| 57 | blink::mojom::BackgroundSyncType::PERIODIC, j_runnable); |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 58 | } |
| 59 | |
Mugdha Lakhani | b7d218f0 | 2019-09-09 14:48:23 | [diff] [blame] | 60 | void JNI_BackgroundSyncBackgroundTaskScheduler_SetPlayServicesVersionCheckDisabledForTests( |
| 61 | JNIEnv* env, |
| 62 | jboolean disabled) { |
| 63 | BackgroundSyncLauncherAndroid::SetPlayServicesVersionCheckDisabledForTests( |
| 64 | disabled); |
| 65 | } |
| 66 | |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 67 | // static |
jkarlin | db18522 | 2015-05-29 12:02:00 | [diff] [blame] | 68 | BackgroundSyncLauncherAndroid* BackgroundSyncLauncherAndroid::Get() { |
| 69 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 70 | |
| 71 | return g_background_sync_launcher.Pointer(); |
| 72 | } |
| 73 | |
| 74 | // static |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 75 | void BackgroundSyncLauncherAndroid::SetPlayServicesVersionCheckDisabledForTests( |
| 76 | bool disabled) { |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 77 | disable_play_services_version_check_for_tests = disabled; |
| 78 | } |
| 79 | |
| 80 | // static |
Mugdha Lakhani | eb39fc23 | 2019-11-14 14:54:25 | [diff] [blame] | 81 | void BackgroundSyncLauncherAndroid::ScheduleBrowserWakeUpWithDelay( |
| 82 | blink::mojom::BackgroundSyncType sync_type, |
| 83 | base::TimeDelta delay) { |
| 84 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 85 | |
| 86 | Get()->ScheduleBrowserWakeUpWithDelayImpl(sync_type, delay); |
| 87 | } |
| 88 | |
| 89 | // static |
| 90 | void BackgroundSyncLauncherAndroid::CancelBrowserWakeup( |
| 91 | blink::mojom::BackgroundSyncType sync_type) { |
| 92 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 93 | Get()->CancelBrowserWakeupImpl(sync_type); |
| 94 | } |
| 95 | |
| 96 | // static |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 97 | bool BackgroundSyncLauncherAndroid::ShouldDisableBackgroundSync() { |
| 98 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 99 | if (disable_play_services_version_check_for_tests) |
| 100 | return false; |
Mugdha Lakhani | ec9c3ae7 | 2019-09-20 09:40:34 | [diff] [blame] | 101 | return Java_GooglePlayServicesChecker_shouldDisableBackgroundSync( |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 102 | base::android::AttachCurrentThread()); |
| 103 | } |
| 104 | |
Mugdha Lakhani | eb39fc23 | 2019-11-14 14:54:25 | [diff] [blame] | 105 | void BackgroundSyncLauncherAndroid::ScheduleBrowserWakeUpWithDelayImpl( |
Mugdha Lakhani | e07dd9e | 2019-07-18 17:25:23 | [diff] [blame] | 106 | blink::mojom::BackgroundSyncType sync_type, |
Mugdha Lakhani | eef7642 | 2019-03-22 18:04:49 | [diff] [blame] | 107 | base::TimeDelta soonest_wakeup_delta) { |
jkarlin | db18522 | 2015-05-29 12:02:00 | [diff] [blame] | 108 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 109 | |
jkarlin | fe08f13 | 2015-11-06 13:18:41 | [diff] [blame] | 110 | JNIEnv* env = base::android::AttachCurrentThread(); |
Mugdha Lakhani | eef7642 | 2019-03-22 18:04:49 | [diff] [blame] | 111 | int64_t min_delay_ms = soonest_wakeup_delta.InMilliseconds(); |
Mugdha Lakhani | fe82692b | 2019-02-08 13:08:40 | [diff] [blame] | 112 | |
Mugdha Lakhani | bc994fa | 2019-11-14 16:20:29 | [diff] [blame] | 113 | Java_BackgroundSyncBackgroundTaskScheduler_scheduleOneOffTask( |
Mugdha Lakhani | fe82692b | 2019-02-08 13:08:40 | [diff] [blame] | 114 | env, java_background_sync_background_task_scheduler_launcher_, |
Mugdha Lakhani | bc994fa | 2019-11-14 16:20:29 | [diff] [blame] | 115 | GetBackgroundTaskType(sync_type), min_delay_ms); |
jkarlin | db18522 | 2015-05-29 12:02:00 | [diff] [blame] | 116 | } |
| 117 | |
Mugdha Lakhani | eb39fc23 | 2019-11-14 14:54:25 | [diff] [blame] | 118 | void BackgroundSyncLauncherAndroid::CancelBrowserWakeupImpl( |
| 119 | blink::mojom::BackgroundSyncType sync_type) { |
| 120 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 121 | |
| 122 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 123 | |
Mugdha Lakhani | bc994fa | 2019-11-14 16:20:29 | [diff] [blame] | 124 | Java_BackgroundSyncBackgroundTaskScheduler_cancelOneOffTask( |
Mugdha Lakhani | eb39fc23 | 2019-11-14 14:54:25 | [diff] [blame] | 125 | env, java_background_sync_background_task_scheduler_launcher_, |
Mugdha Lakhani | bc994fa | 2019-11-14 16:20:29 | [diff] [blame] | 126 | GetBackgroundTaskType(sync_type)); |
Mugdha Lakhani | eb39fc23 | 2019-11-14 14:54:25 | [diff] [blame] | 127 | } |
| 128 | |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 129 | void BackgroundSyncLauncherAndroid::FireBackgroundSyncEvents( |
Mugdha Lakhani | eab4b18 | 2019-06-25 10:52:16 | [diff] [blame] | 130 | blink::mojom::BackgroundSyncType sync_type, |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 131 | const base::android::JavaParamRef<jobject>& j_runnable) { |
iclelland | d054a6f | 2016-01-11 20:16:14 | [diff] [blame] | 132 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Mugdha Lakhani | 1f1ea8e | 2019-02-27 16:01:32 | [diff] [blame] | 133 | |
| 134 | auto* profile = ProfileManager::GetLastUsedProfile(); |
| 135 | DCHECK(profile); |
| 136 | |
Mugdha Lakhani | eef7642 | 2019-03-22 18:04:49 | [diff] [blame] | 137 | content::BackgroundSyncContext::FireBackgroundSyncEventsAcrossPartitions( |
Mugdha Lakhani | eab4b18 | 2019-06-25 10:52:16 | [diff] [blame] | 138 | profile, sync_type, j_runnable); |
iclelland | d054a6f | 2016-01-11 20:16:14 | [diff] [blame] | 139 | } |
| 140 | |
jkarlin | db18522 | 2015-05-29 12:02:00 | [diff] [blame] | 141 | BackgroundSyncLauncherAndroid::BackgroundSyncLauncherAndroid() { |
| 142 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 143 | |
| 144 | JNIEnv* env = base::android::AttachCurrentThread(); |
Mugdha Lakhani | fe82692b | 2019-02-08 13:08:40 | [diff] [blame] | 145 | |
Mugdha Lakhani | fe82692b | 2019-02-08 13:08:40 | [diff] [blame] | 146 | java_background_sync_background_task_scheduler_launcher_.Reset( |
| 147 | Java_BackgroundSyncBackgroundTaskScheduler_getInstance(env)); |
jkarlin | db18522 | 2015-05-29 12:02:00 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | BackgroundSyncLauncherAndroid::~BackgroundSyncLauncherAndroid() { |
| 151 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
jkarlin | db18522 | 2015-05-29 12:02:00 | [diff] [blame] | 152 | } |