NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / tab_helper.h
blobf711bacfc213ea8d2edc8c1fb6e57f54607831fb
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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_
6 #define CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "chrome/browser/extensions/active_tab_permission_granter.h"
18 #include "chrome/browser/extensions/extension_function_dispatcher.h"
19 #include "chrome/common/web_application_info.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "content/public/browser/web_contents_observer.h"
23 #include "content/public/browser/web_contents_user_data.h"
24 #include "extensions/common/stack_frame.h"
25 #include "third_party/skia/include/core/SkBitmap.h"
27 class FaviconDownloader;
29 namespace content {
30 struct LoadCommittedDetails;
33 namespace gfx {
34 class Image;
37 namespace extensions {
38 class Extension;
39 class LocationBarController;
40 class ScriptExecutor;
41 class WebstoreInlineInstallerFactory;
43 // Per-tab extension helper. Also handles non-extension apps.
44 class TabHelper : public content::WebContentsObserver,
45 public ExtensionFunctionDispatcher::Delegate,
46 public base::SupportsWeakPtr<TabHelper>,
47 public content::NotificationObserver,
48 public content::WebContentsUserData<TabHelper> {
49 public:
50 // Different types of action when web app info is available.
51 // OnDidGetApplicationInfo uses this to dispatch calls.
52 enum WebAppAction {
53 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.
59 // 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 {
62 public:
63 // Map of extensions IDs to the executing script paths.
64 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap;
66 // Automatically observes and unobserves |tab_helper| on construction
67 // and destruction. |tab_helper| must outlive |this|.
68 explicit ScriptExecutionObserver(TabHelper* tab_helper);
69 ScriptExecutionObserver();
71 // Called when script(s) have executed on a page.
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(
78 const content::WebContents* web_contents,
79 const ExecutingScriptsMap& executing_scripts_map,
80 int32 on_page_id,
81 const GURL& on_url) = 0;
83 protected:
84 virtual ~ScriptExecutionObserver();
86 TabHelper* tab_helper_;
89 // This finds the closest not-smaller bitmap in |bitmaps| for each size in
90 // |sizes| and resizes it to that size. This returns a map of sizes to bitmaps
91 // which contains only bitmaps of a size in |sizes| and at most one bitmap of
92 // each size.
93 static std::map<int, SkBitmap> ConstrainBitmapsToSizes(
94 const std::vector<SkBitmap>& bitmaps,
95 const std::set<int>& sizes);
97 // 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);
104 virtual ~TabHelper();
106 void AddScriptExecutionObserver(ScriptExecutionObserver* observer) {
107 script_execution_observers_.AddObserver(observer);
110 void RemoveScriptExecutionObserver(ScriptExecutionObserver* observer) {
111 script_execution_observers_.RemoveObserver(observer);
114 void CreateApplicationShortcuts();
115 void CreateHostedAppFromWebContents();
116 bool CanCreateApplicationShortcuts() const;
118 void set_pending_web_app_action(WebAppAction action) {
119 pending_web_app_action_ = action;
122 // App extensions ------------------------------------------------------------
124 // Sets the extension denoting this as an app. If |extension| is non-null this
125 // 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.
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.
131 void SetExtensionApp(const Extension* extension);
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);
138 // Set just the app icon, used by panels created by an extension.
139 void SetExtensionAppIconById(const std::string& extension_app_id);
141 const Extension* extension_app() const { return extension_app_; }
142 bool is_app() const { return extension_app_ != NULL; }
143 const WebApplicationInfo& web_app_info() const {
144 return web_app_info_;
147 // If an app extension has been explicitly set for this WebContents its icon
148 // is returned.
150 // NOTE: the returned icon is larger than 16x16 (its size is
151 // extension_misc::EXTENSION_ICON_SMALLISH).
152 SkBitmap* GetExtensionAppIcon();
154 content::WebContents* web_contents() const {
155 return content::WebContentsObserver::web_contents();
158 ScriptExecutor* script_executor() {
159 return script_executor_.get();
162 LocationBarController* location_bar_controller() {
163 return location_bar_controller_.get();
166 ActiveTabPermissionGranter* active_tab_permission_granter() {
167 return active_tab_permission_granter_.get();
170 // Sets a non-extension app icon associated with WebContents and fires an
171 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
172 void SetAppIcon(const SkBitmap& app_icon);
174 // 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);
179 private:
180 explicit TabHelper(content::WebContents* web_contents);
181 friend class content::WebContentsUserData<TabHelper>;
183 // 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);
189 // content::WebContentsObserver overrides.
190 virtual void RenderViewCreated(
191 content::RenderViewHost* render_view_host) OVERRIDE;
192 virtual void DidNavigateMainFrame(
193 const content::LoadCommittedDetails& details,
194 const content::FrameNavigateParams& params) OVERRIDE;
195 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
196 virtual void DidCloneToNewWebContents(
197 content::WebContents* old_web_contents,
198 content::WebContents* new_web_contents) OVERRIDE;
200 // ExtensionFunctionDispatcher::Delegate overrides.
201 virtual extensions::WindowController* GetExtensionWindowController()
202 const OVERRIDE;
203 virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
205 // Message handlers.
206 void OnDidGetApplicationInfo(int32 page_id, const WebApplicationInfo& info);
207 void OnInlineWebstoreInstall(int install_id,
208 int return_route_id,
209 const std::string& webstore_item_id,
210 const GURL& requestor_url);
211 void OnGetAppInstallState(const GURL& requestor_url,
212 int return_route_id,
213 int callback_id);
214 void OnRequest(const ExtensionHostMsg_Request_Params& params);
215 void OnContentScriptsExecuting(
216 const ScriptExecutionObserver::ExecutingScriptsMap& extension_ids,
217 int32 page_id,
218 const GURL& on_url);
219 void OnWatchedPageChange(const std::vector<std::string>& css_selectors);
220 void OnDetailedConsoleMessageAdded(const base::string16& message,
221 const base::string16& source,
222 const StackTrace& stack_trace,
223 int32 severity_level);
225 // App extensions related methods:
227 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
228 // the extension's image asynchronously.
229 void UpdateExtensionAppIcon(const Extension* extension);
231 const Extension* GetExtension(const std::string& extension_app_id);
233 void OnImageLoaded(const gfx::Image& image);
235 // WebstoreStandaloneInstaller::Callback.
236 virtual void OnInlineInstallComplete(int install_id,
237 int return_route_id,
238 bool success,
239 const std::string& error);
241 // content::NotificationObserver.
242 virtual void Observe(int type,
243 const content::NotificationSource& source,
244 const content::NotificationDetails& details) OVERRIDE;
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);
251 // Sends our tab ID to |render_view_host|.
252 void SetTabId(content::RenderViewHost* render_view_host);
254 // Data for app extensions ---------------------------------------------------
256 // Our content script observers. Declare at top so that it will outlive all
257 // other members, since they might add themselves as observers.
258 ObserverList<ScriptExecutionObserver> script_execution_observers_;
260 // If non-null this tab is an app tab and this is the extension the tab was
261 // created for.
262 const Extension* extension_app_;
264 // Icon for extension_app_ (if non-null) or a manually-set icon for
265 // non-extension apps.
266 SkBitmap extension_app_icon_;
268 // Process any extension messages coming from the tab.
269 ExtensionFunctionDispatcher extension_function_dispatcher_;
271 // Cached web app info data.
272 WebApplicationInfo web_app_info_;
274 // Which deferred action to perform when OnDidGetApplicationInfo is notified
275 // from a WebContents.
276 WebAppAction pending_web_app_action_;
278 content::NotificationRegistrar registrar_;
280 scoped_ptr<ScriptExecutor> script_executor_;
282 scoped_ptr<LocationBarController> location_bar_controller_;
284 scoped_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_;
286 scoped_ptr<FaviconDownloader> favicon_downloader_;
288 Profile* profile_;
290 // Vend weak pointers that can be invalidated to stop in-progress loads.
291 base::WeakPtrFactory<TabHelper> image_loader_ptr_factory_;
293 // Creates WebstoreInlineInstaller instances for inline install triggers.
294 scoped_ptr<WebstoreInlineInstallerFactory> webstore_inline_installer_factory_;
296 DISALLOW_COPY_AND_ASSIGN(TabHelper);
299 } // namespace extensions
301 #endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_