Skip to content

Commit

Permalink
A refacotring in the renderer side: introduce InjectionHost to de-cou…
Browse files Browse the repository at this point in the history
…ple extensions and to support content script injection out of chrome extensions.

BUG=437566

Review URL: https://codereview.chromium.org/885493007

Cr-Commit-Position: refs/heads/master@{#316283}
  • Loading branch information
hanxi authored and Commit bot committed Feb 13, 2015
1 parent a3d8170 commit a5c856c
Show file tree
Hide file tree
Showing 18 changed files with 318 additions and 121 deletions.
2 changes: 1 addition & 1 deletion extensions/browser/guest_view/web_view/web_view_guest.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class WebViewGuest : public GuestView<WebViewGuest>,

static const char Type[];

// Return the stored rules registry ID of the given webview. Will generate
// Returns the stored rules registry ID of the given webview. Will generate
// an ID for the first query.
static int GetOrGenerateRulesRegistryID(
int embedder_process_id,
Expand Down
32 changes: 32 additions & 0 deletions extensions/common/host_id.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/common/host_id.h"

HostID::HostID() {
}

HostID::HostID(HostType type, const std::string& id)
: type_(type), id_(id) {
}

HostID::HostID(const HostID& host_id)
: type_(host_id.type()),
id_(host_id.id()) {
}

HostID::~HostID() {
}

bool HostID::operator<(const HostID& host_id) const {
if (type_ != host_id.type())
return type_ < host_id.type();
else if (id_ != host_id.id())
return id_ < host_id.id();
return false;
}

bool HostID::operator==(const HostID& host_id) const {
return type_ == host_id.type() && id_ == host_id.id();
}
18 changes: 7 additions & 11 deletions extensions/common/host_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@
struct HostID {
enum HostType { EXTENSIONS, WEBUI };

HostID() {}
HostID(HostType type, const std::string& id)
: type_(type), id_(id) {}

bool operator<(const HostID& host_id) const {
if (type_ != host_id.type())
return type_ < host_id.type();
else if (id_ != host_id.id())
return id_ < host_id.id();
return false;
}
HostID();
HostID(HostType type, const std::string& id);
HostID(const HostID& host_id);
~HostID();

bool operator<(const HostID& host_id) const;
bool operator==(const HostID& host_id) const;

HostType type() const { return type_; }
const std::string& id() const { return id_; }
Expand Down
5 changes: 5 additions & 0 deletions extensions/extensions.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'common/cast/cast_cert_validator.h',
'common/common_manifest_handlers.cc',
'common/common_manifest_handlers.h',
'common/host_id.cc',
'common/host_id.h',
'common/csp_validator.cc',
'common/csp_validator.h',
Expand Down Expand Up @@ -828,6 +829,8 @@
'renderer/extension_groups.h',
'renderer/extension_helper.cc',
'renderer/extension_helper.h',
'renderer/extension_injection_host.cc',
'renderer/extension_injection_host.h',
'renderer/extensions_render_frame_observer.cc',
'renderer/extensions_render_frame_observer.h',
'renderer/extensions_renderer_client.cc',
Expand All @@ -846,6 +849,8 @@
'renderer/i18n_custom_bindings.h',
'renderer/id_generator_custom_bindings.cc',
'renderer/id_generator_custom_bindings.h',
'renderer/injection_host.cc',
'renderer/injection_host.h',
'renderer/lazy_background_page_native_handler.cc',
'renderer/lazy_background_page_native_handler.h',
'renderer/logging_native_handler.cc',
Expand Down
2 changes: 1 addition & 1 deletion extensions/renderer/dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ const Extension* Dispatcher::GetExtensionFromFrameAndWorld(
std::string extension_id;
if (world_id != 0) {
// Isolated worlds (content script).
extension_id = ScriptInjection::GetExtensionIdForIsolatedWorld(world_id);
extension_id = ScriptInjection::GetHostIdForIsolatedWorld(world_id);
} else if (!frame->document().securityOrigin().isUnique()) {
// TODO(kalman): Delete the above check.

Expand Down
58 changes: 58 additions & 0 deletions extensions/renderer/extension_injection_host.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/common/manifest_handlers/csp_info.h"
#include "extensions/renderer/extension_injection_host.h"

namespace extensions {

ExtensionInjectionHost::ExtensionInjectionHost(
const scoped_refptr<const Extension>& extension)
: InjectionHost(HostID(HostID::EXTENSIONS, extension->id())),
extension_(extension) {
}

ExtensionInjectionHost::~ExtensionInjectionHost() {
}

const std::string& ExtensionInjectionHost::GetContentSecurityPolicy() const {
return CSPInfo::GetContentSecurityPolicy(extension_.get());
}

const GURL& ExtensionInjectionHost::url() const {
return extension_->url();
}

const std::string& ExtensionInjectionHost::name() const {
return extension_->name();
}

PermissionsData::AccessType ExtensionInjectionHost::CanExecuteOnFrame(
const GURL& document_url,
const GURL& top_frame_url,
int tab_id,
bool is_declarative) const {
// Declarative user scripts use "page access" (from "permissions" section in
// manifest) whereas non-declarative user scripts use custom
// "content script access" logic.
if (is_declarative) {
return extension_->permissions_data()->GetPageAccess(
extension_.get(),
document_url,
top_frame_url,
tab_id,
-1, // no process id
nullptr /* ignore error */);
} else {
return extension_->permissions_data()->GetContentScriptAccess(
extension_.get(),
document_url,
top_frame_url,
tab_id,
-1, // no process id
nullptr /* ignore error */);
}
}

} // namespace extensions
39 changes: 39 additions & 0 deletions extensions/renderer/extension_injection_host.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_RENDERER_EXTENSION_INJECTION_HOST_H_
#define EXTENSIONS_RENDERER_EXTENSION_INJECTION_HOST_H_

#include "base/memory/ref_counted.h"
#include "extensions/common/extension.h"
#include "extensions/renderer/injection_host.h"

namespace extensions {

// A wrapper class that holds an extension and implements the InjectionHost
// interface.
class ExtensionInjectionHost : public InjectionHost {
public:
ExtensionInjectionHost(const scoped_refptr<const Extension>& extension);
~ExtensionInjectionHost() override;

private:
// InjectionHost:
const std::string& GetContentSecurityPolicy() const override;
const GURL& url() const override;
const std::string& name() const override;
PermissionsData::AccessType CanExecuteOnFrame(
const GURL& document_url,
const GURL& top_frame_url,
int tab_id,
bool is_declarative) const override;

scoped_refptr<const Extension> extension_;

DISALLOW_COPY_AND_ASSIGN(ExtensionInjectionHost);
};

} // namespace extesions

#endif // EXTENSIONS_RENDERER_EXTENSION_INJECTION_HOST_H_
12 changes: 12 additions & 0 deletions extensions/renderer/injection_host.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/injection_host.h"

InjectionHost::InjectionHost(const HostID& host_id) :
id_(host_id) {
}

InjectionHost::~InjectionHost() {
}
41 changes: 41 additions & 0 deletions extensions/renderer/injection_host.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_RENDERER_INJECTION_HOST_H
#define EXTENSIONS_RENDERER_INJECTION_HOST_H

#include "extensions/common/host_id.h"
#include "extensions/common/permissions/permissions_data.h"
#include "url/gurl.h"

// An interface for all kinds of hosts who own user scripts.
class InjectionHost {
public:
InjectionHost(const HostID& host_id);
virtual ~InjectionHost();

virtual const std::string& GetContentSecurityPolicy() const = 0;

// The base url for the host.
virtual const GURL& url() const = 0;

// The human-readable name of the host.
virtual const std::string& name() const = 0;

// Returns true if the script should execute.
virtual extensions::PermissionsData::AccessType CanExecuteOnFrame(
const GURL& document_url,
const GURL& top_frame_url,
int tab_id,
bool is_declarative) const = 0;

const HostID& id() const { return id_; }
private:
// The ID of the host.
HostID id_;

DISALLOW_COPY_AND_ASSIGN(InjectionHost);
};

#endif // EXTENSIONS_RENDERER_INJECTION_HOST_H
12 changes: 4 additions & 8 deletions extensions/renderer/programmatic_script_injector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include "base/values.h"
#include "content/public/renderer/render_view.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_messages.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/renderer/injection_host.h"
#include "extensions/renderer/script_context.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/web/WebDocument.h"
Expand Down Expand Up @@ -66,7 +66,7 @@ bool ProgrammaticScriptInjector::ShouldInjectCss(
}

PermissionsData::AccessType ProgrammaticScriptInjector::CanExecuteOnFrame(
const Extension* extension,
const InjectionHost* injection_host,
blink::WebFrame* frame,
int tab_id,
const GURL& top_url) const {
Expand All @@ -83,12 +83,8 @@ PermissionsData::AccessType ProgrammaticScriptInjector::CanExecuteOnFrame(
: PermissionsData::ACCESS_DENIED;
}

return extension->permissions_data()->GetPageAccess(extension,
effective_document_url,
top_url,
tab_id,
-1, // no process ID.
NULL /* ignore error */);
return injection_host->CanExecuteOnFrame(
effective_document_url, top_url, tab_id, true /* is_declarative */);
}

std::vector<blink::WebScriptSource> ProgrammaticScriptInjector::GetJsSources(
Expand Down
3 changes: 1 addition & 2 deletions extensions/renderer/programmatic_script_injector.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class RenderView;
}

namespace extensions {
class Extension;

// A ScriptInjector to handle tabs.executeScript().
class ProgrammaticScriptInjector : public ScriptInjector {
Expand All @@ -40,7 +39,7 @@ class ProgrammaticScriptInjector : public ScriptInjector {
bool ShouldInjectJs(UserScript::RunLocation run_location) const override;
bool ShouldInjectCss(UserScript::RunLocation run_location) const override;
PermissionsData::AccessType CanExecuteOnFrame(
const Extension* extension,
const InjectionHost* injection_host,
blink::WebFrame* web_frame,
int tab_id,
const GURL& top_url) const override;
Expand Down
Loading

0 comments on commit a5c856c

Please sign in to comment.