| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/renderer/extensions/schema_generated_bindings.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/command_line.h" | |
| 13 #include "base/json/json_reader.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/string_number_conversions.h" | |
| 16 #include "base/string_util.h" | |
| 17 #include "chrome/common/extensions/extension.h" | |
| 18 #include "chrome/common/extensions/extension_action.h" | |
| 19 #include "chrome/common/extensions/extension_constants.h" | |
| 20 #include "chrome/common/extensions/extension_messages.h" | |
| 21 #include "chrome/common/extensions/extension_set.h" | |
| 22 #include "chrome/common/extensions/url_pattern.h" | |
| 23 #include "chrome/common/extensions/api/extension_api.h" | |
| 24 #include "chrome/common/render_messages.h" | |
| 25 #include "chrome/renderer/chrome_render_process_observer.h" | |
| 26 #include "chrome/renderer/extensions/chrome_v8_context.h" | |
| 27 #include "chrome/renderer/extensions/chrome_v8_context_set.h" | |
| 28 #include "chrome/renderer/extensions/event_bindings.h" | |
| 29 #include "chrome/renderer/extensions/extension_dispatcher.h" | |
| 30 #include "chrome/renderer/extensions/extension_request_sender.h" | |
| 31 #include "chrome/renderer/extensions/miscellaneous_bindings.h" | |
| 32 #include "chrome/renderer/extensions/user_script_slave.h" | |
| 33 #include "content/public/renderer/render_thread.h" | |
| 34 #include "content/public/renderer/render_view.h" | |
| 35 #include "content/public/renderer/v8_value_converter.h" | |
| 36 #include "grit/common_resources.h" | |
| 37 #include "grit/renderer_resources.h" | |
| 38 #include "third_party/skia/include/core/SkBitmap.h" | |
| 39 #include "third_party/skia/include/core/SkColor.h" | |
| 40 #include "ui/base/resource/resource_bundle.h" | |
| 41 #include "v8/include/v8.h" | |
| 42 #include "webkit/glue/webkit_glue.h" | |
| 43 | |
| 44 using content::RenderThread; | |
| 45 using content::V8ValueConverter; | |
| 46 using extensions::ExtensionAPI; | |
| 47 using extensions::Feature; | |
| 48 using WebKit::WebFrame; | |
| 49 using WebKit::WebSecurityOrigin; | |
| 50 | |
| 51 namespace extensions { | |
| 52 | |
| 53 SchemaGeneratedBindings::SchemaGeneratedBindings( | |
| 54 ExtensionDispatcher* extension_dispatcher, | |
| 55 ExtensionRequestSender* request_sender) | |
| 56 : ChromeV8Extension(extension_dispatcher), | |
| 57 request_sender_(request_sender) { | |
| 58 RouteFunction("GetExtensionAPIDefinition", | |
| 59 base::Bind(&SchemaGeneratedBindings::GetExtensionAPIDefinition, | |
| 60 base::Unretained(this))); | |
| 61 RouteFunction("GetNextRequestId", | |
| 62 base::Bind(&SchemaGeneratedBindings::GetNextRequestId, | |
| 63 base::Unretained(this))); | |
| 64 RouteFunction("StartRequest", | |
| 65 base::Bind(&SchemaGeneratedBindings::StartRequest, | |
| 66 base::Unretained(this))); | |
| 67 RouteFunction("SetIconCommon", | |
| 68 base::Bind(&SchemaGeneratedBindings::SetIconCommon, | |
| 69 base::Unretained(this))); | |
| 70 } | |
| 71 | |
| 72 v8::Handle<v8::Value> SchemaGeneratedBindings::GetExtensionAPIDefinition( | |
| 73 const v8::Arguments& args) { | |
| 74 ChromeV8Context* v8_context = | |
| 75 extension_dispatcher()->v8_context_set().GetCurrent(); | |
| 76 CHECK(v8_context); | |
| 77 | |
| 78 // TODO(kalman): This is being calculated twice, first in | |
| 79 // ExtensionDispatcher then again here. It might as well be a property of | |
| 80 // ChromeV8Context, however, this would require making ChromeV8Context take | |
| 81 // an Extension rather than an extension ID. In itself this is fine, | |
| 82 // however it does not play correctly with the "IsTestExtensionId" checks. | |
| 83 // We need to remove that first. | |
| 84 scoped_ptr<std::set<std::string> > apis; | |
| 85 | |
| 86 const std::string& extension_id = v8_context->extension_id(); | |
| 87 if (extension_dispatcher()->IsTestExtensionId(extension_id)) { | |
| 88 apis.reset(new std::set<std::string>()); | |
| 89 // The minimal set of APIs that tests need. | |
| 90 apis->insert("extension"); | |
| 91 } else { | |
| 92 apis = ExtensionAPI::GetSharedInstance()->GetAPIsForContext( | |
| 93 v8_context->context_type(), | |
| 94 extension_dispatcher()->extensions()->GetByID(extension_id), | |
| 95 UserScriptSlave::GetDataSourceURLForFrame(v8_context->web_frame())); | |
| 96 } | |
| 97 | |
| 98 return extension_dispatcher()->v8_schema_registry()->GetSchemas(*apis); | |
| 99 } | |
| 100 | |
| 101 v8::Handle<v8::Value> SchemaGeneratedBindings::GetNextRequestId( | |
| 102 const v8::Arguments& args) { | |
| 103 static int next_request_id = 0; | |
| 104 return v8::Integer::New(next_request_id++); | |
| 105 } | |
| 106 | |
| 107 v8::Handle<v8::Value> SchemaGeneratedBindings::StartRequestCommon( | |
| 108 const v8::Arguments& args, ListValue* value_args) { | |
| 109 std::string name = *v8::String::AsciiValue(args[0]); | |
| 110 int request_id = args[2]->Int32Value(); | |
| 111 bool has_callback = args[3]->BooleanValue(); | |
| 112 bool for_io_thread = args[4]->BooleanValue(); | |
| 113 | |
| 114 request_sender_->StartRequest(name, | |
| 115 request_id, | |
| 116 has_callback, | |
| 117 for_io_thread, | |
| 118 value_args); | |
| 119 return v8::Undefined(); | |
| 120 } | |
| 121 | |
| 122 // Starts an API request to the browser, with an optional callback. The | |
| 123 // callback will be dispatched to EventBindings::HandleResponse. | |
| 124 v8::Handle<v8::Value> SchemaGeneratedBindings::StartRequest( | |
| 125 const v8::Arguments& args) { | |
| 126 std::string str_args = *v8::String::Utf8Value(args[1]); | |
| 127 base::JSONReader reader; | |
| 128 scoped_ptr<Value> value_args; | |
| 129 value_args.reset(reader.JsonToValue(str_args, false, false)); | |
| 130 | |
| 131 // Since we do the serialization in the v8 extension, we should always get | |
| 132 // valid JSON. | |
| 133 if (!value_args.get() || !value_args->IsType(Value::TYPE_LIST)) { | |
| 134 NOTREACHED() << "Invalid JSON passed to StartRequest."; | |
| 135 return v8::Undefined(); | |
| 136 } | |
| 137 | |
| 138 return StartRequestCommon(args, static_cast<ListValue*>(value_args.get())); | |
| 139 } | |
| 140 | |
| 141 bool SchemaGeneratedBindings::ConvertImageDataToBitmapValue( | |
| 142 const v8::Arguments& args, Value** bitmap_value) { | |
| 143 v8::Local<v8::Object> extension_args = args[1]->ToObject(); | |
| 144 v8::Local<v8::Object> details = | |
| 145 extension_args->Get(v8::String::New("0"))->ToObject(); | |
| 146 v8::Local<v8::Object> image_data = | |
| 147 details->Get(v8::String::New("imageData"))->ToObject(); | |
| 148 v8::Local<v8::Object> data = | |
| 149 image_data->Get(v8::String::New("data"))->ToObject(); | |
| 150 int width = image_data->Get(v8::String::New("width"))->Int32Value(); | |
| 151 int height = image_data->Get(v8::String::New("height"))->Int32Value(); | |
| 152 | |
| 153 int data_length = data->Get(v8::String::New("length"))->Int32Value(); | |
| 154 if (data_length != 4 * width * height) { | |
| 155 NOTREACHED() << "Invalid argument to setIcon. Expecting ImageData."; | |
| 156 return false; | |
| 157 } | |
| 158 | |
| 159 SkBitmap bitmap; | |
| 160 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
| 161 bitmap.allocPixels(); | |
| 162 bitmap.eraseARGB(0, 0, 0, 0); | |
| 163 | |
| 164 uint32_t* pixels = bitmap.getAddr32(0, 0); | |
| 165 for (int t = 0; t < width*height; t++) { | |
| 166 // |data| is RGBA, pixels is ARGB. | |
| 167 pixels[t] = SkPreMultiplyColor( | |
| 168 ((data->Get(v8::Integer::New(4*t + 3))->Int32Value() & 0xFF) << 24) | | |
| 169 ((data->Get(v8::Integer::New(4*t + 0))->Int32Value() & 0xFF) << 16) | | |
| 170 ((data->Get(v8::Integer::New(4*t + 1))->Int32Value() & 0xFF) << 8) | | |
| 171 ((data->Get(v8::Integer::New(4*t + 2))->Int32Value() & 0xFF) << 0)); | |
| 172 } | |
| 173 | |
| 174 // Construct the Value object. | |
| 175 IPC::Message bitmap_pickle; | |
| 176 IPC::WriteParam(&bitmap_pickle, bitmap); | |
| 177 *bitmap_value = base::BinaryValue::CreateWithCopiedBuffer( | |
| 178 static_cast<const char*>(bitmap_pickle.data()), bitmap_pickle.size()); | |
| 179 | |
| 180 return true; | |
| 181 } | |
| 182 | |
| 183 v8::Handle<v8::Value> SchemaGeneratedBindings::SetIconCommon( | |
| 184 const v8::Arguments& args) { | |
| 185 Value* bitmap_value = NULL; | |
| 186 if (!ConvertImageDataToBitmapValue(args, &bitmap_value)) | |
| 187 return v8::Undefined(); | |
| 188 | |
| 189 v8::Local<v8::Object> extension_args = args[1]->ToObject(); | |
| 190 v8::Local<v8::Object> details = | |
| 191 extension_args->Get(v8::String::New("0"))->ToObject(); | |
| 192 | |
| 193 DictionaryValue* dict = new DictionaryValue(); | |
| 194 dict->Set("imageData", bitmap_value); | |
| 195 | |
| 196 if (details->Has(v8::String::New("tabId"))) { | |
| 197 dict->SetInteger("tabId", | |
| 198 details->Get(v8::String::New("tabId"))->Int32Value()); | |
| 199 } | |
| 200 | |
| 201 ListValue list_value; | |
| 202 list_value.Append(dict); | |
| 203 | |
| 204 return StartRequestCommon(args, &list_value); | |
| 205 } | |
| 206 | |
| 207 } // namespace extensions | |
| OLD | NEW |