Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / tab_helper.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_TAB_HELPER_H_
6 #define CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_
7
8 #include <set>
9 #include <string>
10 #include <vector>
11
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "chrome/browser/extensions/active_tab_permission_granter.h"
17 #include "chrome/common/extensions/webstore_install_result.h"
18 #include "chrome/common/web_application_info.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/web_contents_observer.h"
22 #include "content/public/browser/web_contents_user_data.h"
23 #include "extensions/browser/extension_function_dispatcher.h"
24 #include "extensions/browser/script_execution_observer.h"
25 #include "extensions/browser/script_executor.h"
26 #include "extensions/common/stack_frame.h"
27 #include "third_party/skia/include/core/SkBitmap.h"
28
29 class FaviconDownloader;
30
31 namespace content {
32 struct LoadCommittedDetails;
33 class RenderFrameHost;
34 }
35
36 namespace gfx {
37 class Image;
38 }
39
40 namespace extensions {
41 class BookmarkAppHelper;
42 class Extension;
43 class LocationBarController;
44 class WebstoreInlineInstallerFactory;
45
46 // Per-tab extension helper. Also handles non-extension apps.
47 class TabHelper : public content::WebContentsObserver,
48                   public extensions::ExtensionFunctionDispatcher::Delegate,
49                   public base::SupportsWeakPtr<TabHelper>,
50                   public content::NotificationObserver,
51                   public content::WebContentsUserData<TabHelper> {
52  public:
53   virtual ~TabHelper();
54
55   void CreateApplicationShortcuts();
56   void CreateHostedAppFromWebContents();
57   bool CanCreateApplicationShortcuts() const;
58   bool CanCreateBookmarkApp() const;
59
60   void UpdateShortcutOnLoadComplete() {
61     update_shortcut_on_load_complete_ = true;
62   }
63
64   // ScriptExecutionObserver::Delegate
65   virtual void AddScriptExecutionObserver(ScriptExecutionObserver* observer);
66   virtual void RemoveScriptExecutionObserver(ScriptExecutionObserver* observer);
67
68   // App extensions ------------------------------------------------------------
69
70   // Sets the extension denoting this as an app. If |extension| is non-null this
71   // tab becomes an app-tab. WebContents does not listen for unload events for
72   // the extension. It's up to consumers of WebContents to do that.
73   //
74   // NOTE: this should only be manipulated before the tab is added to a browser.
75   // TODO(sky): resolve if this is the right way to identify an app tab. If it
76   // is, than this should be passed in the constructor.
77   void SetExtensionApp(const Extension* extension);
78
79   // Convenience for setting the app extension by id. This does nothing if
80   // |extension_app_id| is empty, or an extension can't be found given the
81   // specified id.
82   void SetExtensionAppById(const std::string& extension_app_id);
83
84   // Set just the app icon, used by panels created by an extension.
85   void SetExtensionAppIconById(const std::string& extension_app_id);
86
87   const Extension* extension_app() const { return extension_app_; }
88   bool is_app() const { return extension_app_ != NULL; }
89   const WebApplicationInfo& web_app_info() const {
90     return web_app_info_;
91   }
92
93   // If an app extension has been explicitly set for this WebContents its icon
94   // is returned.
95   //
96   // NOTE: the returned icon is larger than 16x16 (its size is
97   // extension_misc::EXTENSION_ICON_SMALLISH).
98   SkBitmap* GetExtensionAppIcon();
99
100   content::WebContents* web_contents() const {
101     return content::WebContentsObserver::web_contents();
102   }
103
104   ScriptExecutor* script_executor() {
105     return script_executor_.get();
106   }
107
108   LocationBarController* location_bar_controller() {
109     return location_bar_controller_.get();
110   }
111
112   ActiveTabPermissionGranter* active_tab_permission_granter() {
113     return active_tab_permission_granter_.get();
114   }
115
116   // Sets a non-extension app icon associated with WebContents and fires an
117   // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
118   void SetAppIcon(const SkBitmap& app_icon);
119
120   // Sets the factory used to create inline webstore item installers.
121   // Used for testing. Takes ownership of the factory instance.
122   void SetWebstoreInlineInstallerFactoryForTests(
123       WebstoreInlineInstallerFactory* factory);
124
125  private:
126   // Different types of action when web app info is available.
127   // OnDidGetApplicationInfo uses this to dispatch calls.
128   enum WebAppAction {
129     NONE,               // No action at all.
130     CREATE_SHORTCUT,    // Bring up create application shortcut dialog.
131     CREATE_HOSTED_APP,  // Create and install a hosted app.
132     UPDATE_SHORTCUT     // Update icon for app shortcut.
133   };
134
135   explicit TabHelper(content::WebContents* web_contents);
136   friend class content::WebContentsUserData<TabHelper>;
137
138   // Displays UI for completion of creating a bookmark hosted app.
139   void FinishCreateBookmarkApp(const extensions::Extension* extension,
140                                const WebApplicationInfo& web_app_info);
141
142   // content::WebContentsObserver overrides.
143   virtual void RenderViewCreated(
144       content::RenderViewHost* render_view_host) OVERRIDE;
145   virtual void DidNavigateMainFrame(
146       const content::LoadCommittedDetails& details,
147       const content::FrameNavigateParams& params) OVERRIDE;
148   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
149   virtual bool OnMessageReceived(
150       const IPC::Message& message,
151       content::RenderFrameHost* render_frame_host) OVERRIDE;
152   virtual void DidCloneToNewWebContents(
153       content::WebContents* old_web_contents,
154       content::WebContents* new_web_contents) OVERRIDE;
155
156   // extensions::ExtensionFunctionDispatcher::Delegate overrides.
157   virtual extensions::WindowController* GetExtensionWindowController()
158       const OVERRIDE;
159   virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
160
161   // Message handlers.
162   void OnDidGetApplicationInfo(const WebApplicationInfo& info);
163   void OnInlineWebstoreInstall(int install_id,
164                                int return_route_id,
165                                const std::string& webstore_item_id,
166                                const GURL& requestor_url,
167                                int listeners_mask);
168   void OnGetAppInstallState(const GURL& requestor_url,
169                             int return_route_id,
170                             int callback_id);
171   void OnRequest(const ExtensionHostMsg_Request_Params& params);
172   void OnContentScriptsExecuting(
173       const ScriptExecutionObserver::ExecutingScriptsMap& extension_ids,
174       const GURL& on_url);
175   void OnWatchedPageChange(const std::vector<std::string>& css_selectors);
176   void OnDetailedConsoleMessageAdded(const base::string16& message,
177                                      const base::string16& source,
178                                      const StackTrace& stack_trace,
179                                      int32 severity_level);
180
181   // App extensions related methods:
182
183   // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
184   // the extension's image asynchronously.
185   void UpdateExtensionAppIcon(const Extension* extension);
186
187   const Extension* GetExtension(const std::string& extension_app_id);
188
189   void OnImageLoaded(const gfx::Image& image);
190
191   // WebstoreStandaloneInstaller::Callback.
192   virtual void OnInlineInstallComplete(int install_id,
193                                        int return_route_id,
194                                        bool success,
195                                        const std::string& error,
196                                        webstore_install::Result result);
197
198   // content::NotificationObserver.
199   virtual void Observe(int type,
200                        const content::NotificationSource& source,
201                        const content::NotificationDetails& details) OVERRIDE;
202
203   // Requests application info for the specified page. This is an asynchronous
204   // request. The delegate is notified by way of OnDidGetApplicationInfo when
205   // the data is available.
206   void GetApplicationInfo(WebAppAction action);
207
208   // Sends our tab ID to |render_view_host|.
209   void SetTabId(content::RenderViewHost* render_view_host);
210
211   // Data for app extensions ---------------------------------------------------
212
213   // Our content script observers. Declare at top so that it will outlive all
214   // other members, since they might add themselves as observers.
215   ObserverList<ScriptExecutionObserver> script_execution_observers_;
216
217   // If non-null this tab is an app tab and this is the extension the tab was
218   // created for.
219   const Extension* extension_app_;
220
221   // Icon for extension_app_ (if non-null) or a manually-set icon for
222   // non-extension apps.
223   SkBitmap extension_app_icon_;
224
225   // Process any extension messages coming from the tab.
226   extensions::ExtensionFunctionDispatcher extension_function_dispatcher_;
227
228   // Cached web app info data.
229   WebApplicationInfo web_app_info_;
230
231   // Which deferred action to perform when OnDidGetApplicationInfo is notified
232   // from a WebContents.
233   WebAppAction pending_web_app_action_;
234
235   // Which page id was active when the GetApplicationInfo request was sent, for
236   // verification when the reply returns.
237   int32 last_committed_page_id_;
238
239   // Whether to trigger an update when the page load completes.
240   bool update_shortcut_on_load_complete_;
241
242   content::NotificationRegistrar registrar_;
243
244   scoped_ptr<ScriptExecutor> script_executor_;
245
246   scoped_ptr<LocationBarController> location_bar_controller_;
247
248   scoped_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_;
249
250   scoped_ptr<BookmarkAppHelper> bookmark_app_helper_;
251
252   Profile* profile_;
253
254   // Vend weak pointers that can be invalidated to stop in-progress loads.
255   base::WeakPtrFactory<TabHelper> image_loader_ptr_factory_;
256
257   // Creates WebstoreInlineInstaller instances for inline install triggers.
258   scoped_ptr<WebstoreInlineInstallerFactory> webstore_inline_installer_factory_;
259
260   DISALLOW_COPY_AND_ASSIGN(TabHelper);
261 };
262
263 }  // namespace extensions
264
265 #endif  // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_