Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / extensions / tab_helper.h
blob5868d4ed982a130e3208f988bf581a0a19c89b35
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/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/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 BookmarkAppHelper;
39 class Extension;
40 class LocationBarController;
41 class ScriptExecutor;
42 class WebstoreInlineInstallerFactory;
44 // Per-tab extension helper. Also handles non-extension apps.
45 class TabHelper : public content::WebContentsObserver,
46 public extensions::ExtensionFunctionDispatcher::Delegate,
47 public base::SupportsWeakPtr<TabHelper>,
48 public content::NotificationObserver,
49 public content::WebContentsUserData<TabHelper> {
50 public:
51 // Different types of action when web app info is available.
52 // OnDidGetApplicationInfo uses this to dispatch calls.
53 enum WebAppAction {
54 NONE, // No action at all.
55 CREATE_SHORTCUT, // Bring up create application shortcut dialog.
56 CREATE_HOSTED_APP, // Create and install a hosted app.
57 UPDATE_SHORTCUT // Update icon for app shortcut.
60 // Observer base class for classes that need to be notified when content
61 // scripts and/or tabs.executeScript calls run on a page.
62 class ScriptExecutionObserver {
63 public:
64 // Map of extensions IDs to the executing script paths.
65 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap;
67 // Automatically observes and unobserves |tab_helper| on construction
68 // and destruction. |tab_helper| must outlive |this|.
69 explicit ScriptExecutionObserver(TabHelper* tab_helper);
70 ScriptExecutionObserver();
72 // Called when script(s) have executed on a page.
74 // |executing_scripts_map| contains all extensions that are executing
75 // scripts, mapped to the paths for those scripts. This may be an empty set
76 // if the script has no path associated with it (e.g. in the case of
77 // tabs.executeScript).
78 virtual void OnScriptsExecuted(
79 const content::WebContents* web_contents,
80 const ExecutingScriptsMap& executing_scripts_map,
81 int32 on_page_id,
82 const GURL& on_url) = 0;
84 protected:
85 virtual ~ScriptExecutionObserver();
87 TabHelper* tab_helper_;
90 virtual ~TabHelper();
92 void AddScriptExecutionObserver(ScriptExecutionObserver* observer) {
93 script_execution_observers_.AddObserver(observer);
96 void RemoveScriptExecutionObserver(ScriptExecutionObserver* observer) {
97 script_execution_observers_.RemoveObserver(observer);
100 void CreateApplicationShortcuts();
101 void CreateHostedAppFromWebContents();
102 bool CanCreateApplicationShortcuts() const;
103 bool CanCreateBookmarkApp() const;
105 void set_pending_web_app_action(WebAppAction action) {
106 pending_web_app_action_ = action;
109 // App extensions ------------------------------------------------------------
111 // Sets the extension denoting this as an app. If |extension| is non-null this
112 // tab becomes an app-tab. WebContents does not listen for unload events for
113 // the extension. It's up to consumers of WebContents to do that.
115 // NOTE: this should only be manipulated before the tab is added to a browser.
116 // TODO(sky): resolve if this is the right way to identify an app tab. If it
117 // is, than this should be passed in the constructor.
118 void SetExtensionApp(const Extension* extension);
120 // Convenience for setting the app extension by id. This does nothing if
121 // |extension_app_id| is empty, or an extension can't be found given the
122 // specified id.
123 void SetExtensionAppById(const std::string& extension_app_id);
125 // Set just the app icon, used by panels created by an extension.
126 void SetExtensionAppIconById(const std::string& extension_app_id);
128 const Extension* extension_app() const { return extension_app_; }
129 bool is_app() const { return extension_app_ != NULL; }
130 const WebApplicationInfo& web_app_info() const {
131 return web_app_info_;
134 // If an app extension has been explicitly set for this WebContents its icon
135 // is returned.
137 // NOTE: the returned icon is larger than 16x16 (its size is
138 // extension_misc::EXTENSION_ICON_SMALLISH).
139 SkBitmap* GetExtensionAppIcon();
141 content::WebContents* web_contents() const {
142 return content::WebContentsObserver::web_contents();
145 ScriptExecutor* script_executor() {
146 return script_executor_.get();
149 LocationBarController* location_bar_controller() {
150 return location_bar_controller_.get();
153 ActiveTabPermissionGranter* active_tab_permission_granter() {
154 return active_tab_permission_granter_.get();
157 // Sets a non-extension app icon associated with WebContents and fires an
158 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
159 void SetAppIcon(const SkBitmap& app_icon);
161 // Sets the factory used to create inline webstore item installers.
162 // Used for testing. Takes ownership of the factory instance.
163 void SetWebstoreInlineInstallerFactoryForTests(
164 WebstoreInlineInstallerFactory* factory);
166 private:
167 explicit TabHelper(content::WebContents* web_contents);
168 friend class content::WebContentsUserData<TabHelper>;
170 // Displays UI for completion of creating a bookmark hosted app.
171 void FinishCreateBookmarkApp(const extensions::Extension* extension,
172 const WebApplicationInfo& web_app_info);
174 // content::WebContentsObserver overrides.
175 virtual void RenderViewCreated(
176 content::RenderViewHost* render_view_host) OVERRIDE;
177 virtual void DidNavigateMainFrame(
178 const content::LoadCommittedDetails& details,
179 const content::FrameNavigateParams& params) OVERRIDE;
180 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
181 virtual void DidCloneToNewWebContents(
182 content::WebContents* old_web_contents,
183 content::WebContents* new_web_contents) OVERRIDE;
185 // extensions::ExtensionFunctionDispatcher::Delegate overrides.
186 virtual extensions::WindowController* GetExtensionWindowController()
187 const OVERRIDE;
188 virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
190 // Message handlers.
191 void OnDidGetApplicationInfo(int32 page_id, const WebApplicationInfo& info);
192 void OnInlineWebstoreInstall(int install_id,
193 int return_route_id,
194 const std::string& webstore_item_id,
195 const GURL& requestor_url,
196 int listeners_mask);
197 void OnGetAppInstallState(const GURL& requestor_url,
198 int return_route_id,
199 int callback_id);
200 void OnRequest(const ExtensionHostMsg_Request_Params& params);
201 void OnContentScriptsExecuting(
202 const ScriptExecutionObserver::ExecutingScriptsMap& extension_ids,
203 int32 page_id,
204 const GURL& on_url);
205 void OnWatchedPageChange(const std::vector<std::string>& css_selectors);
206 void OnDetailedConsoleMessageAdded(const base::string16& message,
207 const base::string16& source,
208 const StackTrace& stack_trace,
209 int32 severity_level);
211 // App extensions related methods:
213 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
214 // the extension's image asynchronously.
215 void UpdateExtensionAppIcon(const Extension* extension);
217 const Extension* GetExtension(const std::string& extension_app_id);
219 void OnImageLoaded(const gfx::Image& image);
221 // WebstoreStandaloneInstaller::Callback.
222 virtual void OnInlineInstallComplete(int install_id,
223 int return_route_id,
224 bool success,
225 const std::string& error);
227 // content::NotificationObserver.
228 virtual void Observe(int type,
229 const content::NotificationSource& source,
230 const content::NotificationDetails& details) OVERRIDE;
232 // Requests application info for the specified page. This is an asynchronous
233 // request. The delegate is notified by way of OnDidGetApplicationInfo when
234 // the data is available.
235 void GetApplicationInfo(int32 page_id);
237 // Sends our tab ID to |render_view_host|.
238 void SetTabId(content::RenderViewHost* render_view_host);
240 // Data for app extensions ---------------------------------------------------
242 // Our content script observers. Declare at top so that it will outlive all
243 // other members, since they might add themselves as observers.
244 ObserverList<ScriptExecutionObserver> script_execution_observers_;
246 // If non-null this tab is an app tab and this is the extension the tab was
247 // created for.
248 const Extension* extension_app_;
250 // Icon for extension_app_ (if non-null) or a manually-set icon for
251 // non-extension apps.
252 SkBitmap extension_app_icon_;
254 // Process any extension messages coming from the tab.
255 extensions::ExtensionFunctionDispatcher extension_function_dispatcher_;
257 // Cached web app info data.
258 WebApplicationInfo web_app_info_;
260 // Which deferred action to perform when OnDidGetApplicationInfo is notified
261 // from a WebContents.
262 WebAppAction pending_web_app_action_;
264 content::NotificationRegistrar registrar_;
266 scoped_ptr<ScriptExecutor> script_executor_;
268 scoped_ptr<LocationBarController> location_bar_controller_;
270 scoped_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_;
272 scoped_ptr<BookmarkAppHelper> bookmark_app_helper_;
274 Profile* profile_;
276 // Vend weak pointers that can be invalidated to stop in-progress loads.
277 base::WeakPtrFactory<TabHelper> image_loader_ptr_factory_;
279 // Creates WebstoreInlineInstaller instances for inline install triggers.
280 scoped_ptr<WebstoreInlineInstallerFactory> webstore_inline_installer_factory_;
282 DISALLOW_COPY_AND_ASSIGN(TabHelper);
285 } // namespace extensions
287 #endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_