Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: extensions/renderer/set_icon_natives.cc

Issue 1167423002: Use V8 Maybe APIs in extensions/renderer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/renderer/set_icon_natives.h" 5 #include "extensions/renderer/set_icon_natives.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "content/public/common/common_param_traits.h" 10 #include "content/public/common/common_param_traits.h"
11 #include "extensions/renderer/request_sender.h" 11 #include "extensions/renderer/request_sender.h"
12 #include "extensions/renderer/script_context.h" 12 #include "extensions/renderer/script_context.h"
13 #include "extensions/renderer/v8_maybe_helpers.h"
13 #include "ipc/ipc_message_utils.h" 14 #include "ipc/ipc_message_utils.h"
14 #include "third_party/WebKit/public/web/WebArrayBufferConverter.h" 15 #include "third_party/WebKit/public/web/WebArrayBufferConverter.h"
15 #include "third_party/skia/include/core/SkBitmap.h" 16 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/gfx/ipc/gfx_param_traits.h" 17 #include "ui/gfx/ipc/gfx_param_traits.h"
17 18
18 namespace { 19 namespace {
19 20
20 const char* kImageSizeKeys[] = {"19", "38"}; 21 const char* kImageSizeKeys[] = {"19", "38"};
21 const char kInvalidDimensions[] = "ImageData has invalid dimensions."; 22 const char kInvalidDimensions[] = "ImageData has invalid dimensions.";
22 const char kInvalidData[] = "ImageData data length does not match dimensions."; 23 const char kInvalidData[] = "ImageData data length does not match dimensions.";
23 const char kNoMemory[] = "Chrome was unable to initialize icon."; 24 const char kNoMemory[] = "Chrome was unable to initialize icon.";
24 25
26 bool GetIntValue(v8::Local<v8::Context> context,
27 v8::Local<v8::Object> object,
28 v8::Local<v8::Value> key,
29 int* out) {
30 v8::Local<v8::Value> value;
31 if (!object->Get(context, key).ToLocal(&value))
32 return false;
33 auto maybe = value->Int32Value(context);
34 if (maybe.IsNothing())
35 return false;
36 *out = maybe.FromJust();
37 return true;
38 }
39
40 bool GetIntValue(v8::Local<v8::Context> context,
41 v8::Local<v8::Object> object,
42 const char* key,
43 int* out) {
44 return GetIntValue(context, object,
45 extensions::ToV8String(context->GetIsolate(), key), out);
46 }
47
25 } // namespace 48 } // namespace
26 49
27 namespace extensions { 50 namespace extensions {
28 51
29 SetIconNatives::SetIconNatives(ScriptContext* context) 52 SetIconNatives::SetIconNatives(ScriptContext* context)
30 : ObjectBackedNativeHandler(context) { 53 : ObjectBackedNativeHandler(context) {
31 RouteFunction( 54 RouteFunction(
32 "SetIconCommon", 55 "SetIconCommon",
33 base::Bind(&SetIconNatives::SetIconCommon, base::Unretained(this))); 56 base::Bind(&SetIconNatives::SetIconCommon, base::Unretained(this)));
34 } 57 }
35 58
36 bool SetIconNatives::ConvertImageDataToBitmapValue( 59 bool SetIconNatives::ConvertImageDataToBitmapValue(
37 const v8::Local<v8::Object> image_data, 60 const v8::Local<v8::Object> image_data,
38 v8::Local<v8::Value>* image_data_bitmap) { 61 v8::Local<v8::Value>* image_data_bitmap) {
39 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); 62 v8::Local<v8::Context> v8_context = context()->v8_context();
40 v8::Local<v8::Object> data = 63 v8::Isolate* isolate = v8_context->GetIsolate();
41 image_data->Get(v8::String::NewFromUtf8(isolate, "data")) 64 v8::Local<v8::Value> data_value;
42 ->ToObject(isolate); 65 if (!image_data->Get(v8_context, ToV8String(isolate, "data"))
43 int width = 66 .ToLocal(&data_value) ||
44 image_data->Get(v8::String::NewFromUtf8(isolate, "width"))->Int32Value(); 67 !data_value->IsObject())
45 int height = 68 return false;
46 image_data->Get(v8::String::NewFromUtf8(isolate, "height"))->Int32Value(); 69 v8::Local<v8::Object> data = data_value.As<v8::Object>();
70 int width;
71 int height;
72 if (!GetIntValue(v8_context, image_data, "width", &width) ||
73 !GetIntValue(v8_context, image_data, "height", &height))
74 return false;
47 75
48 if (width <= 0 || height <= 0) { 76 if (width <= 0 || height <= 0) {
49 isolate->ThrowException(v8::Exception::Error( 77 isolate->ThrowException(
50 v8::String::NewFromUtf8(isolate, kInvalidDimensions))); 78 v8::Exception::Error(ToV8String(isolate, kInvalidDimensions)));
51 return false; 79 return false;
52 } 80 }
53 81
54 // We need to be able to safely check |data_length| == 4 * width * height 82 // We need to be able to safely check |data_length| == 4 * width * height
55 // without overflowing below. 83 // without overflowing below.
56 int max_width = (std::numeric_limits<int>::max() / 4) / height; 84 int max_width = (std::numeric_limits<int>::max() / 4) / height;
57 if (width > max_width) { 85 if (width > max_width) {
58 isolate->ThrowException(v8::Exception::Error( 86 isolate->ThrowException(v8::Exception::Error(
59 v8::String::NewFromUtf8(isolate, kInvalidDimensions))); 87 ToV8String(isolate, kInvalidDimensions)));
60 return false; 88 return false;
61 } 89 }
62 90
63 int data_length = 91 int data_length;
64 data->Get(v8::String::NewFromUtf8(isolate, "length"))->Int32Value(); 92 if (!GetIntValue(v8_context, data, "length", &data_length))
93 return false;
65 if (data_length != 4 * width * height) { 94 if (data_length != 4 * width * height) {
66 isolate->ThrowException( 95 isolate->ThrowException(
67 v8::Exception::Error(v8::String::NewFromUtf8(isolate, kInvalidData))); 96 v8::Exception::Error(ToV8String(isolate, kInvalidData)));
68 return false; 97 return false;
69 } 98 }
70 99
71 SkBitmap bitmap; 100 SkBitmap bitmap;
72 if (!bitmap.tryAllocN32Pixels(width, height)) { 101 if (!bitmap.tryAllocN32Pixels(width, height)) {
73 isolate->ThrowException( 102 isolate->ThrowException(
74 v8::Exception::Error(v8::String::NewFromUtf8(isolate, kNoMemory))); 103 v8::Exception::Error(ToV8String(isolate, kNoMemory)));
75 return false; 104 return false;
76 } 105 }
77 bitmap.eraseARGB(0, 0, 0, 0); 106 bitmap.eraseARGB(0, 0, 0, 0);
78 107
79 uint32_t* pixels = bitmap.getAddr32(0, 0); 108 uint32_t* pixels = bitmap.getAddr32(0, 0);
80 for (int t = 0; t < width * height; t++) { 109 for (int t = 0; t < width * height; t++) {
81 // |data| is RGBA, pixels is ARGB. 110 // |data| is RGBA, pixels is ARGB.
82 pixels[t] = SkPreMultiplyColor( 111 int a, r, g, b;
83 ((data->Get(v8::Integer::New(isolate, 4 * t + 3))->Int32Value() & 0xFF) 112 if (!GetIntValue(v8_context, data, v8::Integer::New(isolate, 4 * t + 3),
84 << 24) | 113 &a) ||
85 ((data->Get(v8::Integer::New(isolate, 4 * t + 0))->Int32Value() & 0xFF) 114 !GetIntValue(v8_context, data, v8::Integer::New(isolate, 4 * t + 0),
86 << 16) | 115 &r) ||
87 ((data->Get(v8::Integer::New(isolate, 4 * t + 1))->Int32Value() & 0xFF) 116 !GetIntValue(v8_context, data, v8::Integer::New(isolate, 4 * t + 1),
88 << 8) | 117 &g) ||
89 ((data->Get(v8::Integer::New(isolate, 4 * t + 2))->Int32Value() & 0xFF) 118 !GetIntValue(v8_context, data, v8::Integer::New(isolate, 4 * t + 2),
90 << 0)); 119 &b))
120 return false;
121
122 pixels[t] = SkPreMultiplyColor(((a & 0xFF) << 24) | ((r & 0xFF) << 16) |
123 ((g & 0xFF) << 8) | ((b & 0xFF) << 0));
91 } 124 }
92 125
93 // Construct the Value object. 126 // Construct the Value object.
94 IPC::Message bitmap_pickle; 127 IPC::Message bitmap_pickle;
95 IPC::WriteParam(&bitmap_pickle, bitmap); 128 IPC::WriteParam(&bitmap_pickle, bitmap);
96 blink::WebArrayBuffer buffer = 129 blink::WebArrayBuffer buffer =
97 blink::WebArrayBuffer::create(bitmap_pickle.size(), 1); 130 blink::WebArrayBuffer::create(bitmap_pickle.size(), 1);
98 memcpy(buffer.data(), bitmap_pickle.data(), bitmap_pickle.size()); 131 memcpy(buffer.data(), bitmap_pickle.data(), bitmap_pickle.size());
99 *image_data_bitmap = blink::WebArrayBufferConverter::toV8Value( 132 *image_data_bitmap = blink::WebArrayBufferConverter::toV8Value(
100 &buffer, context()->v8_context()->Global(), isolate); 133 &buffer, context()->v8_context()->Global(), isolate);
101 134
102 return true; 135 return true;
103 } 136 }
104 137
105 bool SetIconNatives::ConvertImageDataSetToBitmapValueSet( 138 bool SetIconNatives::ConvertImageDataSetToBitmapValueSet(
106 v8::Local<v8::Object>& details, 139 v8::Local<v8::Object>& details,
107 v8::Local<v8::Object>* bitmap_set_value) { 140 v8::Local<v8::Object>* bitmap_set_value) {
108 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); 141 v8::Local<v8::Context> v8_context = context()->v8_context();
109 v8::Local<v8::Object> image_data_set = 142 v8::Isolate* isolate = v8_context->GetIsolate();
110 details->Get(v8::String::NewFromUtf8(isolate, "imageData")) 143 v8::Local<v8::Value> image_data_set_value;
111 ->ToObject(isolate); 144 if (!details->Get(v8_context, ToV8String(isolate, "imageData"))
145 .ToLocal(&image_data_set_value) ||
146 !image_data_set_value->IsObject())
147 return false;
148 v8::Local<v8::Object> image_data_set = image_data_set_value.As<v8::Object>();
112 149
113 DCHECK(bitmap_set_value); 150 DCHECK(bitmap_set_value);
114 for (size_t i = 0; i < arraysize(kImageSizeKeys); i++) { 151 for (size_t i = 0; i < arraysize(kImageSizeKeys); i++) {
115 if (!image_data_set->Has( 152 v8::Local<v8::String> key = ToV8String(isolate, kImageSizeKeys[i]);
116 v8::String::NewFromUtf8(isolate, kImageSizeKeys[i]))) 153 if (!CheckV8Call(image_data_set->Has(v8_context, key)))
117 continue; 154 continue;
118 v8::Local<v8::Object> image_data = 155 v8::Local<v8::Value> image_data_value;
119 image_data_set->Get(v8::String::NewFromUtf8(isolate, kImageSizeKeys[i])) 156 if (!image_data_set->Get(v8_context, key).ToLocal(&image_data_value) ||
120 ->ToObject(isolate); 157 !image_data_value->IsObject())
158 continue;
159 v8::Local<v8::Object> image_data = image_data_value.As<v8::Object>();
121 v8::Local<v8::Value> image_data_bitmap; 160 v8::Local<v8::Value> image_data_bitmap;
122 if (!ConvertImageDataToBitmapValue(image_data, &image_data_bitmap)) 161 if (!ConvertImageDataToBitmapValue(image_data, &image_data_bitmap))
123 return false; 162 return false;
124 (*bitmap_set_value)->Set( 163 SetProperty(v8_context, *bitmap_set_value, key, image_data_bitmap);
125 v8::String::NewFromUtf8(isolate, kImageSizeKeys[i]), image_data_bitmap);
126 } 164 }
127 return true; 165 return true;
128 } 166 }
129 167
130 void SetIconNatives::SetIconCommon( 168 void SetIconNatives::SetIconCommon(
131 const v8::FunctionCallbackInfo<v8::Value>& args) { 169 const v8::FunctionCallbackInfo<v8::Value>& args) {
132 CHECK_EQ(1, args.Length()); 170 CHECK_EQ(1, args.Length());
133 CHECK(args[0]->IsObject()); 171 CHECK(args[0]->IsObject());
134 v8::Local<v8::Object> details = args[0]->ToObject(args.GetIsolate()); 172 v8::Local<v8::Object> details = args[0].As<v8::Object>();
135 v8::Local<v8::Object> bitmap_set_value(v8::Object::New(args.GetIsolate())); 173 v8::Local<v8::Object> bitmap_set_value(v8::Object::New(args.GetIsolate()));
136 if (!ConvertImageDataSetToBitmapValueSet(details, &bitmap_set_value)) 174 if (!ConvertImageDataSetToBitmapValueSet(details, &bitmap_set_value))
137 return; 175 return;
138 176
177 v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext();
139 v8::Local<v8::Object> dict(v8::Object::New(args.GetIsolate())); 178 v8::Local<v8::Object> dict(v8::Object::New(args.GetIsolate()));
140 dict->Set(v8::String::NewFromUtf8(args.GetIsolate(), "imageData"), 179 SetProperty(context, dict, ToV8String(args.GetIsolate(), "imageData"),
141 bitmap_set_value); 180 bitmap_set_value);
142 if (details->Has(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))) { 181 v8::Local<v8::String> tab_id = ToV8String(args.GetIsolate(), "tabId");
143 dict->Set( 182 if (CheckV8Call(details->Has(context, tab_id))) {
144 v8::String::NewFromUtf8(args.GetIsolate(), "tabId"), 183 v8::Local<v8::Value> tab_id_value = UnsafeGet(context, details, tab_id);
145 details->Get(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))); 184 SetProperty(context, dict, tab_id, tab_id_value);
146 } 185 }
147 args.GetReturnValue().Set(dict); 186 args.GetReturnValue().Set(dict);
148 } 187 }
149 188
150 } // namespace extensions 189 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/send_request_natives.cc ('k') | extensions/renderer/utils_native_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698