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->HasParent() || feature->IsInternal()) | |
635 continue; | |
636 | |
632 if (!context->IsAnyFeatureAvailableToContext(api_name)) { | 637 if (!context->IsAnyFeatureAvailableToContext(api_name)) { |
not at google - send to devlin
2013/06/12 22:34:18
see comment in base_feature_provider.cc
| |
633 DeregisterBinding(api_name, context); | 638 DeregisterBinding(api_name, context); |
634 continue; | 639 continue; |
635 } | 640 } |
636 | 641 |
637 Feature* feature = | |
638 BaseFeatureProvider::GetByName("api")->GetFeature(api_name); | |
639 if (feature && feature->IsInternal()) | |
640 continue; | |
641 | |
642 RegisterBinding(api_name, context); | 642 RegisterBinding(api_name, context); |
643 } | 643 } |
644 } | 644 } |
645 | 645 |
646 void Dispatcher::DeregisterBinding(const std::string& api_name, | 646 void Dispatcher::DeregisterBinding(const std::string& api_name, |
647 ChromeV8Context* context) { | 647 ChromeV8Context* context) { |
648 std::string bind_name; | 648 std::string bind_name; |
649 v8::Handle<v8::Object> bind_object = | 649 v8::Handle<v8::Object> bind_object = |
650 GetOrCreateBindObjectIfAvailable(api_name, &bind_name, context); | 650 GetOrCreateBindObjectIfAvailable(api_name, &bind_name, context); |
651 v8::Handle<v8::String> v8_bind_name = v8::String::New(bind_name.c_str()); | 651 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 | 665 // 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 | 666 // 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 | 667 // 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 | 668 // install the bindings for the API. This is to prevent loading the ancestor |
669 // API schema if it will not be needed. | 669 // API schema if it will not be needed. |
670 // | 670 // |
671 // For example: | 671 // For example: |
672 // If app is available and app.window is not, just install app. | 672 // 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 | 673 // 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. | 674 // app.window on a new object so app does not have to be loaded. |
675 FeatureProvider* feature_provider = BaseFeatureProvider::GetByName("api"); | |
675 std::string ancestor_name; | 676 std::string ancestor_name; |
676 bool only_ancestor_available = false; | 677 bool only_ancestor_available = false; |
677 | 678 |
678 for (size_t i = 0; i < split.size() - 1; ++i) { | 679 for (size_t i = 0; i < split.size() - 1; ++i) { |
679 ancestor_name += (i ? ".": "") + split[i]; | 680 ancestor_name += (i ? ".": "") + split[i]; |
680 if (!ancestor_name.empty() && | 681 if (feature_provider->GetFeature(ancestor_name) && |
681 context->GetAvailability(ancestor_name).is_available() && | 682 context->GetAvailability(ancestor_name).is_available() && |
682 !context->GetAvailability(api_name).is_available()) { | 683 !context->GetAvailability(api_name).is_available()) { |
683 only_ancestor_available = true; | 684 only_ancestor_available = true; |
684 break; | 685 break; |
685 } | 686 } |
686 | 687 |
687 if (bind_object.IsEmpty()) { | 688 if (bind_object.IsEmpty()) { |
688 bind_object = AsObjectOrEmpty(GetOrCreateChrome(context)); | 689 bind_object = AsObjectOrEmpty(GetOrCreateChrome(context)); |
689 if (bind_object.IsEmpty()) | 690 if (bind_object.IsEmpty()) |
690 return v8::Handle<v8::Object>(); | 691 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"; | 1357 DLOG(ERROR) << "Not in a v8::Context"; |
1357 return false; | 1358 return false; |
1358 } | 1359 } |
1359 | 1360 |
1360 if (!context->extension()) { | 1361 if (!context->extension()) { |
1361 v8::ThrowException( | 1362 v8::ThrowException( |
1362 v8::Exception::Error(v8::String::New("Not in an extension."))); | 1363 v8::Exception::Error(v8::String::New("Not in an extension."))); |
1363 return false; | 1364 return false; |
1364 } | 1365 } |
1365 | 1366 |
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 | 1367 // Theoretically we could end up with bindings being injected into sandboxed |
1389 // frames, for example content scripts. Don't let them execute API functions. | 1368 // frames, for example content scripts. Don't let them execute API functions. |
1390 WebKit::WebFrame* frame = context->web_frame(); | 1369 WebKit::WebFrame* frame = context->web_frame(); |
1391 ExtensionURLInfo url_info(frame->document().securityOrigin(), | 1370 ExtensionURLInfo url_info(frame->document().securityOrigin(), |
1392 UserScriptSlave::GetDataSourceURLForFrame(frame)); | 1371 UserScriptSlave::GetDataSourceURLForFrame(frame)); |
1393 if (extensions_.IsSandboxedPage(url_info)) { | 1372 if (extensions_.IsSandboxedPage(url_info)) { |
1394 static const char kMessage[] = | 1373 static const char kMessage[] = |
1395 "%s cannot be used within a sandboxed frame."; | 1374 "%s cannot be used within a sandboxed frame."; |
1396 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | 1375 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
1397 v8::ThrowException( | 1376 v8::ThrowException( |
1398 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | 1377 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
1399 return false; | 1378 return false; |
1400 } | 1379 } |
1401 | 1380 |
1402 return true; | 1381 Feature::Availability availability = context->GetAvailability(function_name); |
1382 if (!availability.is_available()) { | |
1383 v8::ThrowException(v8::Exception::Error( | |
1384 v8::String::New(availability.message().c_str()))); | |
1385 } | |
1386 | |
1387 return availability.is_available(); | |
1403 } | 1388 } |
1404 | 1389 |
1405 void Dispatcher::DispatchEvent(const std::string& extension_id, | 1390 void Dispatcher::DispatchEvent(const std::string& extension_id, |
1406 const std::string& event_name) const { | 1391 const std::string& event_name) const { |
1407 base::ListValue args; | 1392 base::ListValue args; |
1408 args.Set(0, new base::StringValue(event_name)); | 1393 args.Set(0, new base::StringValue(event_name)); |
1409 args.Set(1, new base::ListValue()); | 1394 args.Set(1, new base::ListValue()); |
1410 v8_context_set_.ForEach( | 1395 v8_context_set_.ForEach( |
1411 extension_id, | 1396 extension_id, |
1412 NULL, // all render views | 1397 NULL, // all render views |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1448 RenderView* background_view = | 1433 RenderView* background_view = |
1449 ExtensionHelper::GetBackgroundPage(extension_id); | 1434 ExtensionHelper::GetBackgroundPage(extension_id); |
1450 if (background_view) { | 1435 if (background_view) { |
1451 background_view->Send(new ExtensionHostMsg_EventAck( | 1436 background_view->Send(new ExtensionHostMsg_EventAck( |
1452 background_view->GetRoutingID())); | 1437 background_view->GetRoutingID())); |
1453 } | 1438 } |
1454 } | 1439 } |
1455 } | 1440 } |
1456 | 1441 |
1457 } // namespace extensions | 1442 } // namespace extensions |
OLD | NEW |