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_
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/browser/extensions/extension_function_dispatcher.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/common/stack_frame.h"
24 #include "third_party/skia/include/core/SkBitmap.h"
27 struct LoadCommittedDetails;
34 namespace extensions {
36 class LocationBarController;
37 class ScriptBadgeController;
38 class ScriptBubbleController;
40 class WebstoreInlineInstallerFactory;
42 // Per-tab extension helper. Also handles non-extension apps.
43 class TabHelper : public content::WebContentsObserver,
44 public ExtensionFunctionDispatcher::Delegate,
45 public base::SupportsWeakPtr<TabHelper>,
46 public content::NotificationObserver,
47 public content::WebContentsUserData<TabHelper> {
49 // Different types of action when web app info is available.
50 // OnDidGetApplicationInfo uses this to dispatch calls.
52 NONE, // No action at all.
53 CREATE_SHORTCUT, // Bring up create application shortcut dialog.
54 UPDATE_SHORTCUT // Update icon for app shortcut.
57 // Observer base class for classes that need to be notified when content
58 // scripts and/or tabs.executeScript calls run on a page.
59 class ScriptExecutionObserver {
61 // Map of extensions IDs to the executing script paths.
62 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap;
64 // Automatically observes and unobserves |tab_helper| on construction
65 // and destruction. |tab_helper| must outlive |this|.
66 explicit ScriptExecutionObserver(TabHelper* tab_helper);
67 ScriptExecutionObserver();
69 // Called when script(s) have executed on a page.
71 // |executing_scripts_map| contains all extensions that are executing
72 // scripts, mapped to the paths for those scripts. This may be an empty set
73 // if the script has no path associated with it (e.g. in the case of
74 // tabs.executeScript).
75 virtual void OnScriptsExecuted(
76 const content::WebContents* web_contents,
77 const ExecutingScriptsMap& executing_scripts_map,
79 const GURL& on_url) = 0;
82 virtual ~ScriptExecutionObserver();
84 TabHelper* tab_helper_;
89 void AddScriptExecutionObserver(ScriptExecutionObserver* observer) {
90 script_execution_observers_.AddObserver(observer);
93 void RemoveScriptExecutionObserver(ScriptExecutionObserver* observer) {
94 script_execution_observers_.RemoveObserver(observer);
97 void CreateApplicationShortcuts();
98 bool CanCreateApplicationShortcuts() const;
100 void set_pending_web_app_action(WebAppAction action) {
101 pending_web_app_action_ = action;
104 // App extensions ------------------------------------------------------------
106 // Sets the extension denoting this as an app. If |extension| is non-null this
107 // tab becomes an app-tab. WebContents does not listen for unload events for
108 // the extension. It's up to consumers of WebContents to do that.
110 // NOTE: this should only be manipulated before the tab is added to a browser.
111 // TODO(sky): resolve if this is the right way to identify an app tab. If it
112 // is, than this should be passed in the constructor.
113 void SetExtensionApp(const Extension* extension);
115 // Convenience for setting the app extension by id. This does nothing if
116 // |extension_app_id| is empty, or an extension can't be found given the
118 void SetExtensionAppById(const std::string& extension_app_id);
120 // Set just the app icon, used by panels created by an extension.
121 void SetExtensionAppIconById(const std::string& extension_app_id);
123 const Extension* extension_app() const { return extension_app_; }
124 bool is_app() const { return extension_app_ != NULL; }
125 const WebApplicationInfo& web_app_info() const {
126 return web_app_info_;
129 // If an app extension has been explicitly set for this WebContents its icon
132 // NOTE: the returned icon is larger than 16x16 (its size is
133 // extension_misc::EXTENSION_ICON_SMALLISH).
134 SkBitmap* GetExtensionAppIcon();
136 content::WebContents* web_contents() const {
137 return content::WebContentsObserver::web_contents();
140 ScriptExecutor* script_executor() {
141 return script_executor_.get();
144 LocationBarController* location_bar_controller() {
145 return location_bar_controller_.get();
148 ActiveTabPermissionGranter* active_tab_permission_granter() {
149 return active_tab_permission_granter_.get();
152 ScriptBubbleController* script_bubble_controller() {
153 return script_bubble_controller_.get();
156 // Sets a non-extension app icon associated with WebContents and fires an
157 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
158 void SetAppIcon(const SkBitmap& app_icon);
160 // Sets the factory used to create inline webstore item installers.
161 // Used for testing. Takes ownership of the factory instance.
162 void SetWebstoreInlineInstallerFactoryForTests(
163 WebstoreInlineInstallerFactory* factory);
166 explicit TabHelper(content::WebContents* web_contents);
167 friend class content::WebContentsUserData<TabHelper>;
169 // content::WebContentsObserver overrides.
170 virtual void RenderViewCreated(
171 content::RenderViewHost* render_view_host) OVERRIDE;
172 virtual void DidNavigateMainFrame(
173 const content::LoadCommittedDetails& details,
174 const content::FrameNavigateParams& params) OVERRIDE;
175 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
176 virtual void DidCloneToNewWebContents(
177 content::WebContents* old_web_contents,
178 content::WebContents* new_web_contents) OVERRIDE;
180 // ExtensionFunctionDispatcher::Delegate overrides.
181 virtual extensions::WindowController* GetExtensionWindowController()
183 virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
186 void OnDidGetApplicationInfo(int32 page_id, const WebApplicationInfo& info);
187 void OnInlineWebstoreInstall(int install_id,
189 const std::string& webstore_item_id,
190 const GURL& requestor_url);
191 void OnGetAppInstallState(const GURL& requestor_url,
194 void OnRequest(const ExtensionHostMsg_Request_Params& params);
195 void OnContentScriptsExecuting(
196 const ScriptExecutionObserver::ExecutingScriptsMap& extension_ids,
199 void OnWatchedPageChange(const std::vector<std::string>& css_selectors);
200 void OnDetailedConsoleMessageAdded(const base::string16& message,
201 const base::string16& source,
202 const StackTrace& stack_trace,
203 int32 severity_level);
205 // App extensions related methods:
207 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
208 // the extension's image asynchronously.
209 void UpdateExtensionAppIcon(const Extension* extension);
211 const Extension* GetExtension(const std::string& extension_app_id);
213 void OnImageLoaded(const gfx::Image& image);
215 // WebstoreStandaloneInstaller::Callback.
216 virtual void OnInlineInstallComplete(int install_id,
219 const std::string& error);
221 // content::NotificationObserver.
222 virtual void Observe(int type,
223 const content::NotificationSource& source,
224 const content::NotificationDetails& details) OVERRIDE;
226 // Requests application info for the specified page. This is an asynchronous
227 // request. The delegate is notified by way of OnDidGetApplicationInfo when
228 // the data is available.
229 void GetApplicationInfo(int32 page_id);
231 // Sends our tab ID to |render_view_host|.
232 void SetTabId(content::RenderViewHost* render_view_host);
234 // Data for app extensions ---------------------------------------------------
236 // Our content script observers. Declare at top so that it will outlive all
237 // other members, since they might add themselves as observers.
238 ObserverList<ScriptExecutionObserver> script_execution_observers_;
240 // If non-null this tab is an app tab and this is the extension the tab was
242 const Extension* extension_app_;
244 // Icon for extension_app_ (if non-null) or a manually-set icon for
245 // non-extension apps.
246 SkBitmap extension_app_icon_;
248 // Process any extension messages coming from the tab.
249 ExtensionFunctionDispatcher extension_function_dispatcher_;
251 // Cached web app info data.
252 WebApplicationInfo web_app_info_;
254 // Which deferred action to perform when OnDidGetApplicationInfo is notified
255 // from a WebContents.
256 WebAppAction pending_web_app_action_;
258 content::NotificationRegistrar registrar_;
260 scoped_ptr<ScriptExecutor> script_executor_;
262 scoped_ptr<LocationBarController> location_bar_controller_;
264 scoped_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_;
266 scoped_ptr<ScriptBubbleController> script_bubble_controller_;
270 // Vend weak pointers that can be invalidated to stop in-progress loads.
271 base::WeakPtrFactory<TabHelper> image_loader_ptr_factory_;
273 // Creates WebstoreInlineInstaller instances for inline install triggers.
274 scoped_ptr<WebstoreInlineInstallerFactory> webstore_inline_installer_factory_;
276 DISALLOW_COPY_AND_ASSIGN(TabHelper);
279 } // namespace extensions
281 #endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_