• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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.
4 
5 #include "chrome/renderer/extensions/set_icon_natives.h"
6 
7 #include <limits>
8 
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/common/render_messages.h"
11 #include "chrome/renderer/extensions/request_sender.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "third_party/skia/include/core/SkColor.h"
14 
15 namespace {
16 
17 const char* kImageSizeKeys[] = { "19", "38" };
18 const char kInvalidDimensions[] = "ImageData has invalid dimensions.";
19 const char kInvalidData[] = "ImageData data length does not match dimensions.";
20 const char kNoMemory[] = "Chrome was unable to initialize icon.";
21 
22 }  // namespace
23 
24 namespace extensions {
25 
SetIconNatives(Dispatcher * dispatcher,RequestSender * request_sender,ChromeV8Context * context)26 SetIconNatives::SetIconNatives(Dispatcher* dispatcher,
27                                RequestSender* request_sender,
28                                ChromeV8Context* context)
29     : ChromeV8Extension(dispatcher, context),
30       request_sender_(request_sender) {
31   RouteFunction(
32       "SetIconCommon",
33       base::Bind(&SetIconNatives::SetIconCommon, base::Unretained(this)));
34 }
35 
ConvertImageDataToBitmapValue(const v8::Local<v8::Object> image_data,Value ** bitmap_value)36 bool SetIconNatives::ConvertImageDataToBitmapValue(
37     const v8::Local<v8::Object> image_data,
38     Value** bitmap_value) {
39   v8::Isolate* isolate = context()->v8_context()->GetIsolate();
40   v8::Local<v8::Object> data =
41       image_data->Get(v8::String::NewFromUtf8(isolate, "data"))->ToObject();
42   int width =
43       image_data->Get(v8::String::NewFromUtf8(isolate, "width"))->Int32Value();
44   int height =
45       image_data->Get(v8::String::NewFromUtf8(isolate, "height"))->Int32Value();
46 
47   if (width <= 0 || height <= 0) {
48     isolate->ThrowException(v8::Exception::Error(
49         v8::String::NewFromUtf8(isolate, kInvalidDimensions)));
50     return false;
51   }
52 
53   // We need to be able to safely check |data_length| == 4 * width * height
54   // without overflowing below.
55   int max_width = (std::numeric_limits<int>::max() / 4) / height;
56   if (width > max_width) {
57     isolate->ThrowException(v8::Exception::Error(
58         v8::String::NewFromUtf8(isolate, kInvalidDimensions)));
59     return false;
60   }
61 
62   int data_length =
63       data->Get(v8::String::NewFromUtf8(isolate, "length"))->Int32Value();
64   if (data_length != 4 * width * height) {
65     isolate->ThrowException(
66         v8::Exception::Error(v8::String::NewFromUtf8(isolate, kInvalidData)));
67     return false;
68   }
69 
70   SkBitmap bitmap;
71   bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
72   if (!bitmap.allocPixels()) {
73     isolate->ThrowException(
74         v8::Exception::Error(v8::String::NewFromUtf8(isolate, kNoMemory)));
75     return false;
76   }
77   bitmap.eraseARGB(0, 0, 0, 0);
78 
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(4*t + 3))->Int32Value() & 0xFF) << 24) |
84         ((data->Get(v8::Integer::New(4*t + 0))->Int32Value() & 0xFF) << 16) |
85         ((data->Get(v8::Integer::New(4*t + 1))->Int32Value() & 0xFF) << 8) |
86         ((data->Get(v8::Integer::New(4*t + 2))->Int32Value() & 0xFF) << 0));
87   }
88 
89   // Construct the Value object.
90   IPC::Message bitmap_pickle;
91   IPC::WriteParam(&bitmap_pickle, bitmap);
92   *bitmap_value = base::BinaryValue::CreateWithCopiedBuffer(
93       static_cast<const char*>(bitmap_pickle.data()), bitmap_pickle.size());
94 
95   return true;
96 }
97 
ConvertImageDataSetToBitmapValueSet(const v8::FunctionCallbackInfo<v8::Value> & args,base::DictionaryValue * bitmap_set_value)98 bool SetIconNatives::ConvertImageDataSetToBitmapValueSet(
99     const v8::FunctionCallbackInfo<v8::Value>& args,
100     base::DictionaryValue* bitmap_set_value) {
101   v8::Local<v8::Object> extension_args = args[1]->ToObject();
102   v8::Local<v8::Object> details = extension_args
103       ->Get(v8::String::NewFromUtf8(args.GetIsolate(), "0"))->ToObject();
104   v8::Local<v8::Object> image_data_set =
105       details->Get(v8::String::NewFromUtf8(args.GetIsolate(), "imageData"))
106           ->ToObject();
107 
108   DCHECK(bitmap_set_value);
109   for (size_t i = 0; i < arraysize(kImageSizeKeys); i++) {
110     if (!image_data_set->Has(
111             v8::String::NewFromUtf8(args.GetIsolate(), kImageSizeKeys[i])))
112       continue;
113     v8::Local<v8::Object> image_data = image_data_set
114         ->Get(v8::String::NewFromUtf8(args.GetIsolate(), kImageSizeKeys[i]))
115         ->ToObject();
116     Value* image_data_bitmap = NULL;
117     if (!ConvertImageDataToBitmapValue(image_data, &image_data_bitmap))
118       return false;
119     bitmap_set_value->Set(kImageSizeKeys[i], image_data_bitmap);
120   }
121   return true;
122 }
123 
SetIconCommon(const v8::FunctionCallbackInfo<v8::Value> & args)124 void SetIconNatives::SetIconCommon(
125     const v8::FunctionCallbackInfo<v8::Value>& args) {
126   scoped_ptr<base::DictionaryValue> bitmap_set_value(
127       new base::DictionaryValue());
128   if (!ConvertImageDataSetToBitmapValueSet(args, bitmap_set_value.get()))
129     return;
130 
131   v8::Local<v8::Object> extension_args = args[1]->ToObject();
132   v8::Local<v8::Object> details = extension_args
133       ->Get(v8::String::NewFromUtf8(args.GetIsolate(), "0"))->ToObject();
134 
135   base::DictionaryValue* dict = new base::DictionaryValue();
136   dict->Set("imageData", bitmap_set_value.release());
137 
138   if (details->Has(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))) {
139     dict->SetInteger("tabId",
140                      details->Get(v8::String::NewFromUtf8(
141                          args.GetIsolate(), "tabId"))->Int32Value());
142   }
143 
144   ListValue list_value;
145   list_value.Append(dict);
146 
147   std::string name = *v8::String::Utf8Value(args[0]);
148   int request_id = args[2]->Int32Value();
149   bool has_callback = args[3]->BooleanValue();
150   bool for_io_thread = args[4]->BooleanValue();
151 
152   request_sender_->StartRequest(context(),
153                                 name,
154                                 request_id,
155                                 has_callback,
156                                 for_io_thread,
157                                 &list_value);
158 }
159 
160 }  // namespace extensions
161