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

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

Issue 2494653005: Support API aliases (Closed)
Patch Set: rebase Created 4 years, 1 month 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/dispatcher.h" 5 #include "extensions/renderer/dispatcher.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 1342 matching lines...) Expand 10 before | Expand all | Expand 10 after
1353 // TODO(kalman): Make the bindings registration have zero overhead then run 1353 // TODO(kalman): Make the bindings registration have zero overhead then run
1354 // the same code regardless of context type. 1354 // the same code regardless of context type.
1355 switch (context->context_type()) { 1355 switch (context->context_type()) {
1356 case Feature::UNSPECIFIED_CONTEXT: 1356 case Feature::UNSPECIFIED_CONTEXT:
1357 case Feature::WEB_PAGE_CONTEXT: 1357 case Feature::WEB_PAGE_CONTEXT:
1358 case Feature::BLESSED_WEB_PAGE_CONTEXT: 1358 case Feature::BLESSED_WEB_PAGE_CONTEXT:
1359 // Hard-code registration of any APIs that are exposed to webpage-like 1359 // Hard-code registration of any APIs that are exposed to webpage-like
1360 // contexts, because it's too expensive to run the full bindings code. 1360 // contexts, because it's too expensive to run the full bindings code.
1361 // All of the same permission checks will still apply. 1361 // All of the same permission checks will still apply.
1362 if (context->GetAvailability("app").is_available()) 1362 if (context->GetAvailability("app").is_available())
1363 RegisterBinding("app", context); 1363 RegisterBinding("app", "app", context);
1364 if (context->GetAvailability("webstore").is_available()) 1364 if (context->GetAvailability("webstore").is_available())
1365 RegisterBinding("webstore", context); 1365 RegisterBinding("webstore", "webstore", context);
1366 if (context->GetAvailability("dashboardPrivate").is_available()) 1366 if (context->GetAvailability("dashboardPrivate").is_available())
1367 RegisterBinding("dashboardPrivate", context); 1367 RegisterBinding("dashboardPrivate", "dashboardPrivate", context);
1368 if (IsRuntimeAvailableToContext(context)) 1368 if (IsRuntimeAvailableToContext(context))
1369 RegisterBinding("runtime", context); 1369 RegisterBinding("runtime", "runtime", context);
1370 UpdateContentCapabilities(context); 1370 UpdateContentCapabilities(context);
1371 break; 1371 break;
1372 1372
1373 case Feature::SERVICE_WORKER_CONTEXT: 1373 case Feature::SERVICE_WORKER_CONTEXT:
1374 DCHECK(ExtensionsClient::Get() 1374 DCHECK(ExtensionsClient::Get()
1375 ->ExtensionAPIEnabledInExtensionServiceWorkers()); 1375 ->ExtensionAPIEnabledInExtensionServiceWorkers());
1376 // Intentional fallthrough. 1376 // Intentional fallthrough.
1377 case Feature::BLESSED_EXTENSION_CONTEXT: 1377 case Feature::BLESSED_EXTENSION_CONTEXT:
1378 case Feature::UNBLESSED_EXTENSION_CONTEXT: 1378 case Feature::UNBLESSED_EXTENSION_CONTEXT:
1379 case Feature::CONTENT_SCRIPT_CONTEXT: 1379 case Feature::CONTENT_SCRIPT_CONTEXT:
(...skipping 13 matching lines...) Expand all
1393 if (api_feature_provider->GetParent(map_entry.second.get()) != nullptr) 1393 if (api_feature_provider->GetParent(map_entry.second.get()) != nullptr)
1394 continue; 1394 continue;
1395 1395
1396 // Skip chrome.test if this isn't a test. 1396 // Skip chrome.test if this isn't a test.
1397 if (map_entry.first == "test" && 1397 if (map_entry.first == "test" &&
1398 !base::CommandLine::ForCurrentProcess()->HasSwitch( 1398 !base::CommandLine::ForCurrentProcess()->HasSwitch(
1399 ::switches::kTestType)) { 1399 ::switches::kTestType)) {
1400 continue; 1400 continue;
1401 } 1401 }
1402 1402
1403 if (context->IsAnyFeatureAvailableToContext(*map_entry.second)) { 1403 if (context->IsAnyFeatureAvailableToContext(*map_entry.second, false)) {
1404 // Check if the API feature is indeed an alias. If it is, the API
1405 // should use source API bindings as its own.
1406 std::string source = map_entry.second->GetSource();
1404 // TODO(lazyboy): RegisterBinding() uses |source_map_|, any thread 1407 // TODO(lazyboy): RegisterBinding() uses |source_map_|, any thread
1405 // safety issue? 1408 // safety issue?
1406 RegisterBinding(map_entry.first, context); 1409 RegisterBinding(source.empty() ? map_entry.first : source,
1410 map_entry.first, context);
1407 } 1411 }
1408 } 1412 }
1409 break; 1413 break;
1410 } 1414 }
1411 } 1415 }
1412 } 1416 }
1413 1417
1414 void Dispatcher::RegisterBinding(const std::string& api_name, 1418 void Dispatcher::RegisterBinding(const std::string& api_name,
1419 const std::string& api_bind_name,
1415 ScriptContext* context) { 1420 ScriptContext* context) {
1416 std::string bind_name; 1421 std::string bind_name;
1417 v8::Local<v8::Object> bind_object = 1422 v8::Local<v8::Object> bind_object =
1418 GetOrCreateBindObjectIfAvailable(api_name, &bind_name, context); 1423 GetOrCreateBindObjectIfAvailable(api_bind_name, &bind_name, context);
1419 1424
1420 // Empty if the bind object failed to be created, probably because the 1425 // Empty if the bind object failed to be created, probably because the
1421 // extension overrode chrome with a non-object, e.g. window.chrome = true. 1426 // extension overrode chrome with a non-object, e.g. window.chrome = true.
1422 if (bind_object.IsEmpty()) 1427 if (bind_object.IsEmpty())
1423 return; 1428 return;
1424 1429
1425 v8::Local<v8::String> v8_bind_name = 1430 v8::Local<v8::String> v8_bind_name =
1426 v8::String::NewFromUtf8(context->isolate(), bind_name.c_str()); 1431 v8::String::NewFromUtf8(context->isolate(), bind_name.c_str());
1427 if (bind_object->HasRealNamedProperty(v8_bind_name)) { 1432 if (bind_object->HasRealNamedProperty(v8_bind_name)) {
1428 // The bind object may already have the property if the API has been 1433 // The bind object may already have the property if the API has been
1429 // registered before (or if the extension has put something there already, 1434 // registered before (or if the extension has put something there already,
1430 // but, whatevs). 1435 // but, whatevs).
1431 // 1436 //
1432 // In the former case, we need to re-register the bindings for the APIs 1437 // In the former case, we need to re-register the bindings for the APIs
1433 // which the extension now has permissions for (if any), but not touch any 1438 // which the extension now has permissions for (if any), but not touch any
1434 // others so that we don't destroy state such as event listeners. 1439 // others so that we don't destroy state such as event listeners.
1435 // 1440 //
1436 // TODO(kalman): Only register available APIs to make this all moot. 1441 // TODO(kalman): Only register available APIs to make this all moot.
1437 if (bind_object->HasRealNamedCallbackProperty(v8_bind_name)) 1442 if (bind_object->HasRealNamedCallbackProperty(v8_bind_name))
1438 return; // lazy binding still there, nothing to do 1443 return; // lazy binding still there, nothing to do
1439 if (bind_object->Get(v8_bind_name)->IsObject()) 1444 if (bind_object->Get(v8_bind_name)->IsObject())
1440 return; // binding has already been fully installed 1445 return; // binding has already been fully installed
1441 } 1446 }
1442 1447
1443 ModuleSystem* module_system = context->module_system(); 1448 ModuleSystem* module_system = context->module_system();
1444 if (!source_map_.Contains(api_name)) { 1449 if (!source_map_.Contains(api_name)) {
1445 module_system->RegisterNativeHandler( 1450 module_system->RegisterNativeHandler(
1446 api_name, 1451 api_bind_name,
1447 std::unique_ptr<NativeHandler>( 1452 std::unique_ptr<NativeHandler>(
1448 new BindingGeneratingNativeHandler(context, api_name, "binding"))); 1453 new BindingGeneratingNativeHandler(context, api_name, "binding")));
1449 module_system->SetNativeLazyField( 1454 module_system->SetNativeLazyField(bind_object, bind_name, api_bind_name,
1450 bind_object, bind_name, api_name, "binding"); 1455 "binding");
1451 } else { 1456 } else {
1452 module_system->SetLazyField(bind_object, bind_name, api_name, "binding"); 1457 module_system->SetLazyField(bind_object, bind_name, api_name, "binding");
1453 } 1458 }
1454 } 1459 }
1455 1460
1456 // NOTE: please use the naming convention "foo_natives" for these. 1461 // NOTE: please use the naming convention "foo_natives" for these.
1457 void Dispatcher::RegisterNativeHandlers(ModuleSystem* module_system, 1462 void Dispatcher::RegisterNativeHandlers(ModuleSystem* module_system,
1458 ScriptContext* context, 1463 ScriptContext* context,
1459 RequestSender* request_sender, 1464 RequestSender* request_sender,
1460 V8SchemaRegistry* v8_schema_registry) { 1465 V8SchemaRegistry* v8_schema_registry) {
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1576 // If app.window is available and app is not, delete app and install 1581 // If app.window is available and app is not, delete app and install
1577 // app.window on a new object so app does not have to be loaded. 1582 // app.window on a new object so app does not have to be loaded.
1578 const FeatureProvider* api_feature_provider = 1583 const FeatureProvider* api_feature_provider =
1579 FeatureProvider::GetAPIFeatures(); 1584 FeatureProvider::GetAPIFeatures();
1580 std::string ancestor_name; 1585 std::string ancestor_name;
1581 bool only_ancestor_available = false; 1586 bool only_ancestor_available = false;
1582 1587
1583 for (size_t i = 0; i < split.size() - 1; ++i) { 1588 for (size_t i = 0; i < split.size() - 1; ++i) {
1584 ancestor_name += (i ? "." : "") + split[i]; 1589 ancestor_name += (i ? "." : "") + split[i];
1585 if (api_feature_provider->GetFeature(ancestor_name) && 1590 if (api_feature_provider->GetFeature(ancestor_name) &&
1586 context->GetAvailability(ancestor_name).is_available() && 1591 context->GetAvailability(ancestor_name, false).is_available() &&
1587 !context->GetAvailability(api_name).is_available()) { 1592 !context->GetAvailability(api_name, false).is_available()) {
1588 only_ancestor_available = true; 1593 only_ancestor_available = true;
1589 break; 1594 break;
1590 } 1595 }
1591 1596
1592 if (bind_object.IsEmpty()) { 1597 if (bind_object.IsEmpty()) {
1593 bind_object = AsObjectOrEmpty(GetOrCreateChrome(context)); 1598 bind_object = AsObjectOrEmpty(GetOrCreateChrome(context));
1594 if (bind_object.IsEmpty()) 1599 if (bind_object.IsEmpty())
1595 return v8::Local<v8::Object>(); 1600 return v8::Local<v8::Object>();
1596 } 1601 }
1597 bind_object = GetOrCreateObject(bind_object, split[i], context->isolate()); 1602 bind_object = GetOrCreateObject(bind_object, split[i], context->isolate());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1650 // The "guestViewDeny" module must always be loaded last. It registers 1655 // The "guestViewDeny" module must always be loaded last. It registers
1651 // error-providing custom elements for the GuestView types that are not 1656 // error-providing custom elements for the GuestView types that are not
1652 // available, and thus all of those types must have been checked and loaded 1657 // available, and thus all of those types must have been checked and loaded
1653 // (or not loaded) beforehand. 1658 // (or not loaded) beforehand.
1654 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) { 1659 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) {
1655 module_system->Require("guestViewDeny"); 1660 module_system->Require("guestViewDeny");
1656 } 1661 }
1657 } 1662 }
1658 1663
1659 } // namespace extensions 1664 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698