OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/timer/elapsed_timer.h" | |
14 #include "content/public/browser/notification_observer.h" | |
15 #include "content/public/browser/notification_registrar.h" | |
16 #include "content/public/browser/web_contents_delegate.h" | |
17 #include "content/public/browser/web_contents_observer.h" | |
18 #include "extensions/browser/extension_function_dispatcher.h" | |
19 #include "extensions/common/stack_frame.h" | |
20 #include "extensions/common/view_type.h" | |
21 | |
22 class PrefsTabHelper; | |
23 | |
24 namespace content { | |
25 class BrowserContext; | |
26 class RenderProcessHost; | |
27 class RenderWidgetHostView; | |
28 class SiteInstance; | |
29 } | |
30 | |
31 namespace extensions { | |
32 class Extension; | |
33 class ExtensionHostDelegate; | |
34 class WindowController; | |
35 | |
36 // This class is the browser component of an extension component's RenderView. | |
37 // It handles setting up the renderer process, if needed, with special | |
38 // privileges available to extensions. It may have a view to be shown in the | |
39 // browser UI, or it may be hidden. | |
40 class ExtensionHost : public content::WebContentsDelegate, | |
41 public content::WebContentsObserver, | |
42 public ExtensionFunctionDispatcher::Delegate, | |
43 public content::NotificationObserver { | |
44 public: | |
45 class ProcessCreationQueue; | |
46 | |
47 ExtensionHost(const Extension* extension, | |
48 content::SiteInstance* site_instance, | |
49 const GURL& url, ViewType host_type); | |
50 virtual ~ExtensionHost(); | |
51 | |
52 const Extension* extension() const { return extension_; } | |
53 const std::string& extension_id() const { return extension_id_; } | |
54 content::WebContents* host_contents() const { return host_contents_.get(); } | |
55 content::RenderViewHost* render_view_host() const; | |
56 content::RenderProcessHost* render_process_host() const; | |
57 bool did_stop_loading() const { return did_stop_loading_; } | |
58 bool document_element_available() const { | |
59 return document_element_available_; | |
60 } | |
61 | |
62 content::BrowserContext* browser_context() { return browser_context_; } | |
63 | |
64 ViewType extension_host_type() const { return extension_host_type_; } | |
65 const GURL& GetURL() const; | |
66 | |
67 // Returns true if the render view is initialized and didn't crash. | |
68 bool IsRenderViewLive() const; | |
69 | |
70 // Prepares to initializes our RenderViewHost by creating its RenderView and | |
71 // navigating to this host's url. Uses host_view for the RenderViewHost's view | |
72 // (can be NULL). This happens delayed to avoid locking the UI. | |
73 void CreateRenderViewSoon(); | |
74 | |
75 // content::WebContentsObserver | |
76 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
77 virtual void RenderViewCreated( | |
78 content::RenderViewHost* render_view_host) OVERRIDE; | |
79 virtual void RenderViewDeleted( | |
80 content::RenderViewHost* render_view_host) OVERRIDE; | |
81 virtual void RenderViewReady() OVERRIDE; | |
82 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; | |
83 virtual void DocumentAvailableInMainFrame() OVERRIDE; | |
84 virtual void DidStopLoading( | |
85 content::RenderViewHost* render_view_host) OVERRIDE; | |
86 | |
87 // content::WebContentsDelegate | |
88 virtual content::JavaScriptDialogManager* | |
89 GetJavaScriptDialogManager() OVERRIDE; | |
90 virtual void AddNewContents(content::WebContents* source, | |
91 content::WebContents* new_contents, | |
92 WindowOpenDisposition disposition, | |
93 const gfx::Rect& initial_pos, | |
94 bool user_gesture, | |
95 bool* was_blocked) OVERRIDE; | |
96 virtual void CloseContents(content::WebContents* contents) OVERRIDE; | |
97 virtual void RequestMediaAccessPermission( | |
98 content::WebContents* web_contents, | |
99 const content::MediaStreamRequest& request, | |
100 const content::MediaResponseCallback& callback) OVERRIDE; | |
101 virtual bool PreHandleGestureEvent( | |
102 content::WebContents* source, | |
103 const blink::WebGestureEvent& event) OVERRIDE; | |
104 | |
105 // content::NotificationObserver | |
106 virtual void Observe(int type, | |
107 const content::NotificationSource& source, | |
108 const content::NotificationDetails& details) OVERRIDE; | |
109 | |
110 protected: | |
111 content::NotificationRegistrar* registrar() { return ®istrar_; } | |
112 | |
113 // Called after the extension page finishes loading but before the | |
114 // EXTENSION_HOST_DID_STOP_LOADING notification is sent. | |
115 virtual void OnDidStopLoading(); | |
116 | |
117 // Called once when the document first becomes available. | |
118 virtual void OnDocumentAvailable(); | |
119 | |
120 // Navigates to the initial page. | |
121 virtual void LoadInitialURL(); | |
122 | |
123 // Returns true if we're hosting a background page. | |
124 virtual bool IsBackgroundPage() const; | |
125 | |
126 // Closes this host (results in deletion). | |
127 void Close(); | |
128 | |
129 private: | |
130 friend class ProcessCreationQueue; | |
131 | |
132 // Actually create the RenderView for this host. See CreateRenderViewSoon. | |
133 void CreateRenderViewNow(); | |
134 | |
135 // Message handlers. | |
136 void OnRequest(const ExtensionHostMsg_Request_Params& params); | |
137 void OnEventAck(); | |
138 void OnIncrementLazyKeepaliveCount(); | |
139 void OnDecrementLazyKeepaliveCount(); | |
140 | |
141 // Delegate for functionality that cannot exist in the extensions module. | |
142 scoped_ptr<ExtensionHostDelegate> delegate_; | |
143 | |
144 // The extension that we're hosting in this view. | |
145 const Extension* extension_; | |
146 | |
147 // Id of extension that we're hosting in this view. | |
148 const std::string extension_id_; | |
149 | |
150 // The browser context that this host is tied to. | |
151 content::BrowserContext* browser_context_; | |
152 | |
153 // The host for our HTML content. | |
154 scoped_ptr<content::WebContents> host_contents_; | |
155 | |
156 // A weak pointer to the current or pending RenderViewHost. We don't access | |
157 // this through the host_contents because we want to deal with the pending | |
158 // host, so we can send messages to it before it finishes loading. | |
159 content::RenderViewHost* render_view_host_; | |
160 | |
161 // Whether the RenderWidget has reported that it has stopped loading. | |
162 bool did_stop_loading_; | |
163 | |
164 // True if the main frame has finished parsing. | |
165 bool document_element_available_; | |
166 | |
167 // The original URL of the page being hosted. | |
168 GURL initial_url_; | |
169 | |
170 content::NotificationRegistrar registrar_; | |
171 | |
172 ExtensionFunctionDispatcher extension_function_dispatcher_; | |
173 | |
174 // The type of view being hosted. | |
175 ViewType extension_host_type_; | |
176 | |
177 // Used to measure how long it's been since the host was created. | |
178 base::ElapsedTimer since_created_; | |
179 | |
180 DISALLOW_COPY_AND_ASSIGN(ExtensionHost); | |
181 }; | |
182 | |
183 } // namespace extensions | |
184 | |
185 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_ | |
OLD | NEW |