File: background_scheduler_bridge.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (142 lines) | stat: -rw-r--r-- 5,754 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/offline_pages/android/background_scheduler_bridge.h"

#include "base/android/callback_android.h"
#include "base/android/scoped_java_ref.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "chrome/browser/offline_pages/android/offline_page_auto_fetcher_service_factory.h"
#include "chrome/browser/offline_pages/offline_page_model_factory.h"
#include "chrome/browser/offline_pages/request_coordinator_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/offline_pages/core/background/device_conditions.h"
#include "components/offline_pages/core/background/offliner.h"
#include "components/offline_pages/core/background/request_coordinator.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/android/chrome_jni_headers/BackgroundSchedulerBridge_jni.h"

using base::android::JavaParamRef;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;

namespace offline_pages {
namespace android {

// JNI call to start request processing in scheduled mode.
static jboolean JNI_BackgroundSchedulerBridge_StartScheduledProcessing(
    JNIEnv* env,
    const jboolean j_power_connected,
    const jint j_battery_percentage,
    const jint j_net_connection_type,
    const JavaParamRef<jobject>& j_callback_obj) {
  ScopedJavaGlobalRef<jobject> j_callback_ref;
  j_callback_ref.Reset(env, j_callback_obj);

  Profile* profile = ProfileManager::GetLastUsedProfile();
  if (!profile) {
    return false;
  }

  // Make sure the auto-fetch service is running, so it can respond to completed
  // pages.
  OfflinePageAutoFetcherServiceFactory::GetForBrowserContext(profile);

  // Lookup/create RequestCoordinator KeyedService and call
  // StartScheduledProcessing on it with bound j_callback_obj.
  RequestCoordinator* coordinator =
      RequestCoordinatorFactory::GetInstance()->GetForBrowserContext(profile);
  DVLOG(2) << "resource_coordinator: " << coordinator;
  DeviceConditions device_conditions(
      j_power_connected, j_battery_percentage,
      static_cast<net::NetworkChangeNotifier::ConnectionType>(
          j_net_connection_type));
  return coordinator->StartScheduledProcessing(
      device_conditions,
      base::BindRepeating(&base::android::RunBooleanCallbackAndroid,
                          j_callback_ref));
}

// JNI call to stop request processing in scheduled mode.
static void JNI_BackgroundSchedulerBridge_StopScheduledProcessing(JNIEnv* env) {
  Profile* profile = ProfileManager::GetLastUsedProfile();
  if (!profile) {
    return;
  }
  RequestCoordinator* coordinator =
      RequestCoordinatorFactory::GetInstance()->GetForBrowserContext(profile);
  DVLOG(2) << "resource_coordinator: " << coordinator;
  if (!coordinator) {
    return;
  }
  coordinator->CancelProcessing();
}

BackgroundSchedulerBridge::BackgroundSchedulerBridge() = default;

BackgroundSchedulerBridge::~BackgroundSchedulerBridge() = default;

void BackgroundSchedulerBridge::Schedule(
    const TriggerConditions& trigger_conditions) {
  JNIEnv* env = base::android::AttachCurrentThread();
  ScopedJavaLocalRef<jobject> j_conditions =
      CreateTriggerConditions(env, trigger_conditions.require_power_connected,
                              trigger_conditions.minimum_battery_percentage,
                              trigger_conditions.require_unmetered_network);
  Java_BackgroundSchedulerBridge_schedule(env, j_conditions);
}

void BackgroundSchedulerBridge::BackupSchedule(
    const TriggerConditions& trigger_conditions,
    int64_t delay_in_seconds) {
  JNIEnv* env = base::android::AttachCurrentThread();
  ScopedJavaLocalRef<jobject> j_conditions =
      CreateTriggerConditions(env, trigger_conditions.require_power_connected,
                              trigger_conditions.minimum_battery_percentage,
                              trigger_conditions.require_unmetered_network);
  Java_BackgroundSchedulerBridge_backupSchedule(env, j_conditions,
                                                delay_in_seconds);
}

void BackgroundSchedulerBridge::Unschedule() {
  JNIEnv* env = base::android::AttachCurrentThread();
  Java_BackgroundSchedulerBridge_unschedule(env);
}

ScopedJavaLocalRef<jobject> BackgroundSchedulerBridge::CreateTriggerConditions(
    JNIEnv* env,
    bool require_power_connected,
    int minimum_battery_percentage,
    bool require_unmetered_network) const {
  return Java_BackgroundSchedulerBridge_createTriggerConditions(
      env, require_power_connected, minimum_battery_percentage,
      require_unmetered_network);
}

const DeviceConditions&
BackgroundSchedulerBridge::GetCurrentDeviceConditions() {
  JNIEnv* env = base::android::AttachCurrentThread();
  // Call the JNI methods to get the device conditions we need.
  jboolean jpower = Java_BackgroundSchedulerBridge_getPowerConditions(env);
  jint jbattery = Java_BackgroundSchedulerBridge_getBatteryConditions(env);
  jint jnetwork = Java_BackgroundSchedulerBridge_getNetworkConditions(env);

  // Cast the java types back to the types we use.
  bool power = static_cast<bool>(jpower);
  int battery = static_cast<int>(jbattery);

  net::NetworkChangeNotifier::ConnectionType network_connection_type =
      static_cast<net::NetworkChangeNotifier::ConnectionType>(jnetwork);

  // Now return the current conditions to the caller.
  device_conditions_ = std::make_unique<DeviceConditions>(
      power, battery, network_connection_type);
  return *device_conditions_;
}

}  // namespace android
}  // namespace offline_pages