Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "ipc/ipc_message_utils.h" | 13 #include "ipc/ipc_message_utils.h" |
| 14 #include "third_party/WebKit/public/web/WebArrayBufferConverter.h" | |
| 14 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 #include "ui/gfx/ipc/gfx_param_traits.h" | 16 #include "ui/gfx/ipc/gfx_param_traits.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const char* kImageSizeKeys[] = {"19", "38"}; | 20 const char* kImageSizeKeys[] = {"19", "38"}; |
| 20 const char kInvalidDimensions[] = "ImageData has invalid dimensions."; | 21 const char kInvalidDimensions[] = "ImageData has invalid dimensions."; |
| 21 const char kInvalidData[] = "ImageData data length does not match dimensions."; | 22 const char kInvalidData[] = "ImageData data length does not match dimensions."; |
| 22 const char kNoMemory[] = "Chrome was unable to initialize icon."; | 23 const char kNoMemory[] = "Chrome was unable to initialize icon."; |
| 23 | 24 |
| 24 } // namespace | 25 } // namespace |
| 25 | 26 |
| 26 namespace extensions { | 27 namespace extensions { |
| 27 | 28 |
| 28 SetIconNatives::SetIconNatives(RequestSender* request_sender, | 29 SetIconNatives::SetIconNatives(RequestSender* request_sender, |
| 29 ScriptContext* context) | 30 ScriptContext* context) |
| 30 : ObjectBackedNativeHandler(context), request_sender_(request_sender) { | 31 : ObjectBackedNativeHandler(context), request_sender_(request_sender) { |
| 31 RouteFunction( | 32 RouteFunction( |
| 32 "SetIconCommon", | 33 "SetIconCommon", |
| 33 base::Bind(&SetIconNatives::SetIconCommon, base::Unretained(this))); | 34 base::Bind(&SetIconNatives::SetIconCommon, base::Unretained(this))); |
| 34 } | 35 } |
| 35 | 36 |
| 36 bool SetIconNatives::ConvertImageDataToBitmapValue( | 37 bool SetIconNatives::ConvertImageDataToBitmapValue( |
| 37 const v8::Local<v8::Object> image_data, | 38 const v8::Local<v8::Object> image_data, |
| 38 base::Value** bitmap_value) { | 39 v8::Local<v8::Value>* image_data_bitmap) { |
| 39 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); | 40 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
| 40 v8::Local<v8::Object> data = | 41 v8::Local<v8::Object> data = |
| 41 image_data->Get(v8::String::NewFromUtf8(isolate, "data"))->ToObject(); | 42 image_data->Get(v8::String::NewFromUtf8(isolate, "data"))->ToObject(); |
| 42 int width = | 43 int width = |
| 43 image_data->Get(v8::String::NewFromUtf8(isolate, "width"))->Int32Value(); | 44 image_data->Get(v8::String::NewFromUtf8(isolate, "width"))->Int32Value(); |
| 44 int height = | 45 int height = |
| 45 image_data->Get(v8::String::NewFromUtf8(isolate, "height"))->Int32Value(); | 46 image_data->Get(v8::String::NewFromUtf8(isolate, "height"))->Int32Value(); |
| 46 | 47 |
| 47 if (width <= 0 || height <= 0) { | 48 if (width <= 0 || height <= 0) { |
| 48 isolate->ThrowException(v8::Exception::Error( | 49 isolate->ThrowException(v8::Exception::Error( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 << 16) | | 86 << 16) | |
| 86 ((data->Get(v8::Integer::New(isolate, 4 * t + 1))->Int32Value() & 0xFF) | 87 ((data->Get(v8::Integer::New(isolate, 4 * t + 1))->Int32Value() & 0xFF) |
| 87 << 8) | | 88 << 8) | |
| 88 ((data->Get(v8::Integer::New(isolate, 4 * t + 2))->Int32Value() & 0xFF) | 89 ((data->Get(v8::Integer::New(isolate, 4 * t + 2))->Int32Value() & 0xFF) |
| 89 << 0)); | 90 << 0)); |
| 90 } | 91 } |
| 91 | 92 |
| 92 // Construct the Value object. | 93 // Construct the Value object. |
| 93 IPC::Message bitmap_pickle; | 94 IPC::Message bitmap_pickle; |
| 94 IPC::WriteParam(&bitmap_pickle, bitmap); | 95 IPC::WriteParam(&bitmap_pickle, bitmap); |
| 95 *bitmap_value = base::BinaryValue::CreateWithCopiedBuffer( | 96 blink::WebArrayBuffer buffer = |
| 96 static_cast<const char*>(bitmap_pickle.data()), bitmap_pickle.size()); | 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); | |
| 97 | 101 |
| 98 return true; | 102 return true; |
| 99 } | 103 } |
| 100 | 104 |
| 101 bool SetIconNatives::ConvertImageDataSetToBitmapValueSet( | 105 bool SetIconNatives::ConvertImageDataSetToBitmapValueSet( |
| 102 const v8::FunctionCallbackInfo<v8::Value>& args, | 106 const v8::FunctionCallbackInfo<v8::Value>& args, |
| 103 base::DictionaryValue* bitmap_set_value) { | 107 v8::Local<v8::Object>* bitmap_set_value) { |
| 104 v8::Local<v8::Object> extension_args = args[1]->ToObject(); | 108 v8::Local<v8::Object> details = args[0]->ToObject(); |
| 105 v8::Local<v8::Object> details = | |
| 106 extension_args->Get(v8::String::NewFromUtf8(args.GetIsolate(), "0")) | |
| 107 ->ToObject(); | |
| 108 v8::Local<v8::Object> image_data_set = | 109 v8::Local<v8::Object> image_data_set = |
| 109 details->Get(v8::String::NewFromUtf8(args.GetIsolate(), "imageData")) | 110 details->Get(v8::String::NewFromUtf8( |
| 110 ->ToObject(); | 111 args.GetIsolate(), "imageData"))->ToObject(); |
| 111 | 112 |
| 112 DCHECK(bitmap_set_value); | 113 DCHECK(bitmap_set_value); |
| 113 for (size_t i = 0; i < arraysize(kImageSizeKeys); i++) { | 114 for (size_t i = 0; i < arraysize(kImageSizeKeys); i++) { |
| 114 if (!image_data_set->Has( | 115 if (!image_data_set->Has( |
| 115 v8::String::NewFromUtf8(args.GetIsolate(), kImageSizeKeys[i]))) | 116 v8::String::NewFromUtf8(args.GetIsolate(), kImageSizeKeys[i]))) |
| 116 continue; | 117 continue; |
| 117 v8::Local<v8::Object> image_data = | 118 v8::Local<v8::Object> image_data = |
| 118 image_data_set->Get(v8::String::NewFromUtf8(args.GetIsolate(), | 119 image_data_set->Get(v8::String::NewFromUtf8(args.GetIsolate(), |
| 119 kImageSizeKeys[i])) | 120 kImageSizeKeys[i])) |
| 120 ->ToObject(); | 121 ->ToObject(); |
| 121 base::Value* image_data_bitmap = NULL; | 122 v8::Local<v8::Value> image_data_bitmap; |
| 122 if (!ConvertImageDataToBitmapValue(image_data, &image_data_bitmap)) | 123 if (!ConvertImageDataToBitmapValue(image_data, &image_data_bitmap)) |
| 123 return false; | 124 return false; |
| 124 bitmap_set_value->Set(kImageSizeKeys[i], image_data_bitmap); | 125 (*bitmap_set_value)->Set( |
| 126 v8::String::NewFromUtf8(args.GetIsolate(), kImageSizeKeys[i]), | |
| 127 image_data_bitmap); | |
| 125 } | 128 } |
| 126 return true; | 129 return true; |
| 127 } | 130 } |
| 128 | 131 |
| 129 void SetIconNatives::SetIconCommon( | 132 void SetIconNatives::SetIconCommon( |
| 130 const v8::FunctionCallbackInfo<v8::Value>& args) { | 133 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 131 scoped_ptr<base::DictionaryValue> bitmap_set_value( | 134 CHECK_EQ(1, args.Length()); |
| 132 new base::DictionaryValue()); | 135 CHECK(args[0]->IsObject()); |
| 133 if (!ConvertImageDataSetToBitmapValueSet(args, bitmap_set_value.get())) | 136 v8::Local<v8::Object> bitmap_set_value(v8::Object::New(args.GetIsolate())); |
| 137 if (!ConvertImageDataSetToBitmapValueSet(args, &bitmap_set_value)) | |
|
not at google - send to devlin
2014/08/20 14:42:19
All ConvertImageDataSetToBitmapValueSet does to |a
gpdavis
2014/08/20 17:58:18
Ah, I wasn't sure if these were the same isolate a
| |
| 134 return; | 138 return; |
| 135 | 139 |
| 136 v8::Local<v8::Object> extension_args = args[1]->ToObject(); | 140 v8::Local<v8::Object> details = args[0]->ToObject(); |
| 137 v8::Local<v8::Object> details = | |
| 138 extension_args->Get(v8::String::NewFromUtf8(args.GetIsolate(), "0")) | |
| 139 ->ToObject(); | |
| 140 | 141 |
| 141 base::DictionaryValue* dict = new base::DictionaryValue(); | 142 v8::Local<v8::Object> dict(v8::Object::New(args.GetIsolate())); |
| 142 dict->Set("imageData", bitmap_set_value.release()); | 143 dict->Set(v8::String::NewFromUtf8(args.GetIsolate(), "imageData"), |
| 143 | 144 bitmap_set_value); |
| 144 if (details->Has(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))) { | 145 if (details->Has(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))) { |
| 145 dict->SetInteger( | 146 dict->Set( |
| 146 "tabId", | 147 v8::String::NewFromUtf8(args.GetIsolate(), "tabId"), |
| 147 details->Get(v8::String::NewFromUtf8(args.GetIsolate(), "tabId")) | 148 details->Get(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))); |
| 148 ->Int32Value()); | |
| 149 } | 149 } |
| 150 | 150 args.GetReturnValue().Set(dict); |
| 151 base::ListValue list_value; | |
| 152 list_value.Append(dict); | |
| 153 | |
| 154 std::string name = *v8::String::Utf8Value(args[0]); | |
| 155 int request_id = args[2]->Int32Value(); | |
| 156 bool has_callback = args[3]->BooleanValue(); | |
| 157 bool for_io_thread = args[4]->BooleanValue(); | |
| 158 | |
| 159 request_sender_->StartRequest( | |
| 160 context(), name, request_id, has_callback, for_io_thread, &list_value); | |
| 161 } | 151 } |
| 162 | 152 |
| 163 } // namespace extensions | 153 } // namespace extensions |
| OLD | NEW |