File: notification_suspender_android.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (136 lines) | stat: -rw-r--r-- 5,240 bytes parent folder | download | duplicates (4)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <vector>

#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/check_op.h"
#include "base/functional/callback_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/platform_notification_context.h"
#include "content/public/browser/storage_partition.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/image/image.h"

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

using base::android::AppendJavaStringArrayToStringVector;
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;
using content::BrowserContext;
using content::NotificationResourceData;
using content::PlatformNotificationContext;
using jni_zero::AttachCurrentThread;

namespace {

SkBitmap ExtractImage(JNIEnv* env,
                      const JavaParamRef<jobjectArray>& j_resources,
                      int index) {
  ScopedJavaLocalRef<jobject> j_image(
      env, env->GetObjectArrayElement(j_resources, index));
  return j_image.is_null()
             ? SkBitmap()
             : CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(j_image));
}

std::vector<blink::NotificationResources> ParseResources(
    JNIEnv* env,
    const JavaParamRef<jobjectArray>& j_resources) {
  // Resources is an array of bitmaps with the following order:
  // [icon, badge, image, icon, badge, image, ...]
  int resource_count = env->GetArrayLength(j_resources);
  DCHECK(resource_count % 3 == 0);

  std::vector<blink::NotificationResources> resources;
  for (int i = 0; i < resource_count; i += 3) {
    blink::NotificationResources res;
    res.notification_icon = ExtractImage(env, j_resources, i + 0);
    res.badge = ExtractImage(env, j_resources, i + 1);
    res.image = ExtractImage(env, j_resources, i + 2);
    resources.emplace_back(std::move(res));
  }
  return resources;
}

PlatformNotificationContext* GetContext(Profile* profile, const GURL& origin) {
  auto* partition = profile->GetStoragePartitionForUrl(origin);
  auto* context = partition->GetPlatformNotificationContext();
  DCHECK(context);
  return context;
}

}  // namespace

// Stores the given |j_resources| to be displayed later again. Note that
// |j_resources| is expected to have 3 entries (icon, badge, image in that
// order) for each notification id in |j_notification_ids|. If a notification
// does not have a particular resource, pass null instead. |j_origins| must be
// the same size as |j_notification_ids|.
static void JNI_NotificationSuspender_StoreNotificationResources(
    JNIEnv* env,
    Profile* profile,
    const JavaParamRef<jobjectArray>& j_notification_ids,
    const JavaParamRef<jobjectArray>& j_origins,
    const JavaParamRef<jobjectArray>& j_resources) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(profile);

  std::vector<std::string> id_strings;
  AppendJavaStringArrayToStringVector(env, j_notification_ids, &id_strings);
  std::vector<std::string> origin_strings;
  AppendJavaStringArrayToStringVector(env, j_origins, &origin_strings);
  std::vector<blink::NotificationResources> resources =
      ParseResources(env, j_resources);

  DCHECK(id_strings.size() == origin_strings.size());
  DCHECK(id_strings.size() == resources.size());

  // Group resources by context.
  std::map<PlatformNotificationContext*, std::vector<NotificationResourceData>>
      resources_by_context;
  for (size_t i = 0; i < id_strings.size(); ++i) {
    GURL origin(std::move(origin_strings[i]));
    if (!origin.is_valid() || !origin.SchemeIsHTTPOrHTTPS())
      continue;
    PlatformNotificationContext* context = GetContext(profile, origin);
    resources_by_context[context].emplace_back(NotificationResourceData{
        std::move(id_strings[i]), std::move(origin), std::move(resources[i])});
  }

  // Store resources in each context.
  for (auto& entry : resources_by_context) {
    entry.first->WriteNotificationResources(std::move(entry.second),
                                            base::DoNothing());
  }
}

// ReDisplays all notifications with stored resources for all |j_origins|.
static void JNI_NotificationSuspender_ReDisplayNotifications(
    JNIEnv* env,
    Profile* profile,
    std::vector<std::string>& origin_strings) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(profile);

  // Group origins by context.
  std::map<PlatformNotificationContext*, std::vector<GURL>> origins_by_context;
  for (std::string& origin_string : origin_strings) {
    GURL origin(std::move(origin_string));
    if (!origin.is_valid() || !origin.SchemeIsHTTPOrHTTPS())
      continue;
    origins_by_context[GetContext(profile, origin)].emplace_back(
        std::move(origin));
  }

  // ReDisplay notifications from each context.
  for (auto& entry : origins_by_context) {
    entry.first->ReDisplayNotifications(std::move(entry.second),
                                        base::DoNothing());
  }
}