• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "libcef/browser/extensions/extension_web_contents_observer.h"
6 
7 #include "chrome/common/webui_url_constants.h"
8 #include "content/public/browser/child_process_security_policy.h"
9 #include "content/public/browser/render_frame_host.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/common/url_constants.h"
12 #include "third_party/blink/public/common/chrome_debug_urls.h"
13 
14 namespace extensions {
15 
CefExtensionWebContentsObserver(content::WebContents * web_contents)16 CefExtensionWebContentsObserver::CefExtensionWebContentsObserver(
17     content::WebContents* web_contents)
18     : ExtensionWebContentsObserver(web_contents),
19       content::WebContentsUserData<CefExtensionWebContentsObserver>(
20           *web_contents),
21       script_executor_(new ScriptExecutor(web_contents)) {}
22 
~CefExtensionWebContentsObserver()23 CefExtensionWebContentsObserver::~CefExtensionWebContentsObserver() {}
24 
25 // static
CreateForWebContents(content::WebContents * web_contents)26 void CefExtensionWebContentsObserver::CreateForWebContents(
27     content::WebContents* web_contents) {
28   content::WebContentsUserData<
29       CefExtensionWebContentsObserver>::CreateForWebContents(web_contents);
30 
31   // Initialize this instance if necessary.
32   FromWebContents(web_contents)->Initialize();
33 }
34 
RenderFrameCreated(content::RenderFrameHost * render_frame_host)35 void CefExtensionWebContentsObserver::RenderFrameCreated(
36     content::RenderFrameHost* render_frame_host) {
37   ExtensionWebContentsObserver::RenderFrameCreated(render_frame_host);
38 
39   const Extension* extension = GetExtensionFromFrame(render_frame_host, false);
40   if (!extension)
41     return;
42 
43   int process_id = render_frame_host->GetProcess()->GetID();
44   auto policy = content::ChildProcessSecurityPolicy::GetInstance();
45 
46   // Components of chrome that are implemented as extensions or platform apps
47   // are allowed to use chrome://resources/ and chrome://theme/ URLs.
48   if ((extension->is_extension() || extension->is_platform_app()) &&
49       Manifest::IsComponentLocation(extension->location())) {
50     policy->GrantRequestOrigin(
51         process_id, url::Origin::Create(GURL(blink::kChromeUIResourcesURL)));
52     policy->GrantRequestOrigin(
53         process_id, url::Origin::Create(GURL(chrome::kChromeUIThemeURL)));
54   }
55 }
56 
57 WEB_CONTENTS_USER_DATA_KEY_IMPL(CefExtensionWebContentsObserver);
58 
59 }  // namespace extensions
60