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/common/extensions/webstore_install_result.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/browser/script_execution_observer.h"
25 #include "extensions/browser/script_executor.h"
26 #include "extensions/common/stack_frame.h"
27 #include "third_party/skia/include/core/SkBitmap.h"
29 class FaviconDownloader
;
32 struct LoadCommittedDetails
;
33 class RenderFrameHost
;
40 namespace extensions
{
41 class BookmarkAppHelper
;
43 class LocationBarController
;
44 class WebstoreInlineInstallerFactory
;
46 // Per-tab extension helper. Also handles non-extension apps.
47 class TabHelper
: public content::WebContentsObserver
,
48 public extensions::ExtensionFunctionDispatcher::Delegate
,
49 public base::SupportsWeakPtr
<TabHelper
>,
50 public content::NotificationObserver
,
51 public content::WebContentsUserData
<TabHelper
> {
55 void CreateApplicationShortcuts();
56 void CreateHostedAppFromWebContents();
57 bool CanCreateApplicationShortcuts() const;
58 bool CanCreateBookmarkApp() const;
60 void UpdateShortcutOnLoadComplete() {
61 update_shortcut_on_load_complete_
= true;
64 // ScriptExecutionObserver::Delegate
65 virtual void AddScriptExecutionObserver(ScriptExecutionObserver
* observer
);
66 virtual void RemoveScriptExecutionObserver(ScriptExecutionObserver
* observer
);
68 // App extensions ------------------------------------------------------------
70 // Sets the extension denoting this as an app. If |extension| is non-null this
71 // tab becomes an app-tab. WebContents does not listen for unload events for
72 // the extension. It's up to consumers of WebContents to do that.
74 // NOTE: this should only be manipulated before the tab is added to a browser.
75 // TODO(sky): resolve if this is the right way to identify an app tab. If it
76 // is, than this should be passed in the constructor.
77 void SetExtensionApp(const Extension
* extension
);
79 // Convenience for setting the app extension by id. This does nothing if
80 // |extension_app_id| is empty, or an extension can't be found given the
82 void SetExtensionAppById(const std::string
& extension_app_id
);
84 // Set just the app icon, used by panels created by an extension.
85 void SetExtensionAppIconById(const std::string
& extension_app_id
);
87 const Extension
* extension_app() const { return extension_app_
; }
88 bool is_app() const { return extension_app_
!= NULL
; }
89 const WebApplicationInfo
& web_app_info() const {
93 // If an app extension has been explicitly set for this WebContents its icon
96 // NOTE: the returned icon is larger than 16x16 (its size is
97 // extension_misc::EXTENSION_ICON_SMALLISH).
98 SkBitmap
* GetExtensionAppIcon();
100 content::WebContents
* web_contents() const {
101 return content::WebContentsObserver::web_contents();
104 ScriptExecutor
* script_executor() {
105 return script_executor_
.get();
108 LocationBarController
* location_bar_controller() {
109 return location_bar_controller_
.get();
112 ActiveTabPermissionGranter
* active_tab_permission_granter() {
113 return active_tab_permission_granter_
.get();
116 // Sets a non-extension app icon associated with WebContents and fires an
117 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
118 void SetAppIcon(const SkBitmap
& app_icon
);
120 // Sets the factory used to create inline webstore item installers.
121 // Used for testing. Takes ownership of the factory instance.
122 void SetWebstoreInlineInstallerFactoryForTests(
123 WebstoreInlineInstallerFactory
* factory
);
126 // Different types of action when web app info is available.
127 // OnDidGetApplicationInfo uses this to dispatch calls.
129 NONE
, // No action at all.
130 CREATE_SHORTCUT
, // Bring up create application shortcut dialog.
131 CREATE_HOSTED_APP
, // Create and install a hosted app.
132 UPDATE_SHORTCUT
// Update icon for app shortcut.
135 explicit TabHelper(content::WebContents
* web_contents
);
136 friend class content::WebContentsUserData
<TabHelper
>;
138 // Displays UI for completion of creating a bookmark hosted app.
139 void FinishCreateBookmarkApp(const extensions::Extension
* extension
,
140 const WebApplicationInfo
& web_app_info
);
142 // content::WebContentsObserver overrides.
143 virtual void RenderViewCreated(
144 content::RenderViewHost
* render_view_host
) OVERRIDE
;
145 virtual void DidNavigateMainFrame(
146 const content::LoadCommittedDetails
& details
,
147 const content::FrameNavigateParams
& params
) OVERRIDE
;
148 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
149 virtual bool OnMessageReceived(
150 const IPC::Message
& message
,
151 content::RenderFrameHost
* render_frame_host
) OVERRIDE
;
152 virtual void DidCloneToNewWebContents(
153 content::WebContents
* old_web_contents
,
154 content::WebContents
* new_web_contents
) OVERRIDE
;
156 // extensions::ExtensionFunctionDispatcher::Delegate overrides.
157 virtual extensions::WindowController
* GetExtensionWindowController()
159 virtual content::WebContents
* GetAssociatedWebContents() const OVERRIDE
;
162 void OnDidGetApplicationInfo(const WebApplicationInfo
& info
);
163 void OnInlineWebstoreInstall(int install_id
,
165 const std::string
& webstore_item_id
,
166 const GURL
& requestor_url
,
168 void OnGetAppInstallState(const GURL
& requestor_url
,
171 void OnRequest(const ExtensionHostMsg_Request_Params
& params
);
172 void OnContentScriptsExecuting(
173 const ScriptExecutionObserver::ExecutingScriptsMap
& extension_ids
,
175 void OnWatchedPageChange(const std::vector
<std::string
>& css_selectors
);
176 void OnDetailedConsoleMessageAdded(const base::string16
& message
,
177 const base::string16
& source
,
178 const StackTrace
& stack_trace
,
179 int32 severity_level
);
181 // App extensions related methods:
183 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
184 // the extension's image asynchronously.
185 void UpdateExtensionAppIcon(const Extension
* extension
);
187 const Extension
* GetExtension(const std::string
& extension_app_id
);
189 void OnImageLoaded(const gfx::Image
& image
);
191 // WebstoreStandaloneInstaller::Callback.
192 virtual void OnInlineInstallComplete(int install_id
,
195 const std::string
& error
,
196 webstore_install::Result result
);
198 // content::NotificationObserver.
199 virtual void Observe(int type
,
200 const content::NotificationSource
& source
,
201 const content::NotificationDetails
& details
) OVERRIDE
;
203 // Requests application info for the specified page. This is an asynchronous
204 // request. The delegate is notified by way of OnDidGetApplicationInfo when
205 // the data is available.
206 void GetApplicationInfo(WebAppAction action
);
208 // Sends our tab ID to |render_view_host|.
209 void SetTabId(content::RenderViewHost
* render_view_host
);
211 // Data for app extensions ---------------------------------------------------
213 // Our content script observers. Declare at top so that it will outlive all
214 // other members, since they might add themselves as observers.
215 ObserverList
<ScriptExecutionObserver
> script_execution_observers_
;
217 // If non-null this tab is an app tab and this is the extension the tab was
219 const Extension
* extension_app_
;
221 // Icon for extension_app_ (if non-null) or a manually-set icon for
222 // non-extension apps.
223 SkBitmap extension_app_icon_
;
225 // Process any extension messages coming from the tab.
226 extensions::ExtensionFunctionDispatcher extension_function_dispatcher_
;
228 // Cached web app info data.
229 WebApplicationInfo web_app_info_
;
231 // Which deferred action to perform when OnDidGetApplicationInfo is notified
232 // from a WebContents.
233 WebAppAction pending_web_app_action_
;
235 // Which page id was active when the GetApplicationInfo request was sent, for
236 // verification when the reply returns.
237 int32 last_committed_page_id_
;
239 // Whether to trigger an update when the page load completes.
240 bool update_shortcut_on_load_complete_
;
242 content::NotificationRegistrar registrar_
;
244 scoped_ptr
<ScriptExecutor
> script_executor_
;
246 scoped_ptr
<LocationBarController
> location_bar_controller_
;
248 scoped_ptr
<ActiveTabPermissionGranter
> active_tab_permission_granter_
;
250 scoped_ptr
<BookmarkAppHelper
> bookmark_app_helper_
;
254 // Vend weak pointers that can be invalidated to stop in-progress loads.
255 base::WeakPtrFactory
<TabHelper
> image_loader_ptr_factory_
;
257 // Creates WebstoreInlineInstaller instances for inline install triggers.
258 scoped_ptr
<WebstoreInlineInstallerFactory
> webstore_inline_installer_factory_
;
260 DISALLOW_COPY_AND_ASSIGN(TabHelper
);
263 } // namespace extensions
265 #endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_