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