summaryrefslogtreecommitdiffstats
path: root/chrome/browser/notifications/notification_conversion_helper.cc
blob: 4434af17bedbf677f4c0aa2da6223e3d6ebb0988 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2014 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 "chrome/browser/notifications/notification_conversion_helper.h"

#include <string>
#include <vector>

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/common/extensions/api/notification_provider.h"
#include "chrome/common/extensions/api/notifications/notification_style.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "ui/gfx/skia_util.h"

void NotificationConversionHelper::NotificationToNotificationOptions(
    const Notification& notification,
    extensions::api::notifications::NotificationOptions* options) {
  // Extract required fields: type, title, message, and icon.
  std::string type = MapTypeToString(notification.type());
  options->type = extensions::api::notifications::ParseTemplateType(type);

  if (!notification.icon().IsEmpty()) {
    scoped_ptr<extensions::api::notifications::NotificationBitmap> icon(
        new extensions::api::notifications::NotificationBitmap());
    GfxImageToNotificationBitmap(&notification.icon(), icon.get());
    options->icon_bitmap = icon.Pass();
  }

  options->title.reset(
      new std::string(base::UTF16ToUTF8(notification.title())));
  options->message.reset(
      new std::string(base::UTF16ToUTF8(notification.message())));

  // Handle optional data provided.
  const message_center::RichNotificationData* rich_data =
      &notification.rich_notification_data();

  if (!rich_data->small_image.IsEmpty()) {
    scoped_ptr<extensions::api::notifications::NotificationBitmap> icon_mask(
        new extensions::api::notifications::NotificationBitmap());
    GfxImageToNotificationBitmap(&rich_data->small_image, icon_mask.get());
    options->app_icon_mask_bitmap = icon_mask.Pass();
  }

  options->priority.reset(new int(rich_data->priority));

  options->is_clickable.reset(new bool(rich_data->clickable));

  options->event_time.reset(new double(rich_data->timestamp.ToDoubleT()));

  if (!rich_data->context_message.empty())
    options->context_message.reset(
        new std::string(base::UTF16ToUTF8(rich_data->context_message)));

  if (!rich_data->buttons.empty()) {
    scoped_ptr<std::vector<
        linked_ptr<extensions::api::notifications::NotificationButton> > >
        button_list(new std::vector<
            linked_ptr<extensions::api::notifications::NotificationButton> >);
    for (size_t i = 0; i < rich_data->buttons.size(); i++) {
      linked_ptr<extensions::api::notifications::NotificationButton> button(
          new extensions::api::notifications::NotificationButton);
      button->title = base::UTF16ToUTF8(rich_data->buttons[i].title);

      if (!rich_data->buttons[i].icon.IsEmpty()) {
        scoped_ptr<extensions::api::notifications::NotificationBitmap> icon(
            new extensions::api::notifications::NotificationBitmap());
        GfxImageToNotificationBitmap(&rich_data->buttons[i].icon, icon.get());
        button->icon_bitmap = icon.Pass();
      }
      button_list->push_back(button);
    }
    options->buttons = button_list.Pass();
  }

  // Only image type notifications should have images.
  if (type == "image" && !rich_data->image.IsEmpty()) {
    scoped_ptr<extensions::api::notifications::NotificationBitmap> image(
        new extensions::api::notifications::NotificationBitmap());
    GfxImageToNotificationBitmap(&notification.image(), image.get());
    options->image_bitmap = image.Pass();
  } else if (type != "image" && !rich_data->image.IsEmpty()) {
    DVLOG(1) << "Only image type notifications should have images.";
  }

  // Only progress type notifications should have progress bars.
  if (type == "progress")
    options->progress.reset(new int(rich_data->progress));
  else if (rich_data->progress != 0)
    DVLOG(1) << "Only progress type notifications should have progress.";

  // Only list type notifications should have lists.
  if (type == "list" && !rich_data->items.empty()) {
    scoped_ptr<std::vector<
        linked_ptr<extensions::api::notifications::NotificationItem> > >
        list(new std::vector<
            linked_ptr<extensions::api::notifications::NotificationItem> >);
    for (size_t j = 0; j < rich_data->items.size(); j++) {
      linked_ptr<extensions::api::notifications::NotificationItem> item(
          new extensions::api::notifications::NotificationItem);
      item->title = base::UTF16ToUTF8(rich_data->items[j].title);
      item->message = base::UTF16ToUTF8(rich_data->items[j].message);
      list->push_back(item);
    }
    options->items = list.Pass();
  } else if (type != "list" && !rich_data->items.empty()) {
    DVLOG(1) << "Only list type notifications should have lists.";
  }
}

void NotificationConversionHelper::GfxImageToNotificationBitmap(
    const gfx::Image* gfx_image,
    extensions::api::notifications::NotificationBitmap* notification_bitmap) {
  SkBitmap sk_bitmap = gfx_image->AsBitmap();
  sk_bitmap.lockPixels();

  notification_bitmap->width = sk_bitmap.width();
  notification_bitmap->height = sk_bitmap.height();
  int pixel_count = sk_bitmap.width() * sk_bitmap.height();
  const int BYTES_PER_PIXEL = 4;

  uint32_t* bitmap_pixels = sk_bitmap.getAddr32(0, 0);
  const unsigned char* bitmap =
      reinterpret_cast<const unsigned char*>(bitmap_pixels);
  scoped_ptr<std::vector<char>> rgba_bitmap_data(
      new std::vector<char>(pixel_count * BYTES_PER_PIXEL));

  gfx::ConvertSkiaToRGBA(bitmap, pixel_count,
                         reinterpret_cast<unsigned char*>(
                             vector_as_array(rgba_bitmap_data.get())));
  sk_bitmap.unlockPixels();

  notification_bitmap->data = rgba_bitmap_data.Pass();
  return;
}

bool NotificationConversionHelper::NotificationBitmapToGfxImage(
    float max_scale,
    const gfx::Size& target_size_dips,
    extensions::api::notifications::NotificationBitmap* notification_bitmap,
    gfx::Image* return_image) {
  if (!notification_bitmap)
    return false;

  const int max_device_pixel_width = target_size_dips.width() * max_scale;
  const int max_device_pixel_height = target_size_dips.height() * max_scale;

  const int BYTES_PER_PIXEL = 4;

  const int width = notification_bitmap->width;
  const int height = notification_bitmap->height;

  if (width < 0 || height < 0 || width > max_device_pixel_width ||
      height > max_device_pixel_height)
    return false;

  // Ensure we have rgba data.
  std::vector<char>* rgba_data = notification_bitmap->data.get();
  if (!rgba_data)
    return false;

  const size_t rgba_data_length = rgba_data->size();
  const size_t rgba_area = width * height;

  if (rgba_data_length != rgba_area * BYTES_PER_PIXEL)
    return false;

  SkBitmap bitmap;
  // Allocate the actual backing store with the sanitized dimensions.
  if (!bitmap.tryAllocN32Pixels(width, height))
    return false;

  // Ensure that our bitmap and our data now refer to the same number of pixels.
  if (rgba_data_length != bitmap.getSafeSize())
    return false;

  uint32_t* pixels = bitmap.getAddr32(0, 0);
  const char* c_rgba_data = rgba_data->data();

  for (size_t t = 0; t < rgba_area; ++t) {
    // |c_rgba_data| is RGBA, pixels is ARGB.
    size_t rgba_index = t * BYTES_PER_PIXEL;
    pixels[t] =
        SkPreMultiplyColor(((c_rgba_data[rgba_index + 3] & 0xFF) << 24) |
                           ((c_rgba_data[rgba_index + 0] & 0xFF) << 16) |
                           ((c_rgba_data[rgba_index + 1] & 0xFF) << 8) |
                           ((c_rgba_data[rgba_index + 2] & 0xFF) << 0));
  }

  // TODO(dewittj): Handle HiDPI images with more than one scale factor
  // representation.
  gfx::ImageSkia skia(gfx::ImageSkiaRep(bitmap, 1.0f));
  *return_image = gfx::Image(skia);
  return true;
}

std::string NotificationConversionHelper::MapTypeToString(
    message_center::NotificationType type) {
  switch (type) {
    case message_center::NOTIFICATION_TYPE_BASE_FORMAT:
      return "basic";
    case message_center::NOTIFICATION_TYPE_IMAGE:
      return "image";
    case message_center::NOTIFICATION_TYPE_MULTIPLE:
      return "list";
    case message_center::NOTIFICATION_TYPE_PROGRESS:
      return "progress";
    default:
      NOTREACHED();
      return "";
  }
}