File: notification_resources_loader.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; 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 (148 lines) | stat: -rw-r--r-- 5,560 bytes parent folder | download | duplicates (9)
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
143
144
145
146
147
148
// 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 "third_party/blink/renderer/modules/notifications/notification_resources_loader.h"

#include <cmath>
#include <optional>

#include "base/time/time.h"
#include "third_party/blink/public/common/notifications/notification_constants.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-blink.h"
#include "third_party/blink/public/mojom/notifications/notification.mojom-blink.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/threading.h"

namespace blink {

namespace {

// 99.9% of all images were fetched successfully in 90 seconds.
constexpr base::TimeDelta kImageFetchTimeout = base::Seconds(90);

enum class NotificationIconType { kImage, kIcon, kBadge, kActionIcon };

gfx::Size GetIconDimensions(NotificationIconType type) {
  switch (type) {
    case NotificationIconType::kImage:
      return {kNotificationMaxImageWidthPx, kNotificationMaxImageHeightPx};
    case NotificationIconType::kIcon:
      return {kNotificationMaxIconSizePx, kNotificationMaxIconSizePx};
    case NotificationIconType::kBadge:
      return {kNotificationMaxBadgeSizePx, kNotificationMaxBadgeSizePx};
    case NotificationIconType::kActionIcon:
      return {kNotificationMaxActionIconSizePx,
              kNotificationMaxActionIconSizePx};
  }
}

}  // namespace

NotificationResourcesLoader::NotificationResourcesLoader(
    CompletionCallback completion_callback)
    : started_(false),
      completion_callback_(std::move(completion_callback)),
      pending_request_count_(0) {
  DCHECK(completion_callback_);
}

NotificationResourcesLoader::~NotificationResourcesLoader() = default;

void NotificationResourcesLoader::Start(
    ExecutionContext* context,
    const mojom::blink::NotificationData& notification_data) {
  DCHECK(!started_);
  started_ = true;

  wtf_size_t num_actions = notification_data.actions.has_value()
                               ? notification_data.actions->size()
                               : 0;
  pending_request_count_ = 3 /* image, icon, badge */ + num_actions;

  // TODO(johnme): ensure image is not loaded when it will not be used.
  // TODO(mvanouwerkerk): ensure no badge is loaded when it will not be used.
  LoadIcon(context, notification_data.image,
           GetIconDimensions(NotificationIconType::kImage),
           WTF::BindOnce(&NotificationResourcesLoader::DidLoadIcon,
                         WrapWeakPersistent(this), WTF::Unretained(&image_)));
  LoadIcon(context, notification_data.icon,
           GetIconDimensions(NotificationIconType::kIcon),
           WTF::BindOnce(&NotificationResourcesLoader::DidLoadIcon,
                         WrapWeakPersistent(this), WTF::Unretained(&icon_)));
  LoadIcon(context, notification_data.badge,
           GetIconDimensions(NotificationIconType::kBadge),
           WTF::BindOnce(&NotificationResourcesLoader::DidLoadIcon,
                         WrapWeakPersistent(this), WTF::Unretained(&badge_)));

  action_icons_.Grow(num_actions);
  for (wtf_size_t i = 0; i < num_actions; i++) {
    LoadIcon(context, notification_data.actions.value()[i]->icon,
             GetIconDimensions(NotificationIconType::kActionIcon),
             WTF::BindOnce(&NotificationResourcesLoader::DidLoadIcon,
                           WrapWeakPersistent(this),
                           WTF::Unretained(&action_icons_[i])));
  }
}

mojom::blink::NotificationResourcesPtr
NotificationResourcesLoader::GetResources() const {
  auto resources = mojom::blink::NotificationResources::New();
  resources->image = image_;
  resources->icon = icon_;
  resources->badge = badge_;
  resources->action_icons = action_icons_;
  return resources;
}

void NotificationResourcesLoader::Stop() {
  for (const auto& icon_loader : icon_loaders_)
    icon_loader->Stop();
}

void NotificationResourcesLoader::Trace(Visitor* visitor) const {
  visitor->Trace(icon_loaders_);
}

void NotificationResourcesLoader::LoadIcon(
    ExecutionContext* context,
    const KURL& url,
    const gfx::Size& resize_dimensions,
    ThreadedIconLoader::IconCallback icon_callback) {
  if (url.IsNull() || url.IsEmpty() || !url.IsValid()) {
    std::move(icon_callback).Run(SkBitmap(), -1.0);
    return;
  }

  ResourceRequest resource_request(url);
  resource_request.SetRequestContext(mojom::blink::RequestContextType::IMAGE);
  resource_request.SetRequestDestination(
      network::mojom::RequestDestination::kImage);
  resource_request.SetPriority(ResourceLoadPriority::kMedium);
  resource_request.SetTimeoutInterval(kImageFetchTimeout);

  auto* icon_loader = MakeGarbageCollected<ThreadedIconLoader>();
  icon_loaders_.push_back(icon_loader);
  icon_loader->Start(context, resource_request, resize_dimensions,
                     std::move(icon_callback));
}

void NotificationResourcesLoader::DidLoadIcon(SkBitmap* out_icon,
                                              SkBitmap icon,
                                              double resize_scale) {
  *out_icon = std::move(icon);
  DidFinishRequest();
}

void NotificationResourcesLoader::DidFinishRequest() {
  DCHECK_GT(pending_request_count_, 0);
  pending_request_count_--;
  if (!pending_request_count_) {
    Stop();
    std::move(completion_callback_).Run(this);
    // The |this| pointer may have been deleted now.
  }
}

}  // namespace blink