fix typo in GN target comment
[chromium-blink-merge.git] / extensions / renderer / set_icon_natives.cc
blob744b4b4389fa53892d38433ecd125fee6839fb32
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/renderer/set_icon_natives.h"
7 #include <limits>
9 #include "base/memory/scoped_ptr.h"
10 #include "content/public/common/common_param_traits.h"
11 #include "extensions/renderer/request_sender.h"
12 #include "extensions/renderer/script_context.h"
13 #include "ipc/ipc_message_utils.h"
14 #include "third_party/WebKit/public/web/WebArrayBufferConverter.h"
15 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/gfx/ipc/gfx_param_traits.h"
18 namespace {
20 const char* kImageSizeKeys[] = {"19", "38"};
21 const char kInvalidDimensions[] = "ImageData has invalid dimensions.";
22 const char kInvalidData[] = "ImageData data length does not match dimensions.";
23 const char kNoMemory[] = "Chrome was unable to initialize icon.";
25 } // namespace
27 namespace extensions {
29 SetIconNatives::SetIconNatives(ScriptContext* context)
30 : ObjectBackedNativeHandler(context) {
31 RouteFunction(
32 "SetIconCommon",
33 base::Bind(&SetIconNatives::SetIconCommon, base::Unretained(this)));
36 bool SetIconNatives::ConvertImageDataToBitmapValue(
37 const v8::Local<v8::Object> image_data,
38 v8::Local<v8::Value>* image_data_bitmap) {
39 v8::Isolate* isolate = context()->v8_context()->GetIsolate();
40 v8::Local<v8::Object> data =
41 image_data->Get(v8::String::NewFromUtf8(isolate, "data"))
42 ->ToObject(isolate);
43 int width =
44 image_data->Get(v8::String::NewFromUtf8(isolate, "width"))->Int32Value();
45 int height =
46 image_data->Get(v8::String::NewFromUtf8(isolate, "height"))->Int32Value();
48 if (width <= 0 || height <= 0) {
49 isolate->ThrowException(v8::Exception::Error(
50 v8::String::NewFromUtf8(isolate, kInvalidDimensions)));
51 return false;
54 // We need to be able to safely check |data_length| == 4 * width * height
55 // without overflowing below.
56 int max_width = (std::numeric_limits<int>::max() / 4) / height;
57 if (width > max_width) {
58 isolate->ThrowException(v8::Exception::Error(
59 v8::String::NewFromUtf8(isolate, kInvalidDimensions)));
60 return false;
63 int data_length =
64 data->Get(v8::String::NewFromUtf8(isolate, "length"))->Int32Value();
65 if (data_length != 4 * width * height) {
66 isolate->ThrowException(
67 v8::Exception::Error(v8::String::NewFromUtf8(isolate, kInvalidData)));
68 return false;
71 SkBitmap bitmap;
72 if (!bitmap.tryAllocN32Pixels(width, height)) {
73 isolate->ThrowException(
74 v8::Exception::Error(v8::String::NewFromUtf8(isolate, kNoMemory)));
75 return false;
77 bitmap.eraseARGB(0, 0, 0, 0);
79 uint32_t* pixels = bitmap.getAddr32(0, 0);
80 for (int t = 0; t < width * height; t++) {
81 // |data| is RGBA, pixels is ARGB.
82 pixels[t] = SkPreMultiplyColor(
83 ((data->Get(v8::Integer::New(isolate, 4 * t + 3))->Int32Value() & 0xFF)
84 << 24) |
85 ((data->Get(v8::Integer::New(isolate, 4 * t + 0))->Int32Value() & 0xFF)
86 << 16) |
87 ((data->Get(v8::Integer::New(isolate, 4 * t + 1))->Int32Value() & 0xFF)
88 << 8) |
89 ((data->Get(v8::Integer::New(isolate, 4 * t + 2))->Int32Value() & 0xFF)
90 << 0));
93 // Construct the Value object.
94 IPC::Message bitmap_pickle;
95 IPC::WriteParam(&bitmap_pickle, bitmap);
96 blink::WebArrayBuffer buffer =
97 blink::WebArrayBuffer::create(bitmap_pickle.size(), 1);
98 memcpy(buffer.data(), bitmap_pickle.data(), bitmap_pickle.size());
99 *image_data_bitmap = blink::WebArrayBufferConverter::toV8Value(
100 &buffer, context()->v8_context()->Global(), isolate);
102 return true;
105 bool SetIconNatives::ConvertImageDataSetToBitmapValueSet(
106 v8::Local<v8::Object>& details,
107 v8::Local<v8::Object>* bitmap_set_value) {
108 v8::Isolate* isolate = context()->v8_context()->GetIsolate();
109 v8::Local<v8::Object> image_data_set =
110 details->Get(v8::String::NewFromUtf8(isolate, "imageData"))
111 ->ToObject(isolate);
113 DCHECK(bitmap_set_value);
114 for (size_t i = 0; i < arraysize(kImageSizeKeys); i++) {
115 if (!image_data_set->Has(
116 v8::String::NewFromUtf8(isolate, kImageSizeKeys[i])))
117 continue;
118 v8::Local<v8::Object> image_data =
119 image_data_set->Get(v8::String::NewFromUtf8(isolate, kImageSizeKeys[i]))
120 ->ToObject(isolate);
121 v8::Local<v8::Value> image_data_bitmap;
122 if (!ConvertImageDataToBitmapValue(image_data, &image_data_bitmap))
123 return false;
124 (*bitmap_set_value)->Set(
125 v8::String::NewFromUtf8(isolate, kImageSizeKeys[i]), image_data_bitmap);
127 return true;
130 void SetIconNatives::SetIconCommon(
131 const v8::FunctionCallbackInfo<v8::Value>& args) {
132 CHECK_EQ(1, args.Length());
133 CHECK(args[0]->IsObject());
134 v8::Local<v8::Object> details = args[0]->ToObject(args.GetIsolate());
135 v8::Local<v8::Object> bitmap_set_value(v8::Object::New(args.GetIsolate()));
136 if (!ConvertImageDataSetToBitmapValueSet(details, &bitmap_set_value))
137 return;
139 v8::Local<v8::Object> dict(v8::Object::New(args.GetIsolate()));
140 dict->Set(v8::String::NewFromUtf8(args.GetIsolate(), "imageData"),
141 bitmap_set_value);
142 if (details->Has(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))) {
143 dict->Set(
144 v8::String::NewFromUtf8(args.GetIsolate(), "tabId"),
145 details->Get(v8::String::NewFromUtf8(args.GetIsolate(), "tabId")));
147 args.GetReturnValue().Set(dict);
150 } // namespace extensions