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_reenabler.h"
18 #include "chrome/common/extensions/webstore_install_result.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/browser/extension_function_dispatcher.h"
25 #include "extensions/browser/script_execution_observer.h"
26 #include "extensions/browser/script_executor.h"
27 #include "extensions/common/stack_frame.h"
28 #include "third_party/skia/include/core/SkBitmap.h"
30 class FaviconDownloader
;
33 struct LoadCommittedDetails
;
34 class RenderFrameHost
;
41 namespace extensions
{
42 class ActiveScriptController
;
43 class BookmarkAppHelper
;
45 class LocationBarController
;
46 class WebstoreInlineInstallerFactory
;
48 // Per-tab extension helper. Also handles non-extension apps.
49 class TabHelper
: public content::WebContentsObserver
,
50 public extensions::ExtensionFunctionDispatcher::Delegate
,
51 public base::SupportsWeakPtr
<TabHelper
>,
52 public content::NotificationObserver
,
53 public content::WebContentsUserData
<TabHelper
> {
55 ~TabHelper() override
;
57 void CreateApplicationShortcuts();
58 void CreateHostedAppFromWebContents();
59 bool CanCreateApplicationShortcuts() const;
60 bool CanCreateBookmarkApp() const;
62 void UpdateShortcutOnLoadComplete() {
63 update_shortcut_on_load_complete_
= true;
66 // ScriptExecutionObserver::Delegate
67 virtual void AddScriptExecutionObserver(ScriptExecutionObserver
* observer
);
68 virtual void RemoveScriptExecutionObserver(ScriptExecutionObserver
* observer
);
70 // App extensions ------------------------------------------------------------
72 // Sets the extension denoting this as an app. If |extension| is non-null this
73 // tab becomes an app-tab. WebContents does not listen for unload events for
74 // the extension. It's up to consumers of WebContents to do that.
76 // NOTE: this should only be manipulated before the tab is added to a browser.
77 // TODO(sky): resolve if this is the right way to identify an app tab. If it
78 // is, than this should be passed in the constructor.
79 void SetExtensionApp(const Extension
* extension
);
81 // Convenience for setting the app extension by id. This does nothing if
82 // |extension_app_id| is empty, or an extension can't be found given the
84 void SetExtensionAppById(const std::string
& extension_app_id
);
86 // Set just the app icon, used by panels created by an extension.
87 void SetExtensionAppIconById(const std::string
& extension_app_id
);
89 const Extension
* extension_app() const { return extension_app_
; }
90 bool is_app() const { return extension_app_
!= NULL
; }
91 const WebApplicationInfo
& web_app_info() const {
95 // If an app extension has been explicitly set for this WebContents its icon
98 // NOTE: the returned icon is larger than 16x16 (its size is
99 // extension_misc::EXTENSION_ICON_SMALLISH).
100 SkBitmap
* GetExtensionAppIcon();
102 ScriptExecutor
* script_executor() {
103 return script_executor_
.get();
106 LocationBarController
* location_bar_controller() {
107 return location_bar_controller_
.get();
110 ActiveScriptController
* active_script_controller() {
111 return active_script_controller_
.get();
114 ActiveTabPermissionGranter
* active_tab_permission_granter() {
115 return active_tab_permission_granter_
.get();
118 // Sets a non-extension app icon associated with WebContents and fires an
119 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
120 void SetAppIcon(const SkBitmap
& app_icon
);
122 // Sets the factory used to create inline webstore item installers.
123 // Used for testing. Takes ownership of the factory instance.
124 void SetWebstoreInlineInstallerFactoryForTests(
125 WebstoreInlineInstallerFactory
* factory
);
128 // Different types of action when web app info is available.
129 // OnDidGetApplicationInfo uses this to dispatch calls.
131 NONE
, // No action at all.
132 CREATE_SHORTCUT
, // Bring up create application shortcut dialog.
133 CREATE_HOSTED_APP
, // Create and install a hosted app.
134 UPDATE_SHORTCUT
// Update icon for app shortcut.
137 explicit TabHelper(content::WebContents
* web_contents
);
138 friend class content::WebContentsUserData
<TabHelper
>;
140 // Displays UI for completion of creating a bookmark hosted app.
141 void FinishCreateBookmarkApp(const extensions::Extension
* extension
,
142 const WebApplicationInfo
& web_app_info
);
144 // content::WebContentsObserver overrides.
145 void RenderViewCreated(content::RenderViewHost
* render_view_host
) override
;
146 void DidNavigateMainFrame(
147 const content::LoadCommittedDetails
& details
,
148 const content::FrameNavigateParams
& params
) override
;
149 bool OnMessageReceived(const IPC::Message
& message
) override
;
150 bool OnMessageReceived(const IPC::Message
& message
,
151 content::RenderFrameHost
* render_frame_host
) override
;
152 void DidCloneToNewWebContents(
153 content::WebContents
* old_web_contents
,
154 content::WebContents
* new_web_contents
) override
;
156 // extensions::ExtensionFunctionDispatcher::Delegate overrides.
157 extensions::WindowController
* GetExtensionWindowController() const override
;
158 content::WebContents
* GetAssociatedWebContents() const override
;
161 void OnDidGetWebApplicationInfo(const WebApplicationInfo
& info
);
162 void OnInlineWebstoreInstall(int install_id
,
164 const std::string
& webstore_item_id
,
165 const GURL
& requestor_url
,
167 void OnGetAppInstallState(const GURL
& requestor_url
,
170 void OnRequest(const ExtensionHostMsg_Request_Params
& params
);
171 void OnContentScriptsExecuting(
172 const ScriptExecutionObserver::ExecutingScriptsMap
& extension_ids
,
174 void OnWatchedPageChange(const std::vector
<std::string
>& css_selectors
);
175 void OnDetailedConsoleMessageAdded(const base::string16
& message
,
176 const base::string16
& source
,
177 const StackTrace
& stack_trace
,
178 int32 severity_level
);
180 // App extensions related methods:
182 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
183 // the extension's image asynchronously.
184 void UpdateExtensionAppIcon(const Extension
* extension
);
186 const Extension
* GetExtension(const std::string
& extension_app_id
);
188 void OnImageLoaded(const gfx::Image
& image
);
190 // WebstoreStandaloneInstaller::Callback.
191 void OnInlineInstallComplete(int install_id
,
194 const std::string
& error
,
195 webstore_install::Result result
);
197 // ExtensionReenabler::Callback.
198 void OnReenableComplete(int install_id
,
200 ExtensionReenabler::ReenableResult result
);
202 // content::NotificationObserver.
203 void Observe(int type
,
204 const content::NotificationSource
& source
,
205 const content::NotificationDetails
& details
) override
;
207 // Requests application info for the specified page. This is an asynchronous
208 // request. The delegate is notified by way of OnDidGetApplicationInfo when
209 // the data is available.
210 void GetApplicationInfo(WebAppAction action
);
212 // Sends our tab ID to |render_view_host|.
213 void SetTabId(content::RenderViewHost
* render_view_host
);
215 // Data for app extensions ---------------------------------------------------
217 // Our content script observers. Declare at top so that it will outlive all
218 // other members, since they might add themselves as observers.
219 ObserverList
<ScriptExecutionObserver
> script_execution_observers_
;
221 // If non-null this tab is an app tab and this is the extension the tab was
223 const Extension
* extension_app_
;
225 // Icon for extension_app_ (if non-null) or a manually-set icon for
226 // non-extension apps.
227 SkBitmap extension_app_icon_
;
229 // Process any extension messages coming from the tab.
230 extensions::ExtensionFunctionDispatcher extension_function_dispatcher_
;
232 // Cached web app info data.
233 WebApplicationInfo web_app_info_
;
235 // Which deferred action to perform when OnDidGetApplicationInfo is notified
236 // from a WebContents.
237 WebAppAction pending_web_app_action_
;
239 // Which navigation entry was active when the GetApplicationInfo request was
240 // sent, for verification when the reply returns.
241 int last_committed_nav_entry_unique_id_
;
243 // Whether to trigger an update when the page load completes.
244 bool update_shortcut_on_load_complete_
;
246 content::NotificationRegistrar registrar_
;
248 scoped_ptr
<ScriptExecutor
> script_executor_
;
250 scoped_ptr
<LocationBarController
> location_bar_controller_
;
252 scoped_ptr
<ActiveScriptController
> active_script_controller_
;
254 scoped_ptr
<ActiveTabPermissionGranter
> active_tab_permission_granter_
;
256 scoped_ptr
<BookmarkAppHelper
> bookmark_app_helper_
;
260 // Creates WebstoreInlineInstaller instances for inline install triggers.
261 scoped_ptr
<WebstoreInlineInstallerFactory
> webstore_inline_installer_factory_
;
263 // The reenable prompt for disabled extensions, if any.
264 scoped_ptr
<ExtensionReenabler
> extension_reenabler_
;
266 // Vend weak pointers that can be invalidated to stop in-progress loads.
267 base::WeakPtrFactory
<TabHelper
> image_loader_ptr_factory_
;
269 // Generic weak ptr factory for posting callbacks.
270 base::WeakPtrFactory
<TabHelper
> weak_ptr_factory_
;
272 DISALLOW_COPY_AND_ASSIGN(TabHelper
);
275 } // namespace extensions
277 #endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_