[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / extensions / browser / extension_host.h
blob567499ba06ff4a3c8c3cca1eeb420eba9159eb41
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_HOST_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_HOST_H_
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/timer/elapsed_timer.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/web_contents_delegate.h"
19 #include "content/public/browser/web_contents_observer.h"
20 #include "extensions/browser/extension_function_dispatcher.h"
21 #include "extensions/common/stack_frame.h"
22 #include "extensions/common/view_type.h"
24 class PrefsTabHelper;
26 namespace content {
27 class BrowserContext;
28 class RenderProcessHost;
29 class RenderWidgetHostView;
30 class SiteInstance;
33 namespace extensions {
34 class Extension;
35 class ExtensionHostDelegate;
36 class ExtensionHostObserver;
37 class WindowController;
39 // This class is the browser component of an extension component's RenderView.
40 // It handles setting up the renderer process, if needed, with special
41 // privileges available to extensions. It may have a view to be shown in the
42 // browser UI, or it may be hidden.
44 // If you are adding code that only affects visible extension views (and not
45 // invisible background pages) you should add it to ExtensionViewHost.
46 class ExtensionHost : public content::WebContentsDelegate,
47 public content::WebContentsObserver,
48 public ExtensionFunctionDispatcher::Delegate,
49 public content::NotificationObserver {
50 public:
51 class ProcessCreationQueue;
53 ExtensionHost(const Extension* extension,
54 content::SiteInstance* site_instance,
55 const GURL& url, ViewType host_type);
56 ~ExtensionHost() override;
58 const Extension* extension() const { return extension_; }
59 const std::string& extension_id() const { return extension_id_; }
60 content::WebContents* host_contents() const { return host_contents_.get(); }
61 content::RenderViewHost* render_view_host() const;
62 content::RenderProcessHost* render_process_host() const;
63 bool did_stop_loading() const { return did_stop_loading_; }
64 bool document_element_available() const {
65 return document_element_available_;
68 content::BrowserContext* browser_context() { return browser_context_; }
70 ViewType extension_host_type() const { return extension_host_type_; }
71 const GURL& GetURL() const;
73 // Returns true if the render view is initialized and didn't crash.
74 bool IsRenderViewLive() const;
76 // Prepares to initializes our RenderViewHost by creating its RenderView and
77 // navigating to this host's url. Uses host_view for the RenderViewHost's view
78 // (can be NULL). This happens delayed to avoid locking the UI.
79 void CreateRenderViewSoon();
81 // Typical observer interface.
82 void AddObserver(ExtensionHostObserver* observer);
83 void RemoveObserver(ExtensionHostObserver* observer);
85 // Called when an IPC message is dispatched to the RenderView associated with
86 // this ExtensionHost.
87 void OnMessageDispatched(const std::string& event_name, int message_id);
89 // Called by the ProcessManager when a network request is started by the
90 // extension corresponding to this ExtensionHost.
91 void OnNetworkRequestStarted(uint64 request_id);
93 // Called by the ProcessManager when a previously started network request is
94 // finished.
95 void OnNetworkRequestDone(uint64 request_id);
97 // content::WebContentsObserver
98 bool OnMessageReceived(const IPC::Message& message) override;
99 void RenderViewCreated(content::RenderViewHost* render_view_host) override;
100 void RenderViewDeleted(content::RenderViewHost* render_view_host) override;
101 void RenderViewReady() override;
102 void RenderProcessGone(base::TerminationStatus status) override;
103 void DocumentAvailableInMainFrame() override;
104 void DidStopLoading(content::RenderViewHost* render_view_host) override;
106 // content::WebContentsDelegate
107 content::JavaScriptDialogManager* GetJavaScriptDialogManager(
108 content::WebContents* source) override;
109 void AddNewContents(content::WebContents* source,
110 content::WebContents* new_contents,
111 WindowOpenDisposition disposition,
112 const gfx::Rect& initial_rect,
113 bool user_gesture,
114 bool* was_blocked) override;
115 void CloseContents(content::WebContents* contents) override;
116 void RequestMediaAccessPermission(
117 content::WebContents* web_contents,
118 const content::MediaStreamRequest& request,
119 const content::MediaResponseCallback& callback) override;
120 bool CheckMediaAccessPermission(content::WebContents* web_contents,
121 const GURL& security_origin,
122 content::MediaStreamType type) override;
123 bool IsNeverVisible(content::WebContents* web_contents) override;
125 // content::NotificationObserver
126 void Observe(int type,
127 const content::NotificationSource& source,
128 const content::NotificationDetails& details) override;
130 protected:
131 content::NotificationRegistrar* registrar() { return &registrar_; }
133 // Called after the extension page finishes loading but before the
134 // EXTENSION_HOST_DID_STOP_LOADING notification is sent.
135 virtual void OnDidStopLoading();
137 // Called once when the document first becomes available.
138 virtual void OnDocumentAvailable();
140 // Navigates to the initial page.
141 virtual void LoadInitialURL();
143 // Returns true if we're hosting a background page.
144 virtual bool IsBackgroundPage() const;
146 // Closes this host (results in deletion).
147 void Close();
149 private:
150 friend class ProcessCreationQueue;
152 // Actually create the RenderView for this host. See CreateRenderViewSoon.
153 void CreateRenderViewNow();
155 // Message handlers.
156 void OnRequest(const ExtensionHostMsg_Request_Params& params);
157 void OnEventAck(int message_id);
158 void OnIncrementLazyKeepaliveCount();
159 void OnDecrementLazyKeepaliveCount();
161 // Delegate for functionality that cannot exist in the extensions module.
162 scoped_ptr<ExtensionHostDelegate> delegate_;
164 // The extension that we're hosting in this view.
165 const Extension* extension_;
167 // Id of extension that we're hosting in this view.
168 const std::string extension_id_;
170 // The browser context that this host is tied to.
171 content::BrowserContext* browser_context_;
173 // The host for our HTML content.
174 scoped_ptr<content::WebContents> host_contents_;
176 // A weak pointer to the current or pending RenderViewHost. We don't access
177 // this through the host_contents because we want to deal with the pending
178 // host, so we can send messages to it before it finishes loading.
179 content::RenderViewHost* render_view_host_;
181 // Whether the RenderWidget has reported that it has stopped loading.
182 bool did_stop_loading_;
184 // True if the main frame has finished parsing.
185 bool document_element_available_;
187 // The original URL of the page being hosted.
188 GURL initial_url_;
190 // Messages sent out to the renderer that have not been acknowledged yet.
191 std::set<int> unacked_messages_;
193 content::NotificationRegistrar registrar_;
195 ExtensionFunctionDispatcher extension_function_dispatcher_;
197 // The type of view being hosted.
198 ViewType extension_host_type_;
200 // Used to measure how long it's been since the host was created.
201 base::ElapsedTimer since_created_;
203 ObserverList<ExtensionHostObserver> observer_list_;
205 DISALLOW_COPY_AND_ASSIGN(ExtensionHost);
208 } // namespace extensions
210 #endif // EXTENSIONS_BROWSER_EXTENSION_HOST_H_