blob: bb30c769cff1061eb4268c7a976df210e0dcba03 [file] [log] [blame]
[email protected]8809f1442012-01-20 21:21:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]36fb2c7c2011-04-04 15:49:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]a6394ae2012-07-16 20:58:435#ifndef CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_
6#define CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_
[email protected]36fb2c7c2011-04-04 15:49:087
[email protected]4b64d712013-01-17 17:53:178#include <map>
[email protected]a340bb42014-01-09 07:33:209#include <set>
[email protected]4b64d712013-01-17 17:53:1710#include <string>
[email protected]a714e462013-01-26 06:33:1011#include <vector>
12
[email protected]28a69d32012-05-30 07:58:1813#include "base/memory/ref_counted.h"
[email protected]3d6d676522013-10-14 20:44:5514#include "base/memory/scoped_ptr.h"
[email protected]82a43732011-10-07 16:09:1115#include "base/memory/weak_ptr.h"
[email protected]a83b8402012-05-17 06:56:4416#include "base/observer_list.h"
[email protected]78ce3022012-09-24 01:48:4817#include "chrome/browser/extensions/active_tab_permission_granter.h"
[email protected]93f50462013-05-10 04:40:4018#include "chrome/common/web_application_info.h"
[email protected]619f86182012-07-03 21:30:1819#include "content/public/browser/notification_observer.h"
20#include "content/public/browser/notification_registrar.h"
[email protected]d8c660432011-12-22 20:51:2521#include "content/public/browser/web_contents_observer.h"
[email protected]46b3c982012-10-09 18:38:3022#include "content/public/browser/web_contents_user_data.h"
[email protected]0b9de032014-03-15 05:47:0123#include "extensions/browser/extension_function_dispatcher.h"
[email protected]88b50b62013-09-01 23:05:0624#include "extensions/common/stack_frame.h"
[email protected]36fb2c7c2011-04-04 15:49:0825#include "third_party/skia/include/core/SkBitmap.h"
26
[email protected]898697902013-12-18 03:25:4627class FaviconDownloader;
28
[email protected]8286f51a2011-05-31 17:39:1329namespace content {
30struct LoadCommittedDetails;
31}
32
[email protected]75b91fd2013-02-12 22:14:2533namespace gfx {
34class Image;
35}
36
[email protected]a83b8402012-05-17 06:56:4437namespace extensions {
[email protected]1c321ee52012-05-21 03:02:3438class Extension;
[email protected]4a988162012-05-27 05:30:0139class LocationBarController;
[email protected]09ae70d42012-11-07 00:42:0940class ScriptExecutor;
[email protected]3d6d676522013-10-14 20:44:5541class WebstoreInlineInstallerFactory;
[email protected]a83b8402012-05-17 06:56:4442
[email protected]74e51402011-04-06 14:17:5943// Per-tab extension helper. Also handles non-extension apps.
[email protected]a6394ae2012-07-16 20:58:4344class TabHelper : public content::WebContentsObserver,
45 public ExtensionFunctionDispatcher::Delegate,
[email protected]a6394ae2012-07-16 20:58:4346 public base::SupportsWeakPtr<TabHelper>,
[email protected]7381d9f2012-09-12 20:26:2247 public content::NotificationObserver,
[email protected]46b3c982012-10-09 18:38:3048 public content::WebContentsUserData<TabHelper> {
[email protected]36fb2c7c2011-04-04 15:49:0849 public:
[email protected]619f86182012-07-03 21:30:1850 // Different types of action when web app info is available.
51 // OnDidGetApplicationInfo uses this to dispatch calls.
52 enum WebAppAction {
[email protected]488e3952013-11-18 05:29:1453 NONE, // No action at all.
54 CREATE_SHORTCUT, // Bring up create application shortcut dialog.
55 CREATE_HOSTED_APP, // Create and install a hosted app.
56 UPDATE_SHORTCUT // Update icon for app shortcut.
[email protected]619f86182012-07-03 21:30:1857 };
58
[email protected]09ae70d42012-11-07 00:42:0959 // Observer base class for classes that need to be notified when content
60 // scripts and/or tabs.executeScript calls run on a page.
61 class ScriptExecutionObserver {
[email protected]d5b528c2012-09-27 16:30:2062 public:
63 // Map of extensions IDs to the executing script paths.
64 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap;
65
66 // Automatically observes and unobserves |tab_helper| on construction
67 // and destruction. |tab_helper| must outlive |this|.
[email protected]09ae70d42012-11-07 00:42:0968 explicit ScriptExecutionObserver(TabHelper* tab_helper);
69 ScriptExecutionObserver();
[email protected]d5b528c2012-09-27 16:30:2070
[email protected]09ae70d42012-11-07 00:42:0971 // Called when script(s) have executed on a page.
72 //
73 // |executing_scripts_map| contains all extensions that are executing
74 // scripts, mapped to the paths for those scripts. This may be an empty set
75 // if the script has no path associated with it (e.g. in the case of
76 // tabs.executeScript).
77 virtual void OnScriptsExecuted(
[email protected]d5b528c2012-09-27 16:30:2078 const content::WebContents* web_contents,
79 const ExecutingScriptsMap& executing_scripts_map,
80 int32 on_page_id,
81 const GURL& on_url) = 0;
82
83 protected:
[email protected]09ae70d42012-11-07 00:42:0984 virtual ~ScriptExecutionObserver();
[email protected]d5b528c2012-09-27 16:30:2085
[email protected]d5b528c2012-09-27 16:30:2086 TabHelper* tab_helper_;
87 };
88
[email protected]a340bb42014-01-09 07:33:2089 // This finds the closest not-smaller bitmap in |bitmaps| for each size in
[email protected]669348f2014-01-21 02:33:3690 // |sizes| and resizes it to that size. This returns a map of sizes to bitmaps
[email protected]a340bb42014-01-09 07:33:2091 // which contains only bitmaps of a size in |sizes| and at most one bitmap of
92 // each size.
[email protected]669348f2014-01-21 02:33:3693 static std::map<int, SkBitmap> ConstrainBitmapsToSizes(
[email protected]a340bb42014-01-09 07:33:2094 const std::vector<SkBitmap>& bitmaps,
95 const std::set<int>& sizes);
96
[email protected]669348f2014-01-21 02:33:3697 // Adds a square container icon of |output_size| pixels to |bitmaps| by
98 // centering the biggest smaller icon in |bitmaps| and drawing a rounded
99 // rectangle with strip of the that icon's dominant color at the bottom.
100 // Does nothing if an icon of |output_size| already exists in |bitmaps|.
101 static void GenerateContainerIcon(std::map<int, SkBitmap>* bitmaps,
102 int output_size);
103
[email protected]a6394ae2012-07-16 20:58:43104 virtual ~TabHelper();
[email protected]36fb2c7c2011-04-04 15:49:08105
[email protected]09ae70d42012-11-07 00:42:09106 void AddScriptExecutionObserver(ScriptExecutionObserver* observer) {
107 script_execution_observers_.AddObserver(observer);
[email protected]d5b528c2012-09-27 16:30:20108 }
109
[email protected]09ae70d42012-11-07 00:42:09110 void RemoveScriptExecutionObserver(ScriptExecutionObserver* observer) {
111 script_execution_observers_.RemoveObserver(observer);
[email protected]d5b528c2012-09-27 16:30:20112 }
113
[email protected]619f86182012-07-03 21:30:18114 void CreateApplicationShortcuts();
[email protected]488e3952013-11-18 05:29:14115 void CreateHostedAppFromWebContents();
[email protected]619f86182012-07-03 21:30:18116 bool CanCreateApplicationShortcuts() const;
[email protected]1739e57d2011-11-30 21:18:25117
[email protected]619f86182012-07-03 21:30:18118 void set_pending_web_app_action(WebAppAction action) {
119 pending_web_app_action_ = action;
120 }
[email protected]553602e12011-04-05 17:01:18121
[email protected]36fb2c7c2011-04-04 15:49:08122 // App extensions ------------------------------------------------------------
123
124 // Sets the extension denoting this as an app. If |extension| is non-null this
[email protected]0932b30c2012-04-17 13:25:10125 // tab becomes an app-tab. WebContents does not listen for unload events for
126 // the extension. It's up to consumers of WebContents to do that.
[email protected]36fb2c7c2011-04-04 15:49:08127 //
128 // NOTE: this should only be manipulated before the tab is added to a browser.
129 // TODO(sky): resolve if this is the right way to identify an app tab. If it
130 // is, than this should be passed in the constructor.
[email protected]a6394ae2012-07-16 20:58:43131 void SetExtensionApp(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:08132
133 // Convenience for setting the app extension by id. This does nothing if
134 // |extension_app_id| is empty, or an extension can't be found given the
135 // specified id.
136 void SetExtensionAppById(const std::string& extension_app_id);
137
[email protected]dd290d32012-03-06 02:47:51138 // Set just the app icon, used by panels created by an extension.
139 void SetExtensionAppIconById(const std::string& extension_app_id);
140
[email protected]a6394ae2012-07-16 20:58:43141 const Extension* extension_app() const { return extension_app_; }
[email protected]36fb2c7c2011-04-04 15:49:08142 bool is_app() const { return extension_app_ != NULL; }
[email protected]553602e12011-04-05 17:01:18143 const WebApplicationInfo& web_app_info() const {
144 return web_app_info_;
145 }
[email protected]36fb2c7c2011-04-04 15:49:08146
[email protected]0932b30c2012-04-17 13:25:10147 // If an app extension has been explicitly set for this WebContents its icon
[email protected]36fb2c7c2011-04-04 15:49:08148 // is returned.
149 //
150 // NOTE: the returned icon is larger than 16x16 (its size is
[email protected]1d5e58b2013-01-31 08:41:40151 // extension_misc::EXTENSION_ICON_SMALLISH).
[email protected]36fb2c7c2011-04-04 15:49:08152 SkBitmap* GetExtensionAppIcon();
153
[email protected]ea049a02011-12-25 21:37:09154 content::WebContents* web_contents() const {
155 return content::WebContentsObserver::web_contents();
[email protected]36fb2c7c2011-04-04 15:49:08156 }
157
[email protected]a6394ae2012-07-16 20:58:43158 ScriptExecutor* script_executor() {
[email protected]09ae70d42012-11-07 00:42:09159 return script_executor_.get();
[email protected]af78a802012-07-10 23:47:02160 }
[email protected]a83b8402012-05-17 06:56:44161
[email protected]a6394ae2012-07-16 20:58:43162 LocationBarController* location_bar_controller() {
[email protected]af78a802012-07-10 23:47:02163 return location_bar_controller_.get();
164 }
[email protected]a83b8402012-05-17 06:56:44165
[email protected]78ce3022012-09-24 01:48:48166 ActiveTabPermissionGranter* active_tab_permission_granter() {
167 return active_tab_permission_granter_.get();
[email protected]fc5e65d6b2012-06-13 00:22:57168 }
169
[email protected]0932b30c2012-04-17 13:25:10170 // Sets a non-extension app icon associated with WebContents and fires an
[email protected]d9083482012-01-06 00:38:46171 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
[email protected]74e51402011-04-06 14:17:59172 void SetAppIcon(const SkBitmap& app_icon);
173
[email protected]3d6d676522013-10-14 20:44:55174 // Sets the factory used to create inline webstore item installers.
175 // Used for testing. Takes ownership of the factory instance.
176 void SetWebstoreInlineInstallerFactoryForTests(
177 WebstoreInlineInstallerFactory* factory);
178
[email protected]36fb2c7c2011-04-04 15:49:08179 private:
[email protected]a78f03c2012-09-15 05:08:19180 explicit TabHelper(content::WebContents* web_contents);
[email protected]46b3c982012-10-09 18:38:30181 friend class content::WebContentsUserData<TabHelper>;
[email protected]a78f03c2012-09-15 05:08:19182
[email protected]898697902013-12-18 03:25:46183 // Creates a hosted app for the current tab. Requires the |web_app_info_| to
184 // be populated.
185 void CreateHostedApp();
186 void FinishCreateHostedApp(
187 bool success, const std::map<GURL, std::vector<SkBitmap> >& bitmaps);
188
[email protected]d8c660432011-12-22 20:51:25189 // content::WebContentsObserver overrides.
[email protected]fc5e65d6b2012-06-13 00:22:57190 virtual void RenderViewCreated(
191 content::RenderViewHost* render_view_host) OVERRIDE;
[email protected]a6e16aec2011-11-11 18:53:04192 virtual void DidNavigateMainFrame(
[email protected]8286f51a2011-05-31 17:39:13193 const content::LoadCommittedDetails& details,
[email protected]6766b172011-11-21 18:29:36194 const content::FrameNavigateParams& params) OVERRIDE;
[email protected]8915f342011-08-29 22:14:37195 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
[email protected]7381d9f2012-09-12 20:26:22196 virtual void DidCloneToNewWebContents(
197 content::WebContents* old_web_contents,
198 content::WebContents* new_web_contents) OVERRIDE;
[email protected]553602e12011-04-05 17:01:18199
[email protected]c5dbef02011-05-13 05:06:09200 // ExtensionFunctionDispatcher::Delegate overrides.
[email protected]44f4b132012-07-17 20:36:57201 virtual extensions::WindowController* GetExtensionWindowController()
[email protected]b51f35622012-05-05 22:01:43202 const OVERRIDE;
[email protected]ea049a02011-12-25 21:37:09203 virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
[email protected]c5dbef02011-05-13 05:06:09204
[email protected]553602e12011-04-05 17:01:18205 // Message handlers.
206 void OnDidGetApplicationInfo(int32 page_id, const WebApplicationInfo& info);
[email protected]a221ef092011-09-07 01:34:10207 void OnInlineWebstoreInstall(int install_id,
[email protected]7b921042012-02-11 01:41:27208 int return_route_id,
[email protected]a221ef092011-09-07 01:34:10209 const std::string& webstore_item_id,
210 const GURL& requestor_url);
[email protected]f2fe87c2012-04-24 17:53:49211 void OnGetAppInstallState(const GURL& requestor_url,
212 int return_route_id,
213 int callback_id);
[email protected]c5dbef02011-05-13 05:06:09214 void OnRequest(const ExtensionHostMsg_Request_Params& params);
[email protected]d5b528c2012-09-27 16:30:20215 void OnContentScriptsExecuting(
[email protected]09ae70d42012-11-07 00:42:09216 const ScriptExecutionObserver::ExecutingScriptsMap& extension_ids,
[email protected]d5b528c2012-09-27 16:30:20217 int32 page_id,
218 const GURL& on_url);
[email protected]a714e462013-01-26 06:33:10219 void OnWatchedPageChange(const std::vector<std::string>& css_selectors);
[email protected]88b50b62013-09-01 23:05:06220 void OnDetailedConsoleMessageAdded(const base::string16& message,
221 const base::string16& source,
222 const StackTrace& stack_trace,
223 int32 severity_level);
[email protected]36fb2c7c2011-04-04 15:49:08224
225 // App extensions related methods:
226
[email protected]75b91fd2013-02-12 22:14:25227 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
228 // the extension's image asynchronously.
[email protected]a6394ae2012-07-16 20:58:43229 void UpdateExtensionAppIcon(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:08230
[email protected]7069ed82013-01-26 01:39:53231 const Extension* GetExtension(const std::string& extension_app_id);
[email protected]dd290d32012-03-06 02:47:51232
[email protected]7069ed82013-01-26 01:39:53233 void OnImageLoaded(const gfx::Image& image);
[email protected]8915f342011-08-29 22:14:37234
[email protected]734bcec2012-10-08 20:29:05235 // WebstoreStandaloneInstaller::Callback.
[email protected]d2a639e2012-09-17 07:41:21236 virtual void OnInlineInstallComplete(int install_id,
237 int return_route_id,
238 bool success,
239 const std::string& error);
[email protected]36fb2c7c2011-04-04 15:49:08240
[email protected]619f86182012-07-03 21:30:18241 // content::NotificationObserver.
242 virtual void Observe(int type,
243 const content::NotificationSource& source,
244 const content::NotificationDetails& details) OVERRIDE;
245
246 // Requests application info for the specified page. This is an asynchronous
247 // request. The delegate is notified by way of OnDidGetApplicationInfo when
248 // the data is available.
249 void GetApplicationInfo(int32 page_id);
250
[email protected]2fc1cad12013-05-21 00:41:39251 // Sends our tab ID to |render_view_host|.
252 void SetTabId(content::RenderViewHost* render_view_host);
253
[email protected]36fb2c7c2011-04-04 15:49:08254 // Data for app extensions ---------------------------------------------------
255
[email protected]d5b528c2012-09-27 16:30:20256 // Our content script observers. Declare at top so that it will outlive all
257 // other members, since they might add themselves as observers.
[email protected]09ae70d42012-11-07 00:42:09258 ObserverList<ScriptExecutionObserver> script_execution_observers_;
[email protected]a83b8402012-05-17 06:56:44259
[email protected]36fb2c7c2011-04-04 15:49:08260 // If non-null this tab is an app tab and this is the extension the tab was
261 // created for.
[email protected]a6394ae2012-07-16 20:58:43262 const Extension* extension_app_;
[email protected]36fb2c7c2011-04-04 15:49:08263
[email protected]74e51402011-04-06 14:17:59264 // Icon for extension_app_ (if non-null) or a manually-set icon for
265 // non-extension apps.
[email protected]36fb2c7c2011-04-04 15:49:08266 SkBitmap extension_app_icon_;
267
[email protected]c5dbef02011-05-13 05:06:09268 // Process any extension messages coming from the tab.
269 ExtensionFunctionDispatcher extension_function_dispatcher_;
270
[email protected]553602e12011-04-05 17:01:18271 // Cached web app info data.
272 WebApplicationInfo web_app_info_;
273
[email protected]619f86182012-07-03 21:30:18274 // Which deferred action to perform when OnDidGetApplicationInfo is notified
275 // from a WebContents.
276 WebAppAction pending_web_app_action_;
277
278 content::NotificationRegistrar registrar_;
279
[email protected]09ae70d42012-11-07 00:42:09280 scoped_ptr<ScriptExecutor> script_executor_;
[email protected]af78a802012-07-10 23:47:02281
[email protected]a6394ae2012-07-16 20:58:43282 scoped_ptr<LocationBarController> location_bar_controller_;
[email protected]a83b8402012-05-17 06:56:44283
[email protected]78ce3022012-09-24 01:48:48284 scoped_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_;
[email protected]fc5e65d6b2012-06-13 00:22:57285
[email protected]898697902013-12-18 03:25:46286 scoped_ptr<FaviconDownloader> favicon_downloader_;
287
[email protected]4b64d712013-01-17 17:53:17288 Profile* profile_;
289
[email protected]7069ed82013-01-26 01:39:53290 // Vend weak pointers that can be invalidated to stop in-progress loads.
291 base::WeakPtrFactory<TabHelper> image_loader_ptr_factory_;
292
[email protected]3d6d676522013-10-14 20:44:55293 // Creates WebstoreInlineInstaller instances for inline install triggers.
294 scoped_ptr<WebstoreInlineInstallerFactory> webstore_inline_installer_factory_;
295
[email protected]a6394ae2012-07-16 20:58:43296 DISALLOW_COPY_AND_ASSIGN(TabHelper);
[email protected]36fb2c7c2011-04-04 15:49:08297};
298
[email protected]a6394ae2012-07-16 20:58:43299} // namespace extensions
300
301#endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_