summaryrefslogtreecommitdiffstats
path: root/chrome/common
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-26 18:45:21 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-26 18:45:21 +0000
commit2a521c50ab1cb527625a69f3130a421a01f84621 (patch)
tree68153622a64534e8400c122575ab4232b6de2725 /chrome/common
parent43792ceca73af6e06c079db4f034b28a4402989d (diff)
downloadchromium_src-2a521c50ab1cb527625a69f3130a421a01f84621.zip
chromium_src-2a521c50ab1cb527625a69f3130a421a01f84621.tar.gz
chromium_src-2a521c50ab1cb527625a69f3130a421a01f84621.tar.bz2
Refactor away all the duplicate extension data structures in
renderer processes by sending the full extension object instead. ExtensionRendererInfo remains, but it is now just a convenience wrapper around a map of Extension objects. This allows us to reuse all the helper methods on Extension, ExtensionIconSet, ExtensionExtent, etc without duplicating them in the renderer. Also changed broadcasts to renderers to send only changed information, not entire set of extension data again. BUG=70516 Review URL: http://codereview.chromium.org/6242010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72654 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r--chrome/common/extensions/extension.cc34
-rw-r--r--chrome/common/extensions/extension.h28
-rw-r--r--chrome/common/render_messages.h3
-rw-r--r--chrome/common/render_messages_internal.h20
-rw-r--r--chrome/common/render_messages_params.cc122
-rw-r--r--chrome/common/render_messages_params.h48
6 files changed, 140 insertions, 115 deletions
diff --git a/chrome/common/extensions/extension.cc b/chrome/common/extensions/extension.cc
index a132baf..76215e4 100644
--- a/chrome/common/extensions/extension.cc
+++ b/chrome/common/extensions/extension.cc
@@ -2085,7 +2085,7 @@ static std::string SizeToString(const gfx::Size& max_size) {
// static
void Extension::SetScriptingWhitelist(
- const std::vector<std::string>& whitelist) {
+ const Extension::ScriptingWhitelist& whitelist) {
ScriptingWhitelist* current_whitelist =
ExtensionConfig::GetInstance()->whitelist();
current_whitelist->clear();
@@ -2095,6 +2095,11 @@ void Extension::SetScriptingWhitelist(
}
}
+// static
+const Extension::ScriptingWhitelist* Extension::GetScriptingWhitelist() {
+ return ExtensionConfig::GetInstance()->whitelist();
+}
+
void Extension::SetCachedImage(const ExtensionResource& source,
const SkBitmap& image,
const gfx::Size& original_size) const {
@@ -2250,21 +2255,16 @@ bool Extension::HasMultipleUISurfaces() const {
return num_surfaces > 1;
}
-// static
-bool Extension::CanExecuteScriptOnPage(
- const GURL& page_url, bool can_execute_script_everywhere,
- const std::vector<URLPattern>* host_permissions,
- UserScript* script,
- std::string* error) {
- DCHECK(!(host_permissions && script)) << "Shouldn't specify both";
-
+bool Extension::CanExecuteScriptOnPage(const GURL& page_url,
+ UserScript* script,
+ std::string* error) const {
// The gallery is special-cased as a restricted URL for scripting to prevent
// access to special JS bindings we expose to the gallery (and avoid things
// like extensions removing the "report abuse" link).
// TODO(erikkay): This seems like the wrong test. Shouldn't we we testing
// against the store app extent?
if ((page_url.host() == GURL(Extension::ChromeStoreLaunchURL()).host()) &&
- !can_execute_script_everywhere &&
+ !CanExecuteScriptEverywhere() &&
!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kAllowScriptingGallery)) {
if (error)
@@ -2272,17 +2272,19 @@ bool Extension::CanExecuteScriptOnPage(
return false;
}
- if (host_permissions) {
- for (size_t i = 0; i < host_permissions->size(); ++i) {
- if ((*host_permissions)[i].MatchesUrl(page_url))
- return true;
- }
- }
+ // If a script is specified, use its matches.
if (script) {
if (script->MatchesUrl(page_url))
return true;
}
+ // Otherwise, see if this extension has permission to execute script
+ // programmatically on pages.
+ for (size_t i = 0; i < host_permissions_.size(); ++i) {
+ if (host_permissions_[i].MatchesUrl(page_url))
+ return true;
+ }
+
if (error) {
*error = ExtensionErrorUtils::FormatErrorMessage(errors::kCannotAccessPage,
page_url.spec());
diff --git a/chrome/common/extensions/extension.h b/chrome/common/extensions/extension.h
index 95c2151..eba6ed3 100644
--- a/chrome/common/extensions/extension.h
+++ b/chrome/common/extensions/extension.h
@@ -308,22 +308,9 @@ class Extension : public base::RefCountedThreadSafe<Extension> {
// ExtensionService::IsDownloadFromGallery
static std::string ChromeStoreLaunchURL();
- // Helper function that consolidates the check for whether the script can
- // execute into one location. |page_url| is the page that is the candidate
- // for running the script, |can_execute_script_everywhere| specifies whether
- // the extension is on the whitelist, |allowed_pages| is a vector of
- // URLPatterns, listing what access the extension has, |script| is the script
- // pointer (if content script) and |error| is an optional parameter, which
- // will receive the error string listing why access was denied.
- static bool CanExecuteScriptOnPage(
- const GURL& page_url,
- bool can_execute_script_everywhere,
- const std::vector<URLPattern>* allowed_pages,
- UserScript* script,
- std::string* error);
-
// Adds an extension to the scripting whitelist. Used for testing only.
static void SetScriptingWhitelist(const ScriptingWhitelist& whitelist);
+ static const ScriptingWhitelist* GetScriptingWhitelist();
// Returns true if the extension has the specified API permission.
static bool HasApiPermission(const std::set<std::string>& api_permissions,
@@ -383,6 +370,7 @@ class Extension : public base::RefCountedThreadSafe<Extension> {
// Gets the fully resolved absolute launch URL.
GURL GetFullLaunchURL() const;
+
// Image cache related methods. These are only valid on the UI thread and
// not maintained by this class. See ImageLoadingTracker for usage. The
// |original_size| parameter should be the size of the image at |source|
@@ -394,6 +382,18 @@ class Extension : public base::RefCountedThreadSafe<Extension> {
const gfx::Size& max_size) const;
SkBitmap GetCachedImage(const ExtensionResource& source,
const gfx::Size& max_size) const;
+
+ // Returns true if this extension can execute script on a page. If a
+ // UserScript object is passed, permission to run that specific script is
+ // checked (using its matches list). Otherwise, permission to execute script
+ // programmatically is checked (using the extension's host permission).
+ //
+ // This method is also aware of certain special pages that extensions are
+ // usually not allowed to run script on.
+ bool CanExecuteScriptOnPage(const GURL& page_url,
+ UserScript* script,
+ std::string* error) const;
+
// Returns true if this extension is a COMPONENT extension, or if it is
// on the whitelist of extensions that can script all pages.
bool CanExecuteScriptEverywhere() const;
diff --git a/chrome/common/render_messages.h b/chrome/common/render_messages.h
index f0ce905..6e5959a 100644
--- a/chrome/common/render_messages.h
+++ b/chrome/common/render_messages.h
@@ -104,8 +104,7 @@ struct ViewHostMsg_ShowNotification_Params;
struct ViewMsg_New_Params;
struct ViewHostMsg_CreateWindow_Params;
struct ViewHostMsg_RunFileChooser_Params;
-struct ViewMsg_ExtensionRendererInfo;
-struct ViewMsg_ExtensionsUpdated_Params;
+struct ViewMsg_ExtensionLoaded_Params;
struct ViewMsg_DeviceOrientationUpdated_Params;
struct ViewHostMsg_DomMessage_Params;
struct ViewHostMsg_AccessibilityNotification_Params;
diff --git a/chrome/common/render_messages_internal.h b/chrome/common/render_messages_internal.h
index 6d331d0..a55290a 100644
--- a/chrome/common/render_messages_internal.h
+++ b/chrome/common/render_messages_internal.h
@@ -15,6 +15,7 @@
#include "base/platform_file.h"
#include "base/sync_socket.h"
#include "chrome/common/content_settings.h"
+#include "chrome/common/extensions/extension.h"
#include "chrome/common/geoposition.h"
#include "chrome/common/nacl_types.h"
#include "chrome/common/notification_type.h"
@@ -762,6 +763,10 @@ IPC_MESSAGE_ROUTED4(ViewMsg_ExtensionMessageInvoke,
IPC_MESSAGE_CONTROL1(ViewMsg_Extension_SetFunctionNames,
std::vector<std::string>)
+// TODO(aa): SetAPIPermissions, SetHostPermissions, and possibly
+// UpdatePageActions should be replaced with just sending additional data in
+// ExtensionLoaded. See: crbug.com/70516.
+
// Tell the renderer process which permissions the given extension has. See
// Extension::Permissions for which elements correspond to which permissions.
IPC_MESSAGE_CONTROL2(ViewMsg_Extension_SetAPIPermissions,
@@ -780,6 +785,17 @@ IPC_MESSAGE_CONTROL2(ViewMsg_Extension_UpdatePageActions,
std::string /* extension_id */,
std::vector<std::string> /* page_action_ids */)
+// Notifies the renderer that an extension was loaded in the browser.
+IPC_MESSAGE_CONTROL1(ViewMsg_ExtensionLoaded, ViewMsg_ExtensionLoaded_Params);
+
+// Notifies the renderer that an extension was unloaded in the browser.
+IPC_MESSAGE_CONTROL1(ViewMsg_ExtensionUnloaded, std::string);
+
+// Updates the scripting whitelist for extensions in the render process. This is
+// only used for testing.
+IPC_MESSAGE_CONTROL1(ViewMsg_Extension_SetScriptingWhitelist,
+ Extension::ScriptingWhitelist /* extenison ids */);
+
// Changes the text direction of the currently selected input field (if any).
IPC_MESSAGE_ROUTED1(ViewMsg_SetTextDirection,
WebKit::WebTextDirection /* direction */)
@@ -973,10 +989,6 @@ IPC_MESSAGE_ROUTED1(ViewMsg_Geolocation_PositionUpdated,
IPC_MESSAGE_CONTROL1(ViewMsg_SetIsIncognitoProcess,
bool /* is_incognito_processs */)
-// Notification that the list of extensions has been updated.
-IPC_MESSAGE_CONTROL1(ViewMsg_ExtensionsUpdated,
- ViewMsg_ExtensionsUpdated_Params)
-
// Enable accessibility in the renderer process.
IPC_MESSAGE_ROUTED0(ViewMsg_EnableAccessibility)
diff --git a/chrome/common/render_messages_params.cc b/chrome/common/render_messages_params.cc
index fee3b0e..59a17bd 100644
--- a/chrome/common/render_messages_params.cc
+++ b/chrome/common/render_messages_params.cc
@@ -6,6 +6,7 @@
#include "chrome/common/navigation_gesture.h"
#include "chrome/common/common_param_traits.h"
+#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/render_messages.h"
#include "net/base/upload_data.h"
@@ -248,20 +249,6 @@ ViewHostMsg_RunFileChooser_Params::ViewHostMsg_RunFileChooser_Params()
ViewHostMsg_RunFileChooser_Params::~ViewHostMsg_RunFileChooser_Params() {
}
-ViewMsg_ExtensionRendererInfo::ViewMsg_ExtensionRendererInfo()
- : location(Extension::INVALID),
- allowed_to_execute_script_everywhere(false) {
-}
-
-ViewMsg_ExtensionRendererInfo::~ViewMsg_ExtensionRendererInfo() {
-}
-
-ViewMsg_ExtensionsUpdated_Params::ViewMsg_ExtensionsUpdated_Params() {
-}
-
-ViewMsg_ExtensionsUpdated_Params::~ViewMsg_ExtensionsUpdated_Params() {
-}
-
ViewMsg_DeviceOrientationUpdated_Params::
ViewMsg_DeviceOrientationUpdated_Params()
: can_provide_alpha(false),
@@ -285,6 +272,59 @@ ViewHostMsg_DomMessage_Params::ViewHostMsg_DomMessage_Params()
ViewHostMsg_DomMessage_Params::~ViewHostMsg_DomMessage_Params() {
}
+ViewMsg_ExtensionLoaded_Params::ViewMsg_ExtensionLoaded_Params() {
+}
+
+ViewMsg_ExtensionLoaded_Params::~ViewMsg_ExtensionLoaded_Params() {
+}
+
+ViewMsg_ExtensionLoaded_Params::ViewMsg_ExtensionLoaded_Params(
+ const ViewMsg_ExtensionLoaded_Params& other)
+ : manifest(other.manifest->DeepCopy()),
+ location(other.location),
+ path(other.path),
+ id(other.id) {
+}
+
+ViewMsg_ExtensionLoaded_Params::ViewMsg_ExtensionLoaded_Params(
+ const Extension* extension)
+ : manifest(new DictionaryValue()),
+ location(extension->location()),
+ path(extension->path()),
+ id(extension->id()) {
+ // As we need more bits of extension data in the renderer, add more keys to
+ // this list.
+ const char* kRendererExtensionKeys[] = {
+ extension_manifest_keys::kPublicKey,
+ extension_manifest_keys::kName,
+ extension_manifest_keys::kVersion,
+ extension_manifest_keys::kIcons,
+ extension_manifest_keys::kPermissions,
+ extension_manifest_keys::kApp
+ };
+
+ // Copy only the data we need.
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRendererExtensionKeys); ++i) {
+ Value* temp = NULL;
+ if (extension->manifest_value()->Get(kRendererExtensionKeys[i], &temp))
+ manifest->Set(kRendererExtensionKeys[i], temp->DeepCopy());
+ }
+}
+
+scoped_refptr<Extension>
+ ViewMsg_ExtensionLoaded_Params::ConvertToExtension() const {
+ // Extensions that are loaded unpacked won't have a key.
+ const bool kRequireKey = false;
+ std::string error;
+
+ scoped_refptr<Extension> extension(
+ Extension::Create(path, location, *manifest, kRequireKey, &error));
+ if (!extension.get())
+ LOG(ERROR) << "Error deserializing extension: " << error;
+
+ return extension;
+}
+
namespace IPC {
// Self contained templates which are only used inside serializing Params
@@ -1455,51 +1495,25 @@ void ParamTraits<ViewHostMsg_RunFileChooser_Params>::Log(
LogParam(p.accept_types, l);
}
-void ParamTraits<ViewMsg_ExtensionRendererInfo>::Write(Message* m,
- const param_type& p) {
- WriteParam(m, p.id);
- WriteParam(m, p.web_extent);
- WriteParam(m, p.name);
- WriteParam(m, p.icon_url);
+void ParamTraits<ViewMsg_ExtensionLoaded_Params>::Write(Message* m,
+ const param_type& p) {
WriteParam(m, p.location);
- WriteParam(m, p.allowed_to_execute_script_everywhere);
- WriteParam(m, p.host_permissions);
+ WriteParam(m, p.path);
+ WriteParam(m, *(p.manifest));
}
-bool ParamTraits<ViewMsg_ExtensionRendererInfo>::Read(const Message* m,
- void** iter,
- param_type* p) {
- return ReadParam(m, iter, &p->id) &&
- ReadParam(m, iter, &p->web_extent) &&
- ReadParam(m, iter, &p->name) &&
- ReadParam(m, iter, &p->icon_url) &&
- ReadParam(m, iter, &p->location) &&
- ReadParam(m, iter, &p->allowed_to_execute_script_everywhere) &&
- ReadParam(m, iter, &p->host_permissions);
+bool ParamTraits<ViewMsg_ExtensionLoaded_Params>::Read(const Message* m,
+ void** iter,
+ param_type* p) {
+ p->manifest.reset(new DictionaryValue());
+ return ReadParam(m, iter, &p->location) &&
+ ReadParam(m, iter, &p->path) &&
+ ReadParam(m, iter, p->manifest.get());
}
-void ParamTraits<ViewMsg_ExtensionRendererInfo>::Log(const param_type& p,
- std::string* l) {
- LogParam(p.id, l);
-}
-
-void ParamTraits<ViewMsg_ExtensionsUpdated_Params>::Write(
- Message* m,
- const param_type& p) {
- WriteParam(m, p.extensions);
-}
-
-bool ParamTraits<ViewMsg_ExtensionsUpdated_Params>::Read(
- const Message* m,
- void** iter,
- param_type* p) {
- return ReadParam(m, iter, &p->extensions);
-}
-
-void ParamTraits<ViewMsg_ExtensionsUpdated_Params>::Log(
- const param_type& p,
- std::string* l) {
- LogParam(p.extensions, l);
+void ParamTraits<ViewMsg_ExtensionLoaded_Params>::Log(const param_type& p,
+ std::string* l) {
+ l->append(p.id);
}
void ParamTraits<ViewMsg_DeviceOrientationUpdated_Params>::Write(
diff --git a/chrome/common/render_messages_params.h b/chrome/common/render_messages_params.h
index 1bd4516..ce4b18c 100644
--- a/chrome/common/render_messages_params.h
+++ b/chrome/common/render_messages_params.h
@@ -835,25 +835,31 @@ struct ViewHostMsg_RunFileChooser_Params {
string16 accept_types;
};
-struct ViewMsg_ExtensionRendererInfo {
- ViewMsg_ExtensionRendererInfo();
- ~ViewMsg_ExtensionRendererInfo();
+struct ViewMsg_ExtensionLoaded_Params {
+ ViewMsg_ExtensionLoaded_Params();
+ ~ViewMsg_ExtensionLoaded_Params();
+ explicit ViewMsg_ExtensionLoaded_Params(const Extension* extension);
- std::string id;
- ExtensionExtent web_extent;
- std::string name;
- GURL icon_url;
+ // A copy constructor is needed because this structure can end up getting
+ // copied inside the IPC machinery on gcc <= 4.2.
+ ViewMsg_ExtensionLoaded_Params(
+ const ViewMsg_ExtensionLoaded_Params& other);
+
+ // Creates a new extension from the data in this object.
+ scoped_refptr<Extension> ConvertToExtension() const;
+
+ // The subset of the extension manifest data we send to renderers.
+ scoped_ptr<DictionaryValue> manifest;
+
+ // The location the extension was installed from.
Extension::Location location;
- bool allowed_to_execute_script_everywhere;
- std::vector<URLPattern> host_permissions;
-};
-struct ViewMsg_ExtensionsUpdated_Params {
- ViewMsg_ExtensionsUpdated_Params();
- ~ViewMsg_ExtensionsUpdated_Params();
+ // The path the extension was loaded from. This is used in the renderer only
+ // to generate the extension ID for extensions that are loaded unpacked.
+ FilePath path;
- // Describes the installed extension apps and the URLs they cover.
- std::vector<ViewMsg_ExtensionRendererInfo> extensions;
+ // We keep this separate so that it can be used in logging.
+ std::string id;
};
struct ViewMsg_DeviceOrientationUpdated_Params {
@@ -1111,16 +1117,8 @@ struct ParamTraits<ViewHostMsg_RunFileChooser_Params> {
};
template <>
-struct ParamTraits<ViewMsg_ExtensionRendererInfo> {
- typedef ViewMsg_ExtensionRendererInfo param_type;
- static void Write(Message* m, const param_type& p);
- static bool Read(const Message* m, void** iter, param_type* p);
- static void Log(const param_type& p, std::string* l);
-};
-
-template <>
-struct ParamTraits<ViewMsg_ExtensionsUpdated_Params> {
- typedef ViewMsg_ExtensionsUpdated_Params param_type;
+struct ParamTraits<ViewMsg_ExtensionLoaded_Params> {
+ typedef ViewMsg_ExtensionLoaded_Params param_type;
static void Write(Message* m, const param_type& p);
static bool Read(const Message* m, void** iter, param_type* p);
static void Log(const param_type& p, std::string* l);