File: webapk_sync_service_jni.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (129 lines) | stat: -rw-r--r-- 4,306 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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <utility>

#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "chrome/browser/android/webapk/webapk_sync_service.h"
#include "chrome/browser/android/webapk/webapk_sync_service_factory.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/sync/base/features.h"
#include "components/sync/protocol/web_apk_specifics.pb.h"
#include "ui/gfx/android/java_bitmap.h"

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

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

namespace webapk {

namespace {

// Called after getting restorable app info.
void OnGotAppsInfo(const JavaRef<jobject>& java_callback,
                   const std::vector<std::string>& app_ids,
                   const std::vector<std::u16string>& names,
                   const std::vector<int>& last_used_in_days,
                   const std::vector<SkBitmap>& icons) {
  JNIEnv* env = base::android::AttachCurrentThread();
  ScopedJavaLocalRef<jobject> jbitmaps =
      Java_WebApkSyncService_createBitmapList(env);
  for (const SkBitmap& bitmap : icons) {
    ScopedJavaLocalRef<jobject> jbitmap = gfx::ConvertToJavaBitmap(bitmap);
    Java_WebApkSyncService_addToBitmapList(env, jbitmaps, jbitmap);
  }
  Java_PwaRestorableListCallback_onRestorableAppsAvailable(
      env, java_callback, true,
      base::android::ToJavaArrayOfStrings(env, app_ids),
      base::android::ToJavaArrayOfStrings(env, names),
      base::android::ToJavaIntArray(env, last_used_in_days), jbitmaps);
}

}  // namespace

static void JNI_WebApkSyncService_OnWebApkUsed(
    JNIEnv* env,
    const JavaParamRef<jbyteArray>& java_webapk_specifics,
    jboolean is_install) {
  if (!base::FeatureList::IsEnabled(syncer::kWebApkBackupAndRestoreBackend)) {
    return;
  }

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

  std::string specifics_bytes;
  base::android::JavaByteArrayToString(env, java_webapk_specifics,
                                       &specifics_bytes);

  std::unique_ptr<sync_pb::WebApkSpecifics> specifics =
      std::make_unique<sync_pb::WebApkSpecifics>();
  if (!specifics->ParseFromString(specifics_bytes)) {
    LOG(ERROR) << "failed to parse WebApkSpecifics proto";
    return;
  }
  WebApkSyncServiceFactory::GetForProfile(profile)->OnWebApkUsed(
      std::move(specifics), static_cast<bool>(is_install));
}

static void JNI_WebApkSyncService_OnWebApkUninstalled(
    JNIEnv* env,
    std::string& java_manifest_id) {
  if (!base::FeatureList::IsEnabled(syncer::kWebApkBackupAndRestoreBackend)) {
    return;
  }

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

  WebApkSyncServiceFactory::GetForProfile(profile)->OnWebApkUninstalled(
      java_manifest_id);
}

static void JNI_WebApkSyncService_RemoveOldWebAPKsFromSync(
    JNIEnv* env,
    jlong java_current_time_ms_since_unix_epoch) {
  if (!base::FeatureList::IsEnabled(syncer::kWebApkBackupAndRestoreBackend)) {
    return;
  }

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

  WebApkSyncServiceFactory::GetForProfile(profile)->RemoveOldWebAPKsFromSync(
      static_cast<int64_t>(java_current_time_ms_since_unix_epoch));
}

static void JNI_WebApkSyncService_FetchRestorableApps(
    JNIEnv* env,
    Profile* profile,
    const JavaParamRef<jobject>& java_callback) {
  if (profile == nullptr ||
      !base::FeatureList::IsEnabled(syncer::kWebApkBackupAndRestoreBackend)) {
    return;
  }

  ScopedJavaGlobalRef<jobject> callback_ref(java_callback);
  WebApkSyncServiceFactory::GetForProfile(profile)->PrepareRestorableAppsInfo(
      base::BindOnce(&OnGotAppsInfo, callback_ref));
}

}  // namespace webapk