| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/extension_process_bindings.h" | 5 #include "chrome/renderer/extensions/extension_process_bindings.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 14 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
| 15 #include "base/singleton.h" | 15 #include "base/singleton.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 19 #include "chrome/common/extensions/extension_constants.h" | 19 #include "chrome/common/extensions/extension_constants.h" |
| 20 #include "chrome/common/extensions/url_pattern.h" | 20 #include "chrome/common/extensions/url_pattern.h" |
| 21 #include "chrome/common/render_messages.h" | 21 #include "chrome/common/render_messages.h" |
| 22 #include "chrome/common/render_messages_params.h" | 22 #include "chrome/common/render_messages_params.h" |
| 23 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 24 #include "chrome/renderer/extensions/bindings_utils.h" | 24 #include "chrome/renderer/extensions/bindings_utils.h" |
| 25 #include "chrome/renderer/extensions/event_bindings.h" | 25 #include "chrome/renderer/extensions/event_bindings.h" |
| 26 #include "chrome/renderer/extensions/extension_renderer_info.h" |
| 26 #include "chrome/renderer/extensions/js_only_v8_extensions.h" | 27 #include "chrome/renderer/extensions/js_only_v8_extensions.h" |
| 27 #include "chrome/renderer/extensions/renderer_extension_bindings.h" | 28 #include "chrome/renderer/extensions/renderer_extension_bindings.h" |
| 28 #include "chrome/renderer/user_script_slave.h" | 29 #include "chrome/renderer/user_script_slave.h" |
| 29 #include "chrome/renderer/render_thread.h" | 30 #include "chrome/renderer/render_thread.h" |
| 30 #include "chrome/renderer/render_view.h" | 31 #include "chrome/renderer/render_view.h" |
| 31 #include "chrome/renderer/render_view_visitor.h" | 32 #include "chrome/renderer/render_view_visitor.h" |
| 32 #include "grit/common_resources.h" | 33 #include "grit/common_resources.h" |
| 33 #include "grit/renderer_resources.h" | 34 #include "grit/renderer_resources.h" |
| 34 #include "third_party/skia/include/core/SkBitmap.h" | 35 #include "third_party/skia/include/core/SkBitmap.h" |
| 35 #include "third_party/skia/include/core/SkColor.h" | 36 #include "third_party/skia/include/core/SkColor.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 49 using bindings_utils::ExtensionBase; | 50 using bindings_utils::ExtensionBase; |
| 50 using WebKit::WebFrame; | 51 using WebKit::WebFrame; |
| 51 using WebKit::WebSecurityPolicy; | 52 using WebKit::WebSecurityPolicy; |
| 52 using WebKit::WebView; | 53 using WebKit::WebView; |
| 53 | 54 |
| 54 namespace { | 55 namespace { |
| 55 | 56 |
| 56 // A map of extension ID to vector of page action ids. | 57 // A map of extension ID to vector of page action ids. |
| 57 typedef std::map< std::string, std::vector<std::string> > PageActionIdMap; | 58 typedef std::map< std::string, std::vector<std::string> > PageActionIdMap; |
| 58 | 59 |
| 59 // A map of permission name to whether its enabled for this extension. | 60 // A list of permissions that are enabled for this extension. |
| 60 typedef std::map<std::string, bool> PermissionsMap; | 61 typedef std::vector<std::string> PermissionsList; |
| 61 | 62 |
| 62 // A map of extension ID to permissions map. | 63 // A map of extension ID to permissions map. |
| 63 typedef std::map<std::string, PermissionsMap> ExtensionPermissionsMap; | 64 typedef std::map<std::string, PermissionsList> ExtensionPermissionsList; |
| 64 | 65 |
| 65 // A map of extension ID to whether this extension was enabled in incognito. | 66 // A map of extension ID to whether this extension was enabled in incognito. |
| 66 typedef std::map<std::string, bool> IncognitoEnabledMap; | 67 typedef std::map<std::string, bool> IncognitoEnabledMap; |
| 67 | 68 |
| 68 const char kExtensionName[] = "chrome/ExtensionProcessBindings"; | 69 const char kExtensionName[] = "chrome/ExtensionProcessBindings"; |
| 69 const char* kExtensionDeps[] = { | 70 const char* kExtensionDeps[] = { |
| 70 BaseJsV8Extension::kName, | 71 BaseJsV8Extension::kName, |
| 71 EventBindings::kName, | 72 EventBindings::kName, |
| 72 JsonSchemaJsV8Extension::kName, | 73 JsonSchemaJsV8Extension::kName, |
| 73 RendererExtensionBindings::kName, | 74 RendererExtensionBindings::kName, |
| 74 ExtensionApiTestV8Extension::kName, | 75 ExtensionApiTestV8Extension::kName, |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 // A list of the API packages which have no associated permission. | |
| 78 // TODO(erikkay) It might be nice if for consistency we could merge these with | |
| 79 // the permissions list, or at least have them in one place. | |
| 80 const char* kNonPermissionExtensionPackages[] = { | |
| 81 "extension", | |
| 82 // TODO(erikkay): We're inconsistent about the the package name in the events | |
| 83 // for pageAction and browserAction. | |
| 84 "pageAction", | |
| 85 "pageActions", | |
| 86 "browserAction", | |
| 87 "browserActions", | |
| 88 "i18n", | |
| 89 "devtools", | |
| 90 "test" | |
| 91 }; | |
| 92 | |
| 93 struct SingletonData { | 78 struct SingletonData { |
| 94 std::set<std::string> function_names_; | 79 std::set<std::string> function_names_; |
| 95 PageActionIdMap page_action_ids_; | 80 PageActionIdMap page_action_ids_; |
| 96 ExtensionPermissionsMap permissions_; | 81 ExtensionPermissionsList permissions_; |
| 97 IncognitoEnabledMap incognito_enabled_map_; | 82 IncognitoEnabledMap incognito_enabled_map_; |
| 98 }; | 83 }; |
| 99 | 84 |
| 100 static std::set<std::string>* GetFunctionNameSet() { | 85 static std::set<std::string>* GetFunctionNameSet() { |
| 101 return &Singleton<SingletonData>()->function_names_; | 86 return &Singleton<SingletonData>()->function_names_; |
| 102 } | 87 } |
| 103 | 88 |
| 104 static PageActionIdMap* GetPageActionMap() { | 89 static PageActionIdMap* GetPageActionMap() { |
| 105 return &Singleton<SingletonData>()->page_action_ids_; | 90 return &Singleton<SingletonData>()->page_action_ids_; |
| 106 } | 91 } |
| 107 | 92 |
| 108 static PermissionsMap* GetPermissionsMap(const std::string& extension_id) { | 93 static PermissionsList* GetPermissionsList(const std::string& extension_id) { |
| 109 return &Singleton<SingletonData>()->permissions_[extension_id]; | 94 return &Singleton<SingletonData>()->permissions_[extension_id]; |
| 110 } | 95 } |
| 111 | 96 |
| 112 static IncognitoEnabledMap* GetIncognitoEnabledMap() { | 97 static IncognitoEnabledMap* GetIncognitoEnabledMap() { |
| 113 return &Singleton<SingletonData>()->incognito_enabled_map_; | 98 return &Singleton<SingletonData>()->incognito_enabled_map_; |
| 114 } | 99 } |
| 115 | 100 |
| 116 static void GetActiveExtensionIDs(std::set<std::string>* extension_ids) { | 101 static void GetActiveExtensionIDs(std::set<std::string>* extension_ids) { |
| 117 ExtensionPermissionsMap& permissions = | 102 ExtensionPermissionsList& permissions = |
| 118 Singleton<SingletonData>()->permissions_; | 103 Singleton<SingletonData>()->permissions_; |
| 119 | 104 |
| 120 for (ExtensionPermissionsMap::iterator iter = permissions.begin(); | 105 for (ExtensionPermissionsList::iterator iter = permissions.begin(); |
| 121 iter != permissions.end(); ++iter) { | 106 iter != permissions.end(); ++iter) { |
| 122 extension_ids->insert(iter->first); | 107 extension_ids->insert(iter->first); |
| 123 } | 108 } |
| 124 } | 109 } |
| 125 | 110 |
| 126 // A RenderViewVisitor class that iterates through the set of available | 111 // A RenderViewVisitor class that iterates through the set of available |
| 127 // views, looking for a view of the given type, in the given browser window | 112 // views, looking for a view of the given type, in the given browser window |
| 128 // and within the given extension. | 113 // and within the given extension. |
| 129 // Used to accumulate the list of views associated with an extension. | 114 // Used to accumulate the list of views associated with an extension. |
| 130 class ExtensionViewAccumulator : public RenderViewVisitor { | 115 class ExtensionViewAccumulator : public RenderViewVisitor { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 | 212 |
| 228 // Note: do not call this function before or during the chromeHidden.onLoad | 213 // Note: do not call this function before or during the chromeHidden.onLoad |
| 229 // event dispatch. The URL might not have been committed yet and might not | 214 // event dispatch. The URL might not have been committed yet and might not |
| 230 // be an extension URL. | 215 // be an extension URL. |
| 231 static std::string ExtensionIdForCurrentContext() { | 216 static std::string ExtensionIdForCurrentContext() { |
| 232 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); | 217 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); |
| 233 if (!renderview) | 218 if (!renderview) |
| 234 return std::string(); // this can happen as a tab is closing. | 219 return std::string(); // this can happen as a tab is closing. |
| 235 | 220 |
| 236 GURL url = renderview->webview()->mainFrame()->url(); | 221 GURL url = renderview->webview()->mainFrame()->url(); |
| 237 if (!url.SchemeIs(chrome::kExtensionScheme)) | 222 if (!ExtensionRendererInfo::ExtensionBindingsAllowed(url)) |
| 238 return std::string(); | 223 return std::string(); |
| 239 | 224 |
| 240 return url.host(); | 225 return ExtensionRendererInfo::GetIdByURL(url); |
| 241 } | 226 } |
| 242 | 227 |
| 243 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 228 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 244 v8::Handle<v8::String> name) { | 229 v8::Handle<v8::String> name) { |
| 245 if (name->Equals(v8::String::New("GetExtensionAPIDefinition"))) { | 230 if (name->Equals(v8::String::New("GetExtensionAPIDefinition"))) { |
| 246 return v8::FunctionTemplate::New(GetExtensionAPIDefinition); | 231 return v8::FunctionTemplate::New(GetExtensionAPIDefinition); |
| 247 } else if (name->Equals(v8::String::New("GetExtensionViews"))) { | 232 } else if (name->Equals(v8::String::New("GetExtensionViews"))) { |
| 248 return v8::FunctionTemplate::New(GetExtensionViews); | 233 return v8::FunctionTemplate::New(GetExtensionViews); |
| 249 } else if (name->Equals(v8::String::New("GetNextRequestId"))) { | 234 } else if (name->Equals(v8::String::New("GetNextRequestId"))) { |
| 250 return v8::FunctionTemplate::New(GetNextRequestId); | 235 return v8::FunctionTemplate::New(GetNextRequestId); |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 } else { | 619 } else { |
| 635 if (page_action_map.find(extension_id) != page_action_map.end()) | 620 if (page_action_map.find(extension_id) != page_action_map.end()) |
| 636 page_action_map.erase(extension_id); | 621 page_action_map.erase(extension_id); |
| 637 } | 622 } |
| 638 } | 623 } |
| 639 | 624 |
| 640 // static | 625 // static |
| 641 void ExtensionProcessBindings::SetAPIPermissions( | 626 void ExtensionProcessBindings::SetAPIPermissions( |
| 642 const std::string& extension_id, | 627 const std::string& extension_id, |
| 643 const std::vector<std::string>& permissions) { | 628 const std::vector<std::string>& permissions) { |
| 644 PermissionsMap& permissions_map = *GetPermissionsMap(extension_id); | 629 PermissionsList& permissions_list = *GetPermissionsList(extension_id); |
| 645 | 630 permissions_list.assign(permissions.begin(), permissions.end()); |
| 646 // Default all the API permissions to off. We will reset them below. | |
| 647 for (size_t i = 0; i < Extension::kNumPermissions; ++i) | |
| 648 permissions_map[Extension::kPermissionNames[i]] = false; | |
| 649 for (size_t i = 0; i < permissions.size(); ++i) | |
| 650 permissions_map[permissions[i]] = true; | |
| 651 } | 631 } |
| 652 | 632 |
| 653 // static | 633 // static |
| 654 void ExtensionProcessBindings::SetHostPermissions( | 634 void ExtensionProcessBindings::SetHostPermissions( |
| 655 const GURL& extension_url, | 635 const GURL& extension_url, |
| 656 const std::vector<URLPattern>& permissions) { | 636 const std::vector<URLPattern>& permissions) { |
| 657 for (size_t i = 0; i < permissions.size(); ++i) { | 637 for (size_t i = 0; i < permissions.size(); ++i) { |
| 658 const char* schemes[] = { | 638 const char* schemes[] = { |
| 659 chrome::kHttpScheme, | 639 chrome::kHttpScheme, |
| 660 chrome::kHttpsScheme, | 640 chrome::kHttpsScheme, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 685 const std::string& permission) { | 665 const std::string& permission) { |
| 686 std::string permission_name = permission; | 666 std::string permission_name = permission; |
| 687 | 667 |
| 688 // See if this is a function or event name first and strip out the package. | 668 // See if this is a function or event name first and strip out the package. |
| 689 // Functions will be of the form package.function | 669 // Functions will be of the form package.function |
| 690 // Events will be of the form package/id or package.optional.stuff | 670 // Events will be of the form package/id or package.optional.stuff |
| 691 size_t separator = permission.find_first_of("./"); | 671 size_t separator = permission.find_first_of("./"); |
| 692 if (separator != std::string::npos) | 672 if (separator != std::string::npos) |
| 693 permission_name = permission.substr(0, separator); | 673 permission_name = permission.substr(0, separator); |
| 694 | 674 |
| 695 // windows and tabs are the same permission. | 675 PermissionsList& permissions_list = *GetPermissionsList(extension_id); |
| 696 if (permission_name == "windows") | 676 return Extension::HasApiPermission(permissions_list, permission_name); |
| 697 permission_name = Extension::kTabPermission; | |
| 698 | |
| 699 for (size_t i = 0; i < arraysize(kNonPermissionExtensionPackages); ++i) | |
| 700 if (permission_name == kNonPermissionExtensionPackages[i]) | |
| 701 return true; | |
| 702 | |
| 703 PermissionsMap& permissions_map = *GetPermissionsMap(extension_id); | |
| 704 PermissionsMap::iterator it = permissions_map.find(permission_name); | |
| 705 return (it != permissions_map.end() && it->second); | |
| 706 } | 677 } |
| 707 | 678 |
| 708 // static | 679 // static |
| 709 v8::Handle<v8::Value> | 680 v8::Handle<v8::Value> |
| 710 ExtensionProcessBindings::ThrowPermissionDeniedException( | 681 ExtensionProcessBindings::ThrowPermissionDeniedException( |
| 711 const std::string& function_name) { | 682 const std::string& function_name) { |
| 712 static const char kMessage[] = | 683 static const char kMessage[] = |
| 713 "You do not have permission to use '%s'. Be sure to declare" | 684 "You do not have permission to use '%s'. Be sure to declare" |
| 714 " in your manifest what permissions you need."; | 685 " in your manifest what permissions you need."; |
| 715 std::string error_msg = StringPrintf(kMessage, function_name.c_str()); | 686 std::string error_msg = StringPrintf(kMessage, function_name.c_str()); |
| 716 | 687 |
| 717 return v8::ThrowException(v8::Exception::Error( | 688 return v8::ThrowException(v8::Exception::Error( |
| 718 v8::String::New(error_msg.c_str()))); | 689 v8::String::New(error_msg.c_str()))); |
| 719 } | 690 } |
| 720 | 691 |
| OLD | NEW |