diff options
author | jamescook@chromium.org <jamescook@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-20 19:25:48 +0000 |
---|---|---|
committer | jamescook@chromium.org <jamescook@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-20 19:25:48 +0000 |
commit | 1ce1597337ec9459598cc84a6bc3831c2ea3dfac (patch) | |
tree | 348d72c015790f799959ab9e958d8254c705af5a /extensions | |
parent | 998e64060cece98794221895e561970be9a248d0 (diff) | |
download | chromium_src-1ce1597337ec9459598cc84a6bc3831c2ea3dfac.zip chromium_src-1ce1597337ec9459598cc84a6bc3831c2ea3dfac.tar.gz chromium_src-1ce1597337ec9459598cc84a6bc3831c2ea3dfac.tar.bz2 |
Split ExtensionWebContentsObserver into base and Chrome implementations
This allows app_shell to use a simplified ExtensionWebContentsObserver while
Chrome maintains its more complex version.
* Move ExtensionWebContentsObserver to src/extensions
* Introduce ChromeExtensionWebContentsObserver
* Delegate out observer creation from ExtensionHost
Removing this dependency on Chrome will allow ExtensionHost to move into
src/extensions and break another app_shell -> Chrome dependency.
BUG=321341
TEST=browser_tests ErrorConsole* and Extension*
TBR=stevenjb@chromium.org for mechanical changes to chrome/browser/chromeos
TBR=msw@chromium.org for mechanical changes to chrome/browser/ui
Review URL: https://codereview.chromium.org/205243004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@258372 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/browser/extension_web_contents_observer.cc | 111 | ||||
-rw-r--r-- | extensions/browser/extension_web_contents_observer.h | 61 | ||||
-rw-r--r-- | extensions/extensions.gyp | 2 |
3 files changed, 174 insertions, 0 deletions
diff --git a/extensions/browser/extension_web_contents_observer.cc b/extensions/browser/extension_web_contents_observer.cc new file mode 100644 index 0000000..e64fff1 --- /dev/null +++ b/extensions/browser/extension_web_contents_observer.cc @@ -0,0 +1,111 @@ +// Copyright 2014 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/browser/extension_web_contents_observer.h" + +#include "content/public/browser/child_process_security_policy.h" +#include "content/public/browser/render_process_host.h" +#include "content/public/browser/render_view_host.h" +#include "content/public/browser/site_instance.h" +#include "content/public/browser/web_contents.h" +#include "content/public/common/url_constants.h" +#include "extensions/browser/extension_prefs.h" +#include "extensions/browser/extension_registry.h" +#include "extensions/browser/view_type_utils.h" +#include "extensions/common/constants.h" +#include "extensions/common/extension_messages.h" + +namespace extensions { + +ExtensionWebContentsObserver::ExtensionWebContentsObserver( + content::WebContents* web_contents) + : content::WebContentsObserver(web_contents), + browser_context_(web_contents->GetBrowserContext()) {} + +ExtensionWebContentsObserver::~ExtensionWebContentsObserver() {} + +void ExtensionWebContentsObserver::RenderViewCreated( + content::RenderViewHost* render_view_host) { + render_view_host->Send(new ExtensionMsg_NotifyRenderViewType( + render_view_host->GetRoutingID(), GetViewType(web_contents()))); + + const Extension* extension = GetExtension(render_view_host); + if (!extension) + return; + + content::RenderProcessHost* process = render_view_host->GetProcess(); + + // Some extensions use chrome:// URLs. + // This is a temporary solution. Replace it with access to chrome-static:// + // once it is implemented. See: crbug.com/226927. + Manifest::Type type = extension->GetType(); + if (type == Manifest::TYPE_EXTENSION || + type == Manifest::TYPE_LEGACY_PACKAGED_APP || + (type == Manifest::TYPE_PLATFORM_APP && + extension->location() == Manifest::COMPONENT)) { + content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme( + process->GetID(), content::kChromeUIScheme); + } + + // Some extensions use file:// URLs. + if (type == Manifest::TYPE_EXTENSION || + type == Manifest::TYPE_LEGACY_PACKAGED_APP) { + ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_); + if (prefs->AllowFileAccess(extension->id())) { + content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme( + process->GetID(), content::kFileScheme); + } + } + + switch (type) { + case Manifest::TYPE_EXTENSION: + case Manifest::TYPE_USER_SCRIPT: + case Manifest::TYPE_HOSTED_APP: + case Manifest::TYPE_LEGACY_PACKAGED_APP: + case Manifest::TYPE_PLATFORM_APP: + // Always send a Loaded message before ActivateExtension so that + // ExtensionDispatcher knows what Extension is active, not just its ID. + // This is important for classifying the Extension's JavaScript context + // correctly (see ExtensionDispatcher::ClassifyJavaScriptContext). + render_view_host->Send( + new ExtensionMsg_Loaded(std::vector<ExtensionMsg_Loaded_Params>( + 1, ExtensionMsg_Loaded_Params(extension)))); + render_view_host->Send( + new ExtensionMsg_ActivateExtension(extension->id())); + break; + + case Manifest::TYPE_UNKNOWN: + case Manifest::TYPE_THEME: + case Manifest::TYPE_SHARED_MODULE: + break; + } +} + +const Extension* ExtensionWebContentsObserver::GetExtension( + content::RenderViewHost* render_view_host) { + std::string extension_id = GetExtensionId(render_view_host); + if (extension_id.empty()) + return NULL; + + // May be null if the extension doesn't exist, for example if somebody typos + // a chrome-extension:// URL. + return ExtensionRegistry::Get(browser_context_) + ->GetExtensionById(extension_id, ExtensionRegistry::ENABLED); +} + +// static +std::string ExtensionWebContentsObserver::GetExtensionId( + content::RenderViewHost* render_view_host) { + // Note that due to ChromeContentBrowserClient::GetEffectiveURL(), hosted apps + // (excluding bookmark apps) will have a chrome-extension:// URL for their + // site, so we can ignore that wrinkle here. + const GURL& site = render_view_host->GetSiteInstance()->GetSiteURL(); + + if (!site.SchemeIs(kExtensionScheme)) + return std::string(); + + return site.host(); +} + +} // namespace extensions diff --git a/extensions/browser/extension_web_contents_observer.h b/extensions/browser/extension_web_contents_observer.h new file mode 100644 index 0000000..a366e43 --- /dev/null +++ b/extensions/browser/extension_web_contents_observer.h @@ -0,0 +1,61 @@ +// Copyright 2014 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_BROWSER_EXTENSION_WEB_CONTENTS_OBSERVER_H_ +#define EXTENSIONS_BROWSER_EXTENSION_WEB_CONTENTS_OBSERVER_H_ + +#include <string> + +#include "base/compiler_specific.h" +#include "base/macros.h" +#include "content/public/browser/web_contents_observer.h" + +namespace content { +class BrowserContext; +class RenderViewHost; +class WebContents; +} + +namespace extensions { +class Extension; + +// A web contents observer used for renderer and extension processes. Grants the +// renderer access to certain URL scheme patterns for extensions and notifies +// the renderer that the extension was loaded. +// +// Extension system embedders must create an instance for every extension +// WebContents. It must be a subclass so that creating an instance via +// content::WebContentsUserData::CreateForWebContents() provides an object of +// the correct type. For an example, see ChromeExtensionWebContentsObserver. +class ExtensionWebContentsObserver : public content::WebContentsObserver { + protected: + explicit ExtensionWebContentsObserver(content::WebContents* web_contents); + virtual ~ExtensionWebContentsObserver(); + + content::BrowserContext* browser_context() { return browser_context_; } + + // content::WebContentsObserver overrides. + + // A subclass should invoke this method to finish extension process setup. + virtual void RenderViewCreated(content::RenderViewHost* render_view_host) + OVERRIDE; + + // Returns the extension or app associated with a render view host. Returns + // NULL if the render view host is not for a valid extension. + const Extension* GetExtension(content::RenderViewHost* render_view_host); + + // Returns the extension or app ID associated with a render view host. Returns + // the empty string if the render view host is not for a valid extension. + static std::string GetExtensionId(content::RenderViewHost* render_view_host); + + private: + // The BrowserContext associated with the WebContents being observed. + content::BrowserContext* browser_context_; + + DISALLOW_COPY_AND_ASSIGN(ExtensionWebContentsObserver); +}; + +} // namespace extensions + +#endif // EXTENSIONS_BROWSER_EXTENSION_WEB_CONTENTS_OBSERVER_H_ diff --git a/extensions/extensions.gyp b/extensions/extensions.gyp index 4abb2b3..bcccc7c 100644 --- a/extensions/extensions.gyp +++ b/extensions/extensions.gyp @@ -266,6 +266,8 @@ 'browser/extension_system.h', 'browser/extension_system_provider.cc', 'browser/extension_system_provider.h', + 'browser/extension_web_contents_observer.cc', + 'browser/extension_web_contents_observer.h', 'browser/extensions_browser_client.cc', 'browser/extensions_browser_client.h', 'browser/external_provider_interface.h', |