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.
5 #ifndef EXTENSIONS_BROWSER_EXTENSION_WEB_CONTENTS_OBSERVER_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_WEB_CONTENTS_OBSERVER_H_
10 #include "base/compiler_specific.h"
11 #include "base/macros.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "extensions/browser/extension_function_dispatcher.h"
21 namespace extensions
{
24 // A web contents observer used for renderer and extension processes. Grants the
25 // renderer access to certain URL scheme patterns for extensions and notifies
26 // the renderer that the extension was loaded.
28 // Extension system embedders must create an instance for every extension
29 // WebContents. It must be a subclass so that creating an instance via
30 // content::WebContentsUserData::CreateForWebContents() provides an object of
31 // the correct type. For an example, see ChromeExtensionWebContentsObserver.
32 class ExtensionWebContentsObserver
33 : public content::WebContentsObserver
,
34 public ExtensionFunctionDispatcher::Delegate
{
36 // Returns the ExtensionWebContentsObserver for the given |web_contents|.
37 static ExtensionWebContentsObserver
* GetForWebContents(
38 content::WebContents
* web_contents
);
40 ExtensionFunctionDispatcher
* dispatcher() { return &dispatcher_
; }
43 explicit ExtensionWebContentsObserver(content::WebContents
* web_contents
);
44 ~ExtensionWebContentsObserver() override
;
46 content::BrowserContext
* browser_context() { return browser_context_
; }
48 // Initializes a new render frame. Subclasses should invoke this
49 // implementation if extending.
50 virtual void InitializeRenderFrame(
51 content::RenderFrameHost
* render_frame_host
);
53 // ExtensionFunctionDispatcher::Delegate overrides.
54 content::WebContents
* GetAssociatedWebContents() const override
;
56 // content::WebContentsObserver overrides.
58 // A subclass should invoke this method to finish extension process setup.
59 void RenderViewCreated(content::RenderViewHost
* render_view_host
) override
;
61 void RenderFrameCreated(content::RenderFrameHost
* render_frame_host
) override
;
62 void RenderFrameDeleted(content::RenderFrameHost
* render_frame_host
) override
;
64 // Subclasses should call this first before doing their own message handling.
65 bool OnMessageReceived(const IPC::Message
& message
,
66 content::RenderFrameHost
* render_frame_host
) override
;
68 // Per the documentation in WebContentsObserver, these two methods are invoked
69 // when a Pepper plugin instance is attached/detached in the page DOM.
70 void PepperInstanceCreated() override
;
71 void PepperInstanceDeleted() override
;
73 // Returns the extension id associated with the given |render_frame_host|, or
74 // the empty string if there is none.
75 std::string
GetExtensionIdFromFrame(
76 content::RenderFrameHost
* render_frame_host
) const;
78 // Returns the extension associated with the given |render_frame_host|, or
79 // null if there is none.
80 const Extension
* GetExtensionFromFrame(
81 content::RenderFrameHost
* render_frame_host
) const;
83 // TODO(devlin): Remove these once callers are updated to use the FromFrame
85 // Returns the extension or app associated with a render view host. Returns
86 // NULL if the render view host is not for a valid extension.
87 const Extension
* GetExtension(content::RenderViewHost
* render_view_host
);
88 // Returns the extension or app ID associated with a render view host. Returns
89 // the empty string if the render view host is not for a valid extension.
90 static std::string
GetExtensionId(content::RenderViewHost
* render_view_host
);
93 void OnRequest(content::RenderFrameHost
* render_frame_host
,
94 const ExtensionHostMsg_Request_Params
& params
);
96 // A helper function for initializing render frames at the creation of the
98 void InitializeFrameHelper(content::RenderFrameHost
* render_frame_host
);
100 // The BrowserContext associated with the WebContents being observed.
101 content::BrowserContext
* browser_context_
;
103 ExtensionFunctionDispatcher dispatcher_
;
105 DISALLOW_COPY_AND_ASSIGN(ExtensionWebContentsObserver
);
108 } // namespace extensions
110 #endif // EXTENSIONS_BROWSER_EXTENSION_WEB_CONTENTS_OBSERVER_H_