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

Side by Side Diff: extensions/renderer/dispatcher.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
« no previous file with comments | « extensions/renderer/console.cc ('k') | extensions/renderer/event_bindings.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/dispatcher.h" 5 #include "extensions/renderer/dispatcher.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/debug/alias.h" 10 #include "base/debug/alias.h"
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 #include "extensions/renderer/script_context.h" 73 #include "extensions/renderer/script_context.h"
74 #include "extensions/renderer/script_context_set.h" 74 #include "extensions/renderer/script_context_set.h"
75 #include "extensions/renderer/script_injection.h" 75 #include "extensions/renderer/script_injection.h"
76 #include "extensions/renderer/script_injection_manager.h" 76 #include "extensions/renderer/script_injection_manager.h"
77 #include "extensions/renderer/send_request_natives.h" 77 #include "extensions/renderer/send_request_natives.h"
78 #include "extensions/renderer/set_icon_natives.h" 78 #include "extensions/renderer/set_icon_natives.h"
79 #include "extensions/renderer/test_features_native_handler.h" 79 #include "extensions/renderer/test_features_native_handler.h"
80 #include "extensions/renderer/user_gestures_native_handler.h" 80 #include "extensions/renderer/user_gestures_native_handler.h"
81 #include "extensions/renderer/utils_native_handler.h" 81 #include "extensions/renderer/utils_native_handler.h"
82 #include "extensions/renderer/v8_context_native_handler.h" 82 #include "extensions/renderer/v8_context_native_handler.h"
83 #include "extensions/renderer/v8_maybe_helpers.h"
83 #include "grit/extensions_renderer_resources.h" 84 #include "grit/extensions_renderer_resources.h"
84 #include "third_party/WebKit/public/platform/WebString.h" 85 #include "third_party/WebKit/public/platform/WebString.h"
85 #include "third_party/WebKit/public/platform/WebURLRequest.h" 86 #include "third_party/WebKit/public/platform/WebURLRequest.h"
86 #include "third_party/WebKit/public/web/WebCustomElement.h" 87 #include "third_party/WebKit/public/web/WebCustomElement.h"
87 #include "third_party/WebKit/public/web/WebDataSource.h" 88 #include "third_party/WebKit/public/web/WebDataSource.h"
88 #include "third_party/WebKit/public/web/WebDocument.h" 89 #include "third_party/WebKit/public/web/WebDocument.h"
89 #include "third_party/WebKit/public/web/WebFrame.h" 90 #include "third_party/WebKit/public/web/WebFrame.h"
90 #include "third_party/WebKit/public/web/WebLocalFrame.h" 91 #include "third_party/WebKit/public/web/WebLocalFrame.h"
91 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" 92 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
92 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" 93 #include "third_party/WebKit/public/web/WebScopedUserGesture.h"
(...skipping 24 matching lines...) Expand all
117 static const char kEventDispatchFunction[] = "dispatchEvent"; 118 static const char kEventDispatchFunction[] = "dispatchEvent";
118 static const char kOnSuspendEvent[] = "runtime.onSuspend"; 119 static const char kOnSuspendEvent[] = "runtime.onSuspend";
119 static const char kOnSuspendCanceledEvent[] = "runtime.onSuspendCanceled"; 120 static const char kOnSuspendCanceledEvent[] = "runtime.onSuspendCanceled";
120 121
121 // Returns the global value for "chrome" from |context|. If one doesn't exist 122 // Returns the global value for "chrome" from |context|. If one doesn't exist
122 // creates a new object for it. 123 // creates a new object for it.
123 // 124 //
124 // Note that this isn't necessarily an object, since webpages can write, for 125 // Note that this isn't necessarily an object, since webpages can write, for
125 // example, "window.chrome = true". 126 // example, "window.chrome = true".
126 v8::Local<v8::Value> GetOrCreateChrome(ScriptContext* context) { 127 v8::Local<v8::Value> GetOrCreateChrome(ScriptContext* context) {
127 v8::Local<v8::String> chrome_string( 128 v8::Local<v8::Context> v8_context(context->v8_context());
128 v8::String::NewFromUtf8(context->isolate(), "chrome")); 129 v8::Local<v8::String> chrome_string(ToV8String(context->isolate(), "chrome"));
129 v8::Local<v8::Object> global(context->v8_context()->Global()); 130 v8::Local<v8::Object> global(v8_context->Global());
130 v8::Local<v8::Value> chrome(global->Get(chrome_string)); 131 v8::Local<v8::Value> chrome;
131 if (chrome->IsUndefined()) { 132 if (!global->Get(v8_context, chrome_string).ToLocal(&chrome) ||
133 chrome->IsUndefined()) {
132 chrome = v8::Object::New(context->isolate()); 134 chrome = v8::Object::New(context->isolate());
133 global->Set(chrome_string, chrome); 135 SetProperty(v8_context, global, chrome_string, chrome);
134 } 136 }
135 return chrome; 137 return chrome;
136 } 138 }
137 139
138 // Returns |value| cast to an object if possible, else an empty handle. 140 // Returns |value| cast to an object if possible, else an empty handle.
139 v8::Local<v8::Object> AsObjectOrEmpty(v8::Local<v8::Value> value) { 141 v8::Local<v8::Object> AsObjectOrEmpty(v8::Local<v8::Value> value) {
140 return value->IsObject() ? value.As<v8::Object>() : v8::Local<v8::Object>(); 142 return value->IsObject() ? value.As<v8::Object>() : v8::Local<v8::Object>();
141 } 143 }
142 144
143 // Calls a method |method_name| in a module |module_name| belonging to the 145 // Calls a method |method_name| in a module |module_name| belonging to the
(...skipping 1043 matching lines...) Expand 10 before | Expand all | Expand 10 after
1187 ScriptContext* context) { 1189 ScriptContext* context) {
1188 std::string bind_name; 1190 std::string bind_name;
1189 v8::Local<v8::Object> bind_object = 1191 v8::Local<v8::Object> bind_object =
1190 GetOrCreateBindObjectIfAvailable(api_name, &bind_name, context); 1192 GetOrCreateBindObjectIfAvailable(api_name, &bind_name, context);
1191 1193
1192 // Empty if the bind object failed to be created, probably because the 1194 // Empty if the bind object failed to be created, probably because the
1193 // extension overrode chrome with a non-object, e.g. window.chrome = true. 1195 // extension overrode chrome with a non-object, e.g. window.chrome = true.
1194 if (bind_object.IsEmpty()) 1196 if (bind_object.IsEmpty())
1195 return; 1197 return;
1196 1198
1199 v8::Local<v8::Context> v8_context = context->v8_context();
1197 v8::Local<v8::String> v8_bind_name = 1200 v8::Local<v8::String> v8_bind_name =
1198 v8::String::NewFromUtf8(context->isolate(), bind_name.c_str()); 1201 ToV8String(context->isolate(), bind_name.c_str());
1199 if (bind_object->HasRealNamedProperty(v8_bind_name)) { 1202 if (CheckV8Call(
1203 bind_object->HasRealNamedProperty(v8_context, v8_bind_name))) {
1200 // The bind object may already have the property if the API has been 1204 // The bind object may already have the property if the API has been
1201 // registered before (or if the extension has put something there already, 1205 // registered before (or if the extension has put something there already,
1202 // but, whatevs). 1206 // but, whatevs).
1203 // 1207 //
1204 // In the former case, we need to re-register the bindings for the APIs 1208 // In the former case, we need to re-register the bindings for the APIs
1205 // which the extension now has permissions for (if any), but not touch any 1209 // which the extension now has permissions for (if any), but not touch any
1206 // others so that we don't destroy state such as event listeners. 1210 // others so that we don't destroy state such as event listeners.
1207 // 1211 //
1208 // TODO(kalman): Only register available APIs to make this all moot. 1212 // TODO(kalman): Only register available APIs to make this all moot.
1209 if (bind_object->HasRealNamedCallbackProperty(v8_bind_name)) 1213 if (CheckV8Call(bind_object->HasRealNamedCallbackProperty(v8_context,
1214 v8_bind_name)))
1210 return; // lazy binding still there, nothing to do 1215 return; // lazy binding still there, nothing to do
1211 if (bind_object->Get(v8_bind_name)->IsObject()) 1216 v8::Local<v8::Value> bind;
1217 if (bind_object->Get(v8_context, v8_bind_name).ToLocal(&bind) &&
1218 bind->IsObject())
1212 return; // binding has already been fully installed 1219 return; // binding has already been fully installed
1213 } 1220 }
1214 1221
1215 ModuleSystem* module_system = context->module_system(); 1222 ModuleSystem* module_system = context->module_system();
1216 if (!source_map_.Contains(api_name)) { 1223 if (!source_map_.Contains(api_name)) {
1217 module_system->RegisterNativeHandler( 1224 module_system->RegisterNativeHandler(
1218 api_name, 1225 api_name,
1219 scoped_ptr<NativeHandler>(new BindingGeneratingNativeHandler( 1226 scoped_ptr<NativeHandler>(new BindingGeneratingNativeHandler(
1220 module_system, api_name, "binding"))); 1227 module_system, api_name, "binding")));
1221 module_system->SetNativeLazyField( 1228 module_system->SetNativeLazyField(
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1301 if (extension && extension->is_platform_app()) 1308 if (extension && extension->is_platform_app())
1302 return true; 1309 return true;
1303 } 1310 }
1304 return false; 1311 return false;
1305 } 1312 }
1306 1313
1307 v8::Local<v8::Object> Dispatcher::GetOrCreateObject( 1314 v8::Local<v8::Object> Dispatcher::GetOrCreateObject(
1308 const v8::Local<v8::Object>& object, 1315 const v8::Local<v8::Object>& object,
1309 const std::string& field, 1316 const std::string& field,
1310 v8::Isolate* isolate) { 1317 v8::Isolate* isolate) {
1311 v8::Local<v8::String> key = v8::String::NewFromUtf8(isolate, field.c_str()); 1318 DCHECK(field.size() < v8::String::kMaxLength);
1319 v8::Local<v8::String> key = ToV8String(isolate, field.c_str());
1320 v8::Local<v8::Context> context = isolate->GetCurrentContext();
1312 // If the object has a callback property, it is assumed it is an unavailable 1321 // If the object has a callback property, it is assumed it is an unavailable
1313 // API, so it is safe to delete. This is checked before GetOrCreateObject is 1322 // API, so it is safe to delete. This is checked before GetOrCreateObject is
1314 // called. 1323 // called.
1315 if (object->HasRealNamedCallbackProperty(key)) { 1324 if (CheckV8Call(object->HasRealNamedCallbackProperty(context, key))) {
1316 object->Delete(key); 1325 object->Delete(context, key);
1317 } else if (object->HasRealNamedProperty(key)) { 1326 } else if (CheckV8Call(object->HasRealNamedProperty(context, key))) {
1318 v8::Local<v8::Value> value = object->Get(key); 1327 v8::Local<v8::Value> value = object->Get(context, key).ToLocalChecked();
1319 CHECK(value->IsObject()); 1328 CHECK(value->IsObject());
1320 return v8::Local<v8::Object>::Cast(value); 1329 return v8::Local<v8::Object>::Cast(value);
1321 } 1330 }
1322 1331
1323 v8::Local<v8::Object> new_object = v8::Object::New(isolate); 1332 v8::Local<v8::Object> new_object = v8::Object::New(isolate);
1324 object->Set(key, new_object); 1333 SetProperty(context, object, key, new_object);
1325 return new_object; 1334 return new_object;
1326 } 1335 }
1327 1336
1328 v8::Local<v8::Object> Dispatcher::GetOrCreateBindObjectIfAvailable( 1337 v8::Local<v8::Object> Dispatcher::GetOrCreateBindObjectIfAvailable(
1329 const std::string& api_name, 1338 const std::string& api_name,
1330 std::string* bind_name, 1339 std::string* bind_name,
1331 ScriptContext* context) { 1340 ScriptContext* context) {
1332 std::vector<std::string> split; 1341 std::vector<std::string> split;
1333 base::SplitString(api_name, '.', &split); 1342 base::SplitString(api_name, '.', &split);
1334 1343
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1414 // The "guestViewDeny" module must always be loaded last. It registers 1423 // The "guestViewDeny" module must always be loaded last. It registers
1415 // error-providing custom elements for the GuestView types that are not 1424 // error-providing custom elements for the GuestView types that are not
1416 // available, and thus all of those types must have been checked and loaded 1425 // available, and thus all of those types must have been checked and loaded
1417 // (or not loaded) beforehand. 1426 // (or not loaded) beforehand.
1418 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) { 1427 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) {
1419 module_system->Require("guestViewDeny"); 1428 module_system->Require("guestViewDeny");
1420 } 1429 }
1421 } 1430 }
1422 1431
1423 } // namespace extensions 1432 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/console.cc ('k') | extensions/renderer/event_bindings.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698