OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/renderer/extensions/dispatcher.h" | 5 #include "chrome/renderer/extensions/dispatcher.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/debug/alias.h" | 9 #include "base/debug/alias.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
617 | 617 |
618 v8::Handle<v8::Object> new_object = v8::Object::New(); | 618 v8::Handle<v8::Object> new_object = v8::Object::New(); |
619 object->Set(key, new_object); | 619 object->Set(key, new_object); |
620 return new_object; | 620 return new_object; |
621 } | 621 } |
622 | 622 |
623 void Dispatcher::AddOrRemoveBindings(ChromeV8Context* context) { | 623 void Dispatcher::AddOrRemoveBindings(ChromeV8Context* context) { |
624 v8::HandleScope handle_scope; | 624 v8::HandleScope handle_scope; |
625 v8::Context::Scope context_scope(context->v8_context()); | 625 v8::Context::Scope context_scope(context->v8_context()); |
626 | 626 |
627 std::set<std::string> apis = | 627 FeatureProvider* feature_provider = BaseFeatureProvider::GetByName("api"); |
628 ExtensionAPI::GetSharedInstance()->GetAllAPINames(); | 628 const std::vector<std::string>& apis = feature_provider->GetAllFeatureNames(); |
629 for (std::set<std::string>::iterator it = apis.begin(); | 629 for (std::vector<std::string>::const_iterator it = apis.begin(); |
630 it != apis.end(); ++it) { | 630 it != apis.end(); ++it) { |
631 const std::string& api_name = *it; | 631 const std::string& api_name = *it; |
632 Feature* feature = feature_provider->GetFeature(api_name); | |
633 DCHECK(feature); | |
634 if (feature->IsInternal()) | |
635 continue; | |
636 | |
637 // If this API name has parent features, then this must be a function or | |
638 // event, so we should not register. | |
639 Feature* parent_feature = feature; | |
640 bool parent_feature_available = false; | |
641 while ((parent_feature = feature_provider->GetParent(parent_feature))) { | |
not at google - send to devlin
2013/06/13 22:23:25
maybe this should be a for loop i.e. for (Feature*
cduvall
2013/06/13 22:43:08
Done.
| |
642 if (context->IsAnyFeatureAvailableToContext(parent_feature->name())) { | |
643 parent_feature_available = true; | |
644 break; | |
645 } | |
646 } | |
647 if (parent_feature_available) | |
648 continue; | |
649 | |
632 if (!context->IsAnyFeatureAvailableToContext(api_name)) { | 650 if (!context->IsAnyFeatureAvailableToContext(api_name)) { |
633 DeregisterBinding(api_name, context); | 651 DeregisterBinding(api_name, context); |
634 continue; | 652 continue; |
635 } | 653 } |
636 | 654 |
637 Feature* feature = | |
638 BaseFeatureProvider::GetByName("api")->GetFeature(api_name); | |
639 if (feature && feature->IsInternal()) | |
640 continue; | |
641 | |
642 RegisterBinding(api_name, context); | 655 RegisterBinding(api_name, context); |
643 } | 656 } |
644 } | 657 } |
645 | 658 |
646 void Dispatcher::DeregisterBinding(const std::string& api_name, | 659 void Dispatcher::DeregisterBinding(const std::string& api_name, |
647 ChromeV8Context* context) { | 660 ChromeV8Context* context) { |
648 std::string bind_name; | 661 std::string bind_name; |
649 v8::Handle<v8::Object> bind_object = | 662 v8::Handle<v8::Object> bind_object = |
650 GetOrCreateBindObjectIfAvailable(api_name, &bind_name, context); | 663 GetOrCreateBindObjectIfAvailable(api_name, &bind_name, context); |
651 v8::Handle<v8::String> v8_bind_name = v8::String::New(bind_name.c_str()); | 664 v8::Handle<v8::String> v8_bind_name = v8::String::New(bind_name.c_str()); |
(...skipping 13 matching lines...) Expand all Loading... | |
665 // Check if this API has an ancestor. If the API's ancestor is available and | 678 // Check if this API has an ancestor. If the API's ancestor is available and |
666 // the API is not available, don't install the bindings for this API. If | 679 // the API is not available, don't install the bindings for this API. If |
667 // the API is available and its ancestor is not, delete the ancestor and | 680 // the API is available and its ancestor is not, delete the ancestor and |
668 // install the bindings for the API. This is to prevent loading the ancestor | 681 // install the bindings for the API. This is to prevent loading the ancestor |
669 // API schema if it will not be needed. | 682 // API schema if it will not be needed. |
670 // | 683 // |
671 // For example: | 684 // For example: |
672 // If app is available and app.window is not, just install app. | 685 // If app is available and app.window is not, just install app. |
673 // If app.window is available and app is not, delete app and install | 686 // If app.window is available and app is not, delete app and install |
674 // app.window on a new object so app does not have to be loaded. | 687 // app.window on a new object so app does not have to be loaded. |
688 FeatureProvider* feature_provider = BaseFeatureProvider::GetByName("api"); | |
675 std::string ancestor_name; | 689 std::string ancestor_name; |
676 bool only_ancestor_available = false; | 690 bool only_ancestor_available = false; |
677 | 691 |
678 for (size_t i = 0; i < split.size() - 1; ++i) { | 692 for (size_t i = 0; i < split.size() - 1; ++i) { |
679 ancestor_name += (i ? ".": "") + split[i]; | 693 ancestor_name += (i ? ".": "") + split[i]; |
680 if (!ancestor_name.empty() && | 694 if (feature_provider->GetFeature(ancestor_name) && |
681 context->GetAvailability(ancestor_name).is_available() && | 695 context->GetAvailability(ancestor_name).is_available() && |
682 !context->GetAvailability(api_name).is_available()) { | 696 !context->GetAvailability(api_name).is_available()) { |
683 only_ancestor_available = true; | 697 only_ancestor_available = true; |
684 break; | 698 break; |
685 } | 699 } |
686 | 700 |
687 if (bind_object.IsEmpty()) { | 701 if (bind_object.IsEmpty()) { |
688 bind_object = AsObjectOrEmpty(GetOrCreateChrome(context)); | 702 bind_object = AsObjectOrEmpty(GetOrCreateChrome(context)); |
689 if (bind_object.IsEmpty()) | 703 if (bind_object.IsEmpty()) |
690 return v8::Handle<v8::Object>(); | 704 return v8::Handle<v8::Object>(); |
(...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1356 DLOG(ERROR) << "Not in a v8::Context"; | 1370 DLOG(ERROR) << "Not in a v8::Context"; |
1357 return false; | 1371 return false; |
1358 } | 1372 } |
1359 | 1373 |
1360 if (!context->extension()) { | 1374 if (!context->extension()) { |
1361 v8::ThrowException( | 1375 v8::ThrowException( |
1362 v8::Exception::Error(v8::String::New("Not in an extension."))); | 1376 v8::Exception::Error(v8::String::New("Not in an extension."))); |
1363 return false; | 1377 return false; |
1364 } | 1378 } |
1365 | 1379 |
1366 if (!context->extension()->HasAPIPermission(function_name)) { | |
1367 static const char kMessage[] = | |
1368 "You do not have permission to use '%s'. Be sure to declare" | |
1369 " in your manifest what permissions you need."; | |
1370 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | |
1371 APIActivityLogger::LogBlockedCall(context->extension()->id(), | |
1372 function_name); | |
1373 v8::ThrowException( | |
1374 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | |
1375 return false; | |
1376 } | |
1377 | |
1378 if (ExtensionAPI::GetSharedInstance()->IsPrivileged(function_name) && | |
1379 context->context_type() != Feature::BLESSED_EXTENSION_CONTEXT) { | |
1380 static const char kMessage[] = | |
1381 "%s can only be used in an extension process."; | |
1382 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | |
1383 v8::ThrowException( | |
1384 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | |
1385 return false; | |
1386 } | |
1387 | |
1388 // Theoretically we could end up with bindings being injected into sandboxed | 1380 // Theoretically we could end up with bindings being injected into sandboxed |
1389 // frames, for example content scripts. Don't let them execute API functions. | 1381 // frames, for example content scripts. Don't let them execute API functions. |
1390 WebKit::WebFrame* frame = context->web_frame(); | 1382 WebKit::WebFrame* frame = context->web_frame(); |
1391 ExtensionURLInfo url_info(frame->document().securityOrigin(), | 1383 ExtensionURLInfo url_info(frame->document().securityOrigin(), |
1392 UserScriptSlave::GetDataSourceURLForFrame(frame)); | 1384 UserScriptSlave::GetDataSourceURLForFrame(frame)); |
1393 if (extensions_.IsSandboxedPage(url_info)) { | 1385 if (extensions_.IsSandboxedPage(url_info)) { |
1394 static const char kMessage[] = | 1386 static const char kMessage[] = |
1395 "%s cannot be used within a sandboxed frame."; | 1387 "%s cannot be used within a sandboxed frame."; |
1396 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | 1388 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
1397 v8::ThrowException( | 1389 v8::ThrowException( |
1398 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | 1390 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
1399 return false; | 1391 return false; |
1400 } | 1392 } |
1401 | 1393 |
1402 return true; | 1394 Feature::Availability availability = context->GetAvailability(function_name); |
1395 if (!availability.is_available()) { | |
1396 v8::ThrowException(v8::Exception::Error( | |
1397 v8::String::New(availability.message().c_str()))); | |
1398 } | |
1399 | |
1400 return availability.is_available(); | |
1403 } | 1401 } |
1404 | 1402 |
1405 void Dispatcher::DispatchEvent(const std::string& extension_id, | 1403 void Dispatcher::DispatchEvent(const std::string& extension_id, |
1406 const std::string& event_name) const { | 1404 const std::string& event_name) const { |
1407 base::ListValue args; | 1405 base::ListValue args; |
1408 args.Set(0, new base::StringValue(event_name)); | 1406 args.Set(0, new base::StringValue(event_name)); |
1409 args.Set(1, new base::ListValue()); | 1407 args.Set(1, new base::ListValue()); |
1410 v8_context_set_.ForEach( | 1408 v8_context_set_.ForEach( |
1411 extension_id, | 1409 extension_id, |
1412 NULL, // all render views | 1410 NULL, // all render views |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1448 RenderView* background_view = | 1446 RenderView* background_view = |
1449 ExtensionHelper::GetBackgroundPage(extension_id); | 1447 ExtensionHelper::GetBackgroundPage(extension_id); |
1450 if (background_view) { | 1448 if (background_view) { |
1451 background_view->Send(new ExtensionHostMsg_EventAck( | 1449 background_view->Send(new ExtensionHostMsg_EventAck( |
1452 background_view->GetRoutingID())); | 1450 background_view->GetRoutingID())); |
1453 } | 1451 } |
1454 } | 1452 } |
1455 } | 1453 } |
1456 | 1454 |
1457 } // namespace extensions | 1455 } // namespace extensions |
OLD | NEW |