This source file includes following definitions.
- CanRequestResource
- CanRequestExtensionResourceScheme
#include "chrome/renderer/extensions/resource_request_policy.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/manifest_handlers/icons_handler.h"
#include "chrome/common/extensions/manifest_url_handler.h"
#include "chrome/common/url_constants.h"
#include "content/public/common/page_transition_types.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/manifest_handlers/web_accessible_resources_info.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/web/WebConsoleMessage.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "url/gurl.h"
namespace extensions {
bool ResourceRequestPolicy::CanRequestResource(
const GURL& resource_url,
blink::WebFrame* frame,
content::PageTransition transition_type,
const ExtensionSet* loaded_extensions) {
CHECK(resource_url.SchemeIs(extensions::kExtensionScheme));
const Extension* extension =
loaded_extensions->GetExtensionOrAppByURL(resource_url);
if (!extension) {
return true;
}
std::string resource_root_relative_path =
resource_url.path().empty() ? std::string()
: resource_url.path().substr(1);
if (extension->is_hosted_app() &&
!IconsInfo::GetIcons(extension)
.ContainsPath(resource_root_relative_path)) {
LOG(ERROR) << "Denying load of " << resource_url.spec() << " from "
<< "hosted app.";
return false;
}
if (!WebAccessibleResourcesInfo::IsResourceWebAccessible(
extension, resource_url.path()) &&
!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableExtensionsResourceWhitelist)) {
GURL frame_url = frame->document().url();
GURL page_url = frame->top()->document().url();
bool is_empty_origin = frame_url.is_empty();
bool is_own_resource = frame_url.GetOrigin() == extension->url() ||
page_url.GetOrigin() == extension->url();
bool is_dev_tools = page_url.SchemeIs(content::kChromeDevToolsScheme) &&
!ManifestURL::GetDevToolsPage(extension).is_empty();
bool transition_allowed =
!content::PageTransitionIsWebTriggerable(transition_type);
bool is_error_page = frame_url == GURL(content::kUnreachableWebDataURL);
if (!is_empty_origin && !is_own_resource &&
!is_dev_tools && !transition_allowed && !is_error_page) {
std::string message = base::StringPrintf(
"Denying load of %s. Resources must be listed in the "
"web_accessible_resources manifest key in order to be loaded by "
"pages outside the extension.",
resource_url.spec().c_str());
frame->addMessageToConsole(
blink::WebConsoleMessage(blink::WebConsoleMessage::LevelError,
blink::WebString::fromUTF8(message)));
return false;
}
}
return true;
}
bool ResourceRequestPolicy::CanRequestExtensionResourceScheme(
const GURL& resource_url,
blink::WebFrame* frame) {
CHECK(resource_url.SchemeIs(extensions::kExtensionResourceScheme));
GURL frame_url = frame->document().url();
if (!frame_url.is_empty() &&
!frame_url.SchemeIs(extensions::kExtensionScheme)) {
std::string message = base::StringPrintf(
"Denying load of %s. chrome-extension-resources:// can only be "
"loaded from extensions.",
resource_url.spec().c_str());
frame->addMessageToConsole(
blink::WebConsoleMessage(blink::WebConsoleMessage::LevelError,
blink::WebString::fromUTF8(message)));
return false;
}
return true;
}
ResourceRequestPolicy::ResourceRequestPolicy() {
}
}