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_
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
;
30 struct LoadCommittedDetails
;
37 namespace extensions
{
39 class LocationBarController
;
40 class ScriptBadgeController
;
41 class ScriptBubbleController
;
43 class WebstoreInlineInstallerFactory
;
45 // Per-tab extension helper. Also handles non-extension apps.
46 class TabHelper
: public content::WebContentsObserver
,
47 public ExtensionFunctionDispatcher::Delegate
,
48 public base::SupportsWeakPtr
<TabHelper
>,
49 public content::NotificationObserver
,
50 public content::WebContentsUserData
<TabHelper
> {
52 // Different types of action when web app info is available.
53 // OnDidGetApplicationInfo uses this to dispatch calls.
55 NONE
, // No action at all.
56 CREATE_SHORTCUT
, // Bring up create application shortcut dialog.
57 CREATE_HOSTED_APP
, // Create and install a hosted app.
58 UPDATE_SHORTCUT
// Update icon for app shortcut.
61 // Observer base class for classes that need to be notified when content
62 // scripts and/or tabs.executeScript calls run on a page.
63 class ScriptExecutionObserver
{
65 // Map of extensions IDs to the executing script paths.
66 typedef std::map
<std::string
, std::set
<std::string
> > ExecutingScriptsMap
;
68 // Automatically observes and unobserves |tab_helper| on construction
69 // and destruction. |tab_helper| must outlive |this|.
70 explicit ScriptExecutionObserver(TabHelper
* tab_helper
);
71 ScriptExecutionObserver();
73 // Called when script(s) have executed on a page.
75 // |executing_scripts_map| contains all extensions that are executing
76 // scripts, mapped to the paths for those scripts. This may be an empty set
77 // if the script has no path associated with it (e.g. in the case of
78 // tabs.executeScript).
79 virtual void OnScriptsExecuted(
80 const content::WebContents
* web_contents
,
81 const ExecutingScriptsMap
& executing_scripts_map
,
83 const GURL
& on_url
) = 0;
86 virtual ~ScriptExecutionObserver();
88 TabHelper
* tab_helper_
;
91 // This finds the closest not-smaller bitmap in |bitmaps| for each size in
92 // |sizes| and resizes it to that size. This returns a vector of bitmaps
93 // which contains only bitmaps of a size in |sizes| and at most one bitmap of
95 static std::vector
<SkBitmap
> ConstrainBitmapsToSizes(
96 const std::vector
<SkBitmap
>& bitmaps
,
97 const std::set
<int>& sizes
);
101 void AddScriptExecutionObserver(ScriptExecutionObserver
* observer
) {
102 script_execution_observers_
.AddObserver(observer
);
105 void RemoveScriptExecutionObserver(ScriptExecutionObserver
* observer
) {
106 script_execution_observers_
.RemoveObserver(observer
);
109 void CreateApplicationShortcuts();
110 void CreateHostedAppFromWebContents();
111 bool CanCreateApplicationShortcuts() const;
113 void set_pending_web_app_action(WebAppAction action
) {
114 pending_web_app_action_
= action
;
117 // App extensions ------------------------------------------------------------
119 // Sets the extension denoting this as an app. If |extension| is non-null this
120 // tab becomes an app-tab. WebContents does not listen for unload events for
121 // the extension. It's up to consumers of WebContents to do that.
123 // NOTE: this should only be manipulated before the tab is added to a browser.
124 // TODO(sky): resolve if this is the right way to identify an app tab. If it
125 // is, than this should be passed in the constructor.
126 void SetExtensionApp(const Extension
* extension
);
128 // Convenience for setting the app extension by id. This does nothing if
129 // |extension_app_id| is empty, or an extension can't be found given the
131 void SetExtensionAppById(const std::string
& extension_app_id
);
133 // Set just the app icon, used by panels created by an extension.
134 void SetExtensionAppIconById(const std::string
& extension_app_id
);
136 const Extension
* extension_app() const { return extension_app_
; }
137 bool is_app() const { return extension_app_
!= NULL
; }
138 const WebApplicationInfo
& web_app_info() const {
139 return web_app_info_
;
142 // If an app extension has been explicitly set for this WebContents its icon
145 // NOTE: the returned icon is larger than 16x16 (its size is
146 // extension_misc::EXTENSION_ICON_SMALLISH).
147 SkBitmap
* GetExtensionAppIcon();
149 content::WebContents
* web_contents() const {
150 return content::WebContentsObserver::web_contents();
153 ScriptExecutor
* script_executor() {
154 return script_executor_
.get();
157 LocationBarController
* location_bar_controller() {
158 return location_bar_controller_
.get();
161 ActiveTabPermissionGranter
* active_tab_permission_granter() {
162 return active_tab_permission_granter_
.get();
165 ScriptBubbleController
* script_bubble_controller() {
166 return script_bubble_controller_
.get();
169 // Sets a non-extension app icon associated with WebContents and fires an
170 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
171 void SetAppIcon(const SkBitmap
& app_icon
);
173 // Sets the factory used to create inline webstore item installers.
174 // Used for testing. Takes ownership of the factory instance.
175 void SetWebstoreInlineInstallerFactoryForTests(
176 WebstoreInlineInstallerFactory
* factory
);
179 explicit TabHelper(content::WebContents
* web_contents
);
180 friend class content::WebContentsUserData
<TabHelper
>;
182 // Creates a hosted app for the current tab. Requires the |web_app_info_| to
184 void CreateHostedApp();
185 void FinishCreateHostedApp(
186 bool success
, const std::map
<GURL
, std::vector
<SkBitmap
> >& bitmaps
);
188 // content::WebContentsObserver overrides.
189 virtual void RenderViewCreated(
190 content::RenderViewHost
* render_view_host
) OVERRIDE
;
191 virtual void DidNavigateMainFrame(
192 const content::LoadCommittedDetails
& details
,
193 const content::FrameNavigateParams
& params
) OVERRIDE
;
194 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
195 virtual void DidCloneToNewWebContents(
196 content::WebContents
* old_web_contents
,
197 content::WebContents
* new_web_contents
) OVERRIDE
;
199 // ExtensionFunctionDispatcher::Delegate overrides.
200 virtual extensions::WindowController
* GetExtensionWindowController()
202 virtual content::WebContents
* GetAssociatedWebContents() const OVERRIDE
;
205 void OnDidGetApplicationInfo(int32 page_id
, const WebApplicationInfo
& info
);
206 void OnInlineWebstoreInstall(int install_id
,
208 const std::string
& webstore_item_id
,
209 const GURL
& requestor_url
);
210 void OnGetAppInstallState(const GURL
& requestor_url
,
213 void OnRequest(const ExtensionHostMsg_Request_Params
& params
);
214 void OnContentScriptsExecuting(
215 const ScriptExecutionObserver::ExecutingScriptsMap
& extension_ids
,
218 void OnWatchedPageChange(const std::vector
<std::string
>& css_selectors
);
219 void OnDetailedConsoleMessageAdded(const base::string16
& message
,
220 const base::string16
& source
,
221 const StackTrace
& stack_trace
,
222 int32 severity_level
);
224 // App extensions related methods:
226 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
227 // the extension's image asynchronously.
228 void UpdateExtensionAppIcon(const Extension
* extension
);
230 const Extension
* GetExtension(const std::string
& extension_app_id
);
232 void OnImageLoaded(const gfx::Image
& image
);
234 // WebstoreStandaloneInstaller::Callback.
235 virtual void OnInlineInstallComplete(int install_id
,
238 const std::string
& error
);
240 // content::NotificationObserver.
241 virtual void Observe(int type
,
242 const content::NotificationSource
& source
,
243 const content::NotificationDetails
& details
) OVERRIDE
;
245 // Requests application info for the specified page. This is an asynchronous
246 // request. The delegate is notified by way of OnDidGetApplicationInfo when
247 // the data is available.
248 void GetApplicationInfo(int32 page_id
);
250 // Sends our tab ID to |render_view_host|.
251 void SetTabId(content::RenderViewHost
* render_view_host
);
253 // Data for app extensions ---------------------------------------------------
255 // Our content script observers. Declare at top so that it will outlive all
256 // other members, since they might add themselves as observers.
257 ObserverList
<ScriptExecutionObserver
> script_execution_observers_
;
259 // If non-null this tab is an app tab and this is the extension the tab was
261 const Extension
* extension_app_
;
263 // Icon for extension_app_ (if non-null) or a manually-set icon for
264 // non-extension apps.
265 SkBitmap extension_app_icon_
;
267 // Process any extension messages coming from the tab.
268 ExtensionFunctionDispatcher extension_function_dispatcher_
;
270 // Cached web app info data.
271 WebApplicationInfo web_app_info_
;
273 // Which deferred action to perform when OnDidGetApplicationInfo is notified
274 // from a WebContents.
275 WebAppAction pending_web_app_action_
;
277 content::NotificationRegistrar registrar_
;
279 scoped_ptr
<ScriptExecutor
> script_executor_
;
281 scoped_ptr
<LocationBarController
> location_bar_controller_
;
283 scoped_ptr
<ActiveTabPermissionGranter
> active_tab_permission_granter_
;
285 scoped_ptr
<ScriptBubbleController
> script_bubble_controller_
;
287 scoped_ptr
<FaviconDownloader
> favicon_downloader_
;
291 // Vend weak pointers that can be invalidated to stop in-progress loads.
292 base::WeakPtrFactory
<TabHelper
> image_loader_ptr_factory_
;
294 // Creates WebstoreInlineInstaller instances for inline install triggers.
295 scoped_ptr
<WebstoreInlineInstallerFactory
> webstore_inline_installer_factory_
;
297 DISALLOW_COPY_AND_ASSIGN(TabHelper
);
300 } // namespace extensions
302 #endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_