| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "content/public/common/common_param_traits.h" | 15 #include "content/public/common/common_param_traits.h" |
| 15 #include "extensions/renderer/request_sender.h" | 16 #include "extensions/renderer/request_sender.h" |
| 16 #include "extensions/renderer/script_context.h" | 17 #include "extensions/renderer/script_context.h" |
| 17 #include "ipc/ipc_message_utils.h" | 18 #include "ipc/ipc_message_utils.h" |
| 18 #include "third_party/WebKit/public/web/WebArrayBufferConverter.h" | 19 #include "third_party/WebKit/public/web/WebArrayBufferConverter.h" |
| 19 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
| 20 #include "ui/gfx/ipc/gfx_param_traits.h" | 21 #include "ui/gfx/ipc/gfx_param_traits.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 const char* kImageSizeKeys[] = {"19", "38"}; | |
| 25 const char kInvalidDimensions[] = "ImageData has invalid dimensions."; | 25 const char kInvalidDimensions[] = "ImageData has invalid dimensions."; |
| 26 const char kInvalidData[] = "ImageData data length does not match dimensions."; | 26 const char kInvalidData[] = "ImageData data length does not match dimensions."; |
| 27 const char kNoMemory[] = "Chrome was unable to initialize icon."; | 27 const char kNoMemory[] = "Chrome was unable to initialize icon."; |
| 28 | 28 |
| 29 } // namespace | 29 } // namespace |
| 30 | 30 |
| 31 namespace extensions { | 31 namespace extensions { |
| 32 | 32 |
| 33 SetIconNatives::SetIconNatives(ScriptContext* context) | 33 SetIconNatives::SetIconNatives(ScriptContext* context) |
| 34 : ObjectBackedNativeHandler(context) { | 34 : ObjectBackedNativeHandler(context) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 | 108 |
| 109 bool SetIconNatives::ConvertImageDataSetToBitmapValueSet( | 109 bool SetIconNatives::ConvertImageDataSetToBitmapValueSet( |
| 110 v8::Local<v8::Object>& details, | 110 v8::Local<v8::Object>& details, |
| 111 v8::Local<v8::Object>* bitmap_set_value) { | 111 v8::Local<v8::Object>* bitmap_set_value) { |
| 112 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); | 112 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
| 113 v8::Local<v8::Object> image_data_set = | 113 v8::Local<v8::Object> image_data_set = |
| 114 details->Get(v8::String::NewFromUtf8(isolate, "imageData")) | 114 details->Get(v8::String::NewFromUtf8(isolate, "imageData")) |
| 115 ->ToObject(isolate); | 115 ->ToObject(isolate); |
| 116 | 116 |
| 117 DCHECK(bitmap_set_value); | 117 DCHECK(bitmap_set_value); |
| 118 for (size_t i = 0; i < arraysize(kImageSizeKeys); i++) { | 118 |
| 119 if (!image_data_set->Has( | 119 v8::Local<v8::Array> property_names(image_data_set->GetOwnPropertyNames()); |
| 120 v8::String::NewFromUtf8(isolate, kImageSizeKeys[i]))) | 120 for (size_t i = 0; i < property_names->Length(); ++i) { |
| 121 v8::Local<v8::Value> key(property_names->Get(i)); |
| 122 v8::String::Utf8Value utf8_key(key); |
| 123 int size; |
| 124 if (!base::StringToInt(std::string(*utf8_key), &size)) |
| 121 continue; | 125 continue; |
| 122 v8::Local<v8::Object> image_data = | 126 v8::Local<v8::Object> image_data = |
| 123 image_data_set->Get(v8::String::NewFromUtf8(isolate, kImageSizeKeys[i])) | 127 image_data_set->Get(key)->ToObject(isolate); |
| 124 ->ToObject(isolate); | |
| 125 v8::Local<v8::Value> image_data_bitmap; | 128 v8::Local<v8::Value> image_data_bitmap; |
| 126 if (!ConvertImageDataToBitmapValue(image_data, &image_data_bitmap)) | 129 if (!ConvertImageDataToBitmapValue(image_data, &image_data_bitmap)) |
| 127 return false; | 130 return false; |
| 128 (*bitmap_set_value)->Set( | 131 (*bitmap_set_value)->Set(key, image_data_bitmap); |
| 129 v8::String::NewFromUtf8(isolate, kImageSizeKeys[i]), image_data_bitmap); | |
| 130 } | 132 } |
| 131 return true; | 133 return true; |
| 132 } | 134 } |
| 133 | 135 |
| 134 void SetIconNatives::SetIconCommon( | 136 void SetIconNatives::SetIconCommon( |
| 135 const v8::FunctionCallbackInfo<v8::Value>& args) { | 137 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 136 CHECK_EQ(1, args.Length()); | 138 CHECK_EQ(1, args.Length()); |
| 137 CHECK(args[0]->IsObject()); | 139 CHECK(args[0]->IsObject()); |
| 138 v8::Local<v8::Object> details = args[0]->ToObject(args.GetIsolate()); | 140 v8::Local<v8::Object> details = args[0]->ToObject(args.GetIsolate()); |
| 139 v8::Local<v8::Object> bitmap_set_value(v8::Object::New(args.GetIsolate())); | 141 v8::Local<v8::Object> bitmap_set_value(v8::Object::New(args.GetIsolate())); |
| 140 if (!ConvertImageDataSetToBitmapValueSet(details, &bitmap_set_value)) | 142 if (!ConvertImageDataSetToBitmapValueSet(details, &bitmap_set_value)) |
| 141 return; | 143 return; |
| 142 | 144 |
| 143 v8::Local<v8::Object> dict(v8::Object::New(args.GetIsolate())); | 145 v8::Local<v8::Object> dict(v8::Object::New(args.GetIsolate())); |
| 144 dict->Set(v8::String::NewFromUtf8(args.GetIsolate(), "imageData"), | 146 dict->Set(v8::String::NewFromUtf8(args.GetIsolate(), "imageData"), |
| 145 bitmap_set_value); | 147 bitmap_set_value); |
| 146 if (details->Has(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))) { | 148 if (details->Has(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))) { |
| 147 dict->Set( | 149 dict->Set( |
| 148 v8::String::NewFromUtf8(args.GetIsolate(), "tabId"), | 150 v8::String::NewFromUtf8(args.GetIsolate(), "tabId"), |
| 149 details->Get(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))); | 151 details->Get(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))); |
| 150 } | 152 } |
| 151 args.GetReturnValue().Set(dict); | 153 args.GetReturnValue().Set(dict); |
| 152 } | 154 } |
| 153 | 155 |
| 154 } // namespace extensions | 156 } // namespace extensions |
| OLD | NEW |