File: NotificationResourcesLoader.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (131 lines) | stat: -rw-r--r-- 4,606 bytes parent folder | download
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "modules/notifications/NotificationResourcesLoader.h"

#include "platform/Histogram.h"
#include "platform/weborigin/KURL.h"
#include "public/platform/modules/notifications/WebNotificationData.h"
#include "public/platform/modules/notifications/WebNotificationResources.h"
#include "wtf/CurrentTime.h"
#include "wtf/Threading.h"
#include <cmath>

namespace blink {

NotificationResourcesLoader::NotificationResourcesLoader(
    std::unique_ptr<CompletionCallback> completionCallback)
    : m_started(false),
      m_completionCallback(std::move(completionCallback)),
      m_pendingRequestCount(0) {
  DCHECK(m_completionCallback);
}

NotificationResourcesLoader::~NotificationResourcesLoader() {}

void NotificationResourcesLoader::start(
    ExecutionContext* executionContext,
    const WebNotificationData& notificationData) {
  DCHECK(!m_started);
  m_started = true;

  size_t numActions = notificationData.actions.size();
  m_pendingRequestCount = 3 /* image, icon, badge */ + numActions;

  // 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.
  loadImage(executionContext, NotificationImageLoader::Type::Image,
            notificationData.image,
            WTF::bind(&NotificationResourcesLoader::didLoadImage,
                      wrapWeakPersistent(this)));
  loadImage(executionContext, NotificationImageLoader::Type::Icon,
            notificationData.icon,
            WTF::bind(&NotificationResourcesLoader::didLoadIcon,
                      wrapWeakPersistent(this)));
  loadImage(executionContext, NotificationImageLoader::Type::Badge,
            notificationData.badge,
            WTF::bind(&NotificationResourcesLoader::didLoadBadge,
                      wrapWeakPersistent(this)));

  m_actionIcons.resize(numActions);
  for (size_t i = 0; i < numActions; i++)
    loadImage(executionContext, NotificationImageLoader::Type::ActionIcon,
              notificationData.actions[i].icon,
              WTF::bind(&NotificationResourcesLoader::didLoadActionIcon,
                        wrapWeakPersistent(this), i));
}

std::unique_ptr<WebNotificationResources>
NotificationResourcesLoader::getResources() const {
  std::unique_ptr<WebNotificationResources> resources(
      new WebNotificationResources());
  resources->image = m_image;
  resources->icon = m_icon;
  resources->badge = m_badge;
  resources->actionIcons = m_actionIcons;
  return resources;
}

void NotificationResourcesLoader::stop() {
  for (auto imageLoader : m_imageLoaders)
    imageLoader->stop();
}

DEFINE_TRACE(NotificationResourcesLoader) {
  visitor->trace(m_imageLoaders);
}

void NotificationResourcesLoader::loadImage(
    ExecutionContext* executionContext,
    NotificationImageLoader::Type type,
    const KURL& url,
    std::unique_ptr<NotificationImageLoader::ImageCallback> imageCallback) {
  if (url.isNull() || url.isEmpty() || !url.isValid()) {
    didFinishRequest();
    return;
  }

  NotificationImageLoader* imageLoader = new NotificationImageLoader(type);
  m_imageLoaders.push_back(imageLoader);
  imageLoader->start(executionContext, url, std::move(imageCallback));
}

void NotificationResourcesLoader::didLoadImage(const SkBitmap& image) {
  m_image = NotificationImageLoader::scaleDownIfNeeded(
      image, NotificationImageLoader::Type::Image);
  didFinishRequest();
}

void NotificationResourcesLoader::didLoadIcon(const SkBitmap& image) {
  m_icon = NotificationImageLoader::scaleDownIfNeeded(
      image, NotificationImageLoader::Type::Icon);
  didFinishRequest();
}

void NotificationResourcesLoader::didLoadBadge(const SkBitmap& image) {
  m_badge = NotificationImageLoader::scaleDownIfNeeded(
      image, NotificationImageLoader::Type::Badge);
  didFinishRequest();
}

void NotificationResourcesLoader::didLoadActionIcon(size_t actionIndex,
                                                    const SkBitmap& image) {
  DCHECK_LT(actionIndex, m_actionIcons.size());

  m_actionIcons[actionIndex] = NotificationImageLoader::scaleDownIfNeeded(
      image, NotificationImageLoader::Type::ActionIcon);
  didFinishRequest();
}

void NotificationResourcesLoader::didFinishRequest() {
  DCHECK_GT(m_pendingRequestCount, 0);
  m_pendingRequestCount--;
  if (!m_pendingRequestCount) {
    stop();
    (*m_completionCallback)(this);
    // The |this| pointer may have been deleted now.
  }
}

}  // namespace blink