Index: extensions/renderer/set_icon_natives.cc |
diff --git a/extensions/renderer/set_icon_natives.cc b/extensions/renderer/set_icon_natives.cc |
index 744b4b4389fa53892d38433ecd125fee6839fb32..aeaebc4cd98d560f2226267e55ed4d895365170a 100644 |
--- a/extensions/renderer/set_icon_natives.cc |
+++ b/extensions/renderer/set_icon_natives.cc |
@@ -10,6 +10,7 @@ |
#include "content/public/common/common_param_traits.h" |
#include "extensions/renderer/request_sender.h" |
#include "extensions/renderer/script_context.h" |
+#include "extensions/renderer/v8_maybe_helpers.h" |
#include "ipc/ipc_message_utils.h" |
#include "third_party/WebKit/public/web/WebArrayBufferConverter.h" |
#include "third_party/skia/include/core/SkBitmap.h" |
@@ -22,6 +23,28 @@ const char kInvalidDimensions[] = "ImageData has invalid dimensions."; |
const char kInvalidData[] = "ImageData data length does not match dimensions."; |
const char kNoMemory[] = "Chrome was unable to initialize icon."; |
+bool GetIntValue(v8::Local<v8::Context> context, |
+ v8::Local<v8::Object> object, |
+ v8::Local<v8::Value> key, |
+ int* out) { |
+ v8::Local<v8::Value> value; |
+ if (!object->Get(context, key).ToLocal(&value)) |
+ return false; |
+ auto maybe = value->Int32Value(context); |
+ if (maybe.IsNothing()) |
+ return false; |
+ *out = maybe.FromJust(); |
+ return true; |
+} |
+ |
+bool GetIntValue(v8::Local<v8::Context> context, |
+ v8::Local<v8::Object> object, |
+ const char* key, |
+ int* out) { |
+ return GetIntValue(context, object, |
+ extensions::ToV8String(context->GetIsolate(), key), out); |
+} |
+ |
} // namespace |
namespace extensions { |
@@ -36,18 +59,23 @@ SetIconNatives::SetIconNatives(ScriptContext* context) |
bool SetIconNatives::ConvertImageDataToBitmapValue( |
const v8::Local<v8::Object> image_data, |
v8::Local<v8::Value>* image_data_bitmap) { |
- v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
- v8::Local<v8::Object> data = |
- image_data->Get(v8::String::NewFromUtf8(isolate, "data")) |
- ->ToObject(isolate); |
- int width = |
- image_data->Get(v8::String::NewFromUtf8(isolate, "width"))->Int32Value(); |
- int height = |
- image_data->Get(v8::String::NewFromUtf8(isolate, "height"))->Int32Value(); |
+ v8::Local<v8::Context> v8_context = context()->v8_context(); |
+ v8::Isolate* isolate = v8_context->GetIsolate(); |
+ v8::Local<v8::Value> data_value; |
+ if (!image_data->Get(v8_context, ToV8String(isolate, "data")) |
+ .ToLocal(&data_value) || |
+ !data_value->IsObject()) |
+ return false; |
+ v8::Local<v8::Object> data = data_value.As<v8::Object>(); |
+ int width; |
+ int height; |
+ if (!GetIntValue(v8_context, image_data, "width", &width) || |
+ !GetIntValue(v8_context, image_data, "height", &height)) |
+ return false; |
if (width <= 0 || height <= 0) { |
- isolate->ThrowException(v8::Exception::Error( |
- v8::String::NewFromUtf8(isolate, kInvalidDimensions))); |
+ isolate->ThrowException( |
+ v8::Exception::Error(ToV8String(isolate, kInvalidDimensions))); |
return false; |
} |
@@ -56,22 +84,23 @@ bool SetIconNatives::ConvertImageDataToBitmapValue( |
int max_width = (std::numeric_limits<int>::max() / 4) / height; |
if (width > max_width) { |
isolate->ThrowException(v8::Exception::Error( |
- v8::String::NewFromUtf8(isolate, kInvalidDimensions))); |
+ ToV8String(isolate, kInvalidDimensions))); |
return false; |
} |
- int data_length = |
- data->Get(v8::String::NewFromUtf8(isolate, "length"))->Int32Value(); |
+ int data_length; |
+ if (!GetIntValue(v8_context, data, "length", &data_length)) |
+ return false; |
if (data_length != 4 * width * height) { |
isolate->ThrowException( |
- v8::Exception::Error(v8::String::NewFromUtf8(isolate, kInvalidData))); |
+ v8::Exception::Error(ToV8String(isolate, kInvalidData))); |
return false; |
} |
SkBitmap bitmap; |
if (!bitmap.tryAllocN32Pixels(width, height)) { |
isolate->ThrowException( |
- v8::Exception::Error(v8::String::NewFromUtf8(isolate, kNoMemory))); |
+ v8::Exception::Error(ToV8String(isolate, kNoMemory))); |
return false; |
} |
bitmap.eraseARGB(0, 0, 0, 0); |
@@ -79,15 +108,19 @@ bool SetIconNatives::ConvertImageDataToBitmapValue( |
uint32_t* pixels = bitmap.getAddr32(0, 0); |
for (int t = 0; t < width * height; t++) { |
// |data| is RGBA, pixels is ARGB. |
- pixels[t] = SkPreMultiplyColor( |
- ((data->Get(v8::Integer::New(isolate, 4 * t + 3))->Int32Value() & 0xFF) |
- << 24) | |
- ((data->Get(v8::Integer::New(isolate, 4 * t + 0))->Int32Value() & 0xFF) |
- << 16) | |
- ((data->Get(v8::Integer::New(isolate, 4 * t + 1))->Int32Value() & 0xFF) |
- << 8) | |
- ((data->Get(v8::Integer::New(isolate, 4 * t + 2))->Int32Value() & 0xFF) |
- << 0)); |
+ int a, r, g, b; |
+ if (!GetIntValue(v8_context, data, v8::Integer::New(isolate, 4 * t + 3), |
+ &a) || |
+ !GetIntValue(v8_context, data, v8::Integer::New(isolate, 4 * t + 0), |
+ &r) || |
+ !GetIntValue(v8_context, data, v8::Integer::New(isolate, 4 * t + 1), |
+ &g) || |
+ !GetIntValue(v8_context, data, v8::Integer::New(isolate, 4 * t + 2), |
+ &b)) |
+ return false; |
+ |
+ pixels[t] = SkPreMultiplyColor(((a & 0xFF) << 24) | ((r & 0xFF) << 16) | |
+ ((g & 0xFF) << 8) | ((b & 0xFF) << 0)); |
} |
// Construct the Value object. |
@@ -105,24 +138,29 @@ bool SetIconNatives::ConvertImageDataToBitmapValue( |
bool SetIconNatives::ConvertImageDataSetToBitmapValueSet( |
v8::Local<v8::Object>& details, |
v8::Local<v8::Object>* bitmap_set_value) { |
- v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
- v8::Local<v8::Object> image_data_set = |
- details->Get(v8::String::NewFromUtf8(isolate, "imageData")) |
- ->ToObject(isolate); |
+ v8::Local<v8::Context> v8_context = context()->v8_context(); |
+ v8::Isolate* isolate = v8_context->GetIsolate(); |
+ v8::Local<v8::Value> image_data_set_value; |
+ if (!details->Get(v8_context, ToV8String(isolate, "imageData")) |
+ .ToLocal(&image_data_set_value) || |
+ !image_data_set_value->IsObject()) |
+ return false; |
+ v8::Local<v8::Object> image_data_set = image_data_set_value.As<v8::Object>(); |
DCHECK(bitmap_set_value); |
for (size_t i = 0; i < arraysize(kImageSizeKeys); i++) { |
- if (!image_data_set->Has( |
- v8::String::NewFromUtf8(isolate, kImageSizeKeys[i]))) |
+ v8::Local<v8::String> key = ToV8String(isolate, kImageSizeKeys[i]); |
+ if (!CheckV8Call(image_data_set->Has(v8_context, key))) |
+ continue; |
+ v8::Local<v8::Value> image_data_value; |
+ if (!image_data_set->Get(v8_context, key).ToLocal(&image_data_value) || |
+ !image_data_value->IsObject()) |
continue; |
- v8::Local<v8::Object> image_data = |
- image_data_set->Get(v8::String::NewFromUtf8(isolate, kImageSizeKeys[i])) |
- ->ToObject(isolate); |
+ v8::Local<v8::Object> image_data = image_data_value.As<v8::Object>(); |
v8::Local<v8::Value> image_data_bitmap; |
if (!ConvertImageDataToBitmapValue(image_data, &image_data_bitmap)) |
return false; |
- (*bitmap_set_value)->Set( |
- v8::String::NewFromUtf8(isolate, kImageSizeKeys[i]), image_data_bitmap); |
+ SetProperty(v8_context, *bitmap_set_value, key, image_data_bitmap); |
} |
return true; |
} |
@@ -131,18 +169,19 @@ void SetIconNatives::SetIconCommon( |
const v8::FunctionCallbackInfo<v8::Value>& args) { |
CHECK_EQ(1, args.Length()); |
CHECK(args[0]->IsObject()); |
- v8::Local<v8::Object> details = args[0]->ToObject(args.GetIsolate()); |
+ v8::Local<v8::Object> details = args[0].As<v8::Object>(); |
v8::Local<v8::Object> bitmap_set_value(v8::Object::New(args.GetIsolate())); |
if (!ConvertImageDataSetToBitmapValueSet(details, &bitmap_set_value)) |
return; |
+ v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); |
v8::Local<v8::Object> dict(v8::Object::New(args.GetIsolate())); |
- dict->Set(v8::String::NewFromUtf8(args.GetIsolate(), "imageData"), |
- bitmap_set_value); |
- if (details->Has(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))) { |
- dict->Set( |
- v8::String::NewFromUtf8(args.GetIsolate(), "tabId"), |
- details->Get(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))); |
+ SetProperty(context, dict, ToV8String(args.GetIsolate(), "imageData"), |
+ bitmap_set_value); |
+ v8::Local<v8::String> tab_id = ToV8String(args.GetIsolate(), "tabId"); |
+ if (CheckV8Call(details->Has(context, tab_id))) { |
+ v8::Local<v8::Value> tab_id_value = UnsafeGet(context, details, tab_id); |
+ SetProperty(context, dict, tab_id, tab_id_value); |
} |
args.GetReturnValue().Set(dict); |
} |