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

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

Issue 477193003: Refactor setIcon to allow for more general use of imageData (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added page action binding, cleaned up setIcon logic Created 6 years, 4 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/base64.h"
9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
10 #include "content/public/common/common_param_traits.h" 11 #include "content/public/common/common_param_traits.h"
12 #include "content/public/renderer/v8_value_converter.h"
11 #include "extensions/renderer/request_sender.h" 13 #include "extensions/renderer/request_sender.h"
12 #include "extensions/renderer/script_context.h" 14 #include "extensions/renderer/script_context.h"
13 #include "ipc/ipc_message_utils.h" 15 #include "ipc/ipc_message_utils.h"
14 #include "third_party/skia/include/core/SkBitmap.h" 16 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/gfx/ipc/gfx_param_traits.h" 17 #include "ui/gfx/ipc/gfx_param_traits.h"
16 18
17 namespace { 19 namespace {
18 20
19 const char* kImageSizeKeys[] = {"19", "38"}; 21 const char* kImageSizeKeys[] = {"19", "38"};
20 const char kInvalidDimensions[] = "ImageData has invalid dimensions."; 22 const char kInvalidDimensions[] = "ImageData has invalid dimensions.";
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 << 16) | 87 << 16) |
86 ((data->Get(v8::Integer::New(isolate, 4 * t + 1))->Int32Value() & 0xFF) 88 ((data->Get(v8::Integer::New(isolate, 4 * t + 1))->Int32Value() & 0xFF)
87 << 8) | 89 << 8) |
88 ((data->Get(v8::Integer::New(isolate, 4 * t + 2))->Int32Value() & 0xFF) 90 ((data->Get(v8::Integer::New(isolate, 4 * t + 2))->Int32Value() & 0xFF)
89 << 0)); 91 << 0));
90 } 92 }
91 93
92 // Construct the Value object. 94 // Construct the Value object.
93 IPC::Message bitmap_pickle; 95 IPC::Message bitmap_pickle;
94 IPC::WriteParam(&bitmap_pickle, bitmap); 96 IPC::WriteParam(&bitmap_pickle, bitmap);
95 *bitmap_value = base::BinaryValue::CreateWithCopiedBuffer( 97 std::string bitmap_data = std::string(reinterpret_cast<const char*>(
96 static_cast<const char*>(bitmap_pickle.data()), bitmap_pickle.size()); 98 bitmap_pickle.data()), bitmap_pickle.size());
99 std::string data64;
100 base::Base64Encode(bitmap_data, &data64);
not at google - send to devlin 2014/08/19 17:32:29 Ideally you'd only need to convert to a binary val
gpdavis 2014/08/19 22:05:07 Well, that took a while, but I finally figured out
not at google - send to devlin 2014/08/20 14:42:18 Nice!
101 *bitmap_value = new base::StringValue(data64);
97 102
98 return true; 103 return true;
99 } 104 }
100 105
101 bool SetIconNatives::ConvertImageDataSetToBitmapValueSet( 106 bool SetIconNatives::ConvertImageDataSetToBitmapValueSet(
102 const v8::FunctionCallbackInfo<v8::Value>& args, 107 const v8::FunctionCallbackInfo<v8::Value>& args,
103 base::DictionaryValue* bitmap_set_value) { 108 base::DictionaryValue* bitmap_set_value) {
104 v8::Local<v8::Object> extension_args = args[1]->ToObject(); 109 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 = 110 v8::Local<v8::Object> image_data_set =
109 details->Get(v8::String::NewFromUtf8(args.GetIsolate(), "imageData")) 111 details->Get(v8::String::NewFromUtf8(
110 ->ToObject(); 112 args.GetIsolate(), "imageData"))->ToObject();
111 113
112 DCHECK(bitmap_set_value); 114 DCHECK(bitmap_set_value);
113 for (size_t i = 0; i < arraysize(kImageSizeKeys); i++) { 115 for (size_t i = 0; i < arraysize(kImageSizeKeys); i++) {
114 if (!image_data_set->Has( 116 if (!image_data_set->Has(
115 v8::String::NewFromUtf8(args.GetIsolate(), kImageSizeKeys[i]))) 117 v8::String::NewFromUtf8(args.GetIsolate(), kImageSizeKeys[i])))
116 continue; 118 continue;
117 v8::Local<v8::Object> image_data = 119 v8::Local<v8::Object> image_data =
118 image_data_set->Get(v8::String::NewFromUtf8(args.GetIsolate(), 120 image_data_set->Get(v8::String::NewFromUtf8(args.GetIsolate(),
119 kImageSizeKeys[i])) 121 kImageSizeKeys[i]))
120 ->ToObject(); 122 ->ToObject();
121 base::Value* image_data_bitmap = NULL; 123 base::Value* image_data_bitmap = NULL;
122 if (!ConvertImageDataToBitmapValue(image_data, &image_data_bitmap)) 124 if (!ConvertImageDataToBitmapValue(image_data, &image_data_bitmap))
123 return false; 125 return false;
124 bitmap_set_value->Set(kImageSizeKeys[i], image_data_bitmap); 126 bitmap_set_value->Set(kImageSizeKeys[i], image_data_bitmap);
125 } 127 }
126 return true; 128 return true;
127 } 129 }
128 130
129 void SetIconNatives::SetIconCommon( 131 void SetIconNatives::SetIconCommon(
130 const v8::FunctionCallbackInfo<v8::Value>& args) { 132 const v8::FunctionCallbackInfo<v8::Value>& args) {
not at google - send to devlin 2014/08/19 17:32:29 CHECK_EQ(1, args.Length()); CHECK(args[0]->IsObjec
gpdavis 2014/08/19 22:05:08 Done.
131 scoped_ptr<base::DictionaryValue> bitmap_set_value( 133 scoped_ptr<base::DictionaryValue> bitmap_set_value(
132 new base::DictionaryValue()); 134 new base::DictionaryValue());
133 if (!ConvertImageDataSetToBitmapValueSet(args, bitmap_set_value.get())) 135 if (!ConvertImageDataSetToBitmapValueSet(args, bitmap_set_value.get()))
134 return; 136 return;
135 137
136 v8::Local<v8::Object> extension_args = args[1]->ToObject(); 138 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 139
141 base::DictionaryValue* dict = new base::DictionaryValue(); 140 base::DictionaryValue* dict = new base::DictionaryValue();
not at google - send to devlin 2014/08/19 17:32:29 This now leaks |dict| since it's not having its ow
gpdavis 2014/08/19 22:05:07 Ah, gotcha. Nice catch!
142 dict->Set("imageData", bitmap_set_value.release()); 141 dict->Set("imageData", bitmap_set_value.release());
143
144 if (details->Has(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))) { 142 if (details->Has(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))) {
145 dict->SetInteger( 143 dict->SetInteger(
146 "tabId", 144 "tabId",
147 details->Get(v8::String::NewFromUtf8(args.GetIsolate(), "tabId")) 145 details->Get(v8::String::NewFromUtf8(args.GetIsolate(), "tabId"))
148 ->Int32Value()); 146 ->Int32Value());
149 } 147 }
150 148
151 base::ListValue list_value; 149 v8::Isolate* isolate = context()->v8_context()->GetIsolate();
152 list_value.Append(dict); 150 v8::Handle<v8::Value> result = v8::Object::New(isolate);
153 151 scoped_ptr<content::V8ValueConverter> converter(
154 std::string name = *v8::String::Utf8Value(args[0]); 152 content::V8ValueConverter::create());
155 int request_id = args[2]->Int32Value(); 153 result = converter->ToV8Value(dict, context()->v8_context());
156 bool has_callback = args[3]->BooleanValue(); 154 args.GetReturnValue().Set(result);
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 } 155 }
162 156
163 } // namespace extensions 157 } // namespace extensions
OLDNEW
« extensions/renderer/resources/set_icon.js ('K') | « extensions/renderer/resources/set_icon.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698