Extensions cleanup: Merge IsSyncableApp+Extension, ShouldSyncApp+Extension
[chromium-blink-merge.git] / chrome / browser / extensions / tab_helper.h
blob435cfff52e392d48b9d341c4273c6159a748ebd6
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 <set>
9 #include <string>
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "chrome/browser/extensions/active_tab_permission_granter.h"
16 #include "chrome/browser/extensions/extension_reenabler.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;
31 namespace content {
32 struct LoadCommittedDetails;
33 class RenderFrameHost;
36 namespace gfx {
37 class Image;
40 namespace extensions {
41 class ActiveScriptController;
42 class BookmarkAppHelper;
43 class Extension;
44 class LocationBarController;
45 class WebstoreInlineInstallerFactory;
47 // Per-tab extension helper. Also handles non-extension apps.
48 class TabHelper : public content::WebContentsObserver,
49 public extensions::ExtensionFunctionDispatcher::Delegate,
50 public base::SupportsWeakPtr<TabHelper>,
51 public content::NotificationObserver,
52 public content::WebContentsUserData<TabHelper> {
53 public:
54 ~TabHelper() override;
56 void CreateApplicationShortcuts();
57 void CreateHostedAppFromWebContents();
58 bool CanCreateApplicationShortcuts() const;
59 bool CanCreateBookmarkApp() const;
61 void UpdateShortcutOnLoadComplete() {
62 update_shortcut_on_load_complete_ = true;
65 // ScriptExecutionObserver::Delegate
66 virtual void AddScriptExecutionObserver(ScriptExecutionObserver* observer);
67 virtual void RemoveScriptExecutionObserver(ScriptExecutionObserver* observer);
69 // Sets the extension denoting this as an app. If |extension| is non-null this
70 // tab becomes an app-tab. WebContents does not listen for unload events for
71 // the extension. It's up to consumers of WebContents to do that.
73 // NOTE: this should only be manipulated before the tab is added to a browser.
74 // TODO(sky): resolve if this is the right way to identify an app tab. If it
75 // is, than this should be passed in the constructor.
76 void SetExtensionApp(const Extension* extension);
78 // Convenience for setting the app extension by id. This does nothing if
79 // |extension_app_id| is empty, or an extension can't be found given the
80 // specified id.
81 void SetExtensionAppById(const std::string& extension_app_id);
83 // Set just the app icon, used by panels created by an extension.
84 void SetExtensionAppIconById(const std::string& extension_app_id);
86 const Extension* extension_app() const { return extension_app_; }
87 bool is_app() const { return extension_app_ != NULL; }
88 const WebApplicationInfo& web_app_info() const {
89 return web_app_info_;
92 // If an app extension has been explicitly set for this WebContents its icon
93 // is returned.
95 // NOTE: the returned icon is larger than 16x16 (its size is
96 // extension_misc::EXTENSION_ICON_SMALLISH).
97 SkBitmap* GetExtensionAppIcon();
99 ScriptExecutor* script_executor() {
100 return script_executor_.get();
103 LocationBarController* location_bar_controller() {
104 return location_bar_controller_.get();
107 ActiveScriptController* active_script_controller() {
108 return active_script_controller_.get();
111 ActiveTabPermissionGranter* active_tab_permission_granter() {
112 return active_tab_permission_granter_.get();
115 // Sets a non-extension app icon associated with WebContents and fires an
116 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
117 void SetAppIcon(const SkBitmap& app_icon);
119 // Sets the factory used to create inline webstore item installers.
120 // Used for testing. Takes ownership of the factory instance.
121 void SetWebstoreInlineInstallerFactoryForTests(
122 WebstoreInlineInstallerFactory* factory);
124 private:
125 // Utility function to invoke member functions on all relevant
126 // ContentRulesRegistries.
127 template <class Func>
128 void InvokeForContentRulesRegistries(const Func& func);
130 // Different types of action when web app info is available.
131 // OnDidGetApplicationInfo uses this to dispatch calls.
132 enum WebAppAction {
133 NONE, // No action at all.
134 CREATE_SHORTCUT, // Bring up create application shortcut dialog.
135 CREATE_HOSTED_APP, // Create and install a hosted app.
136 UPDATE_SHORTCUT // Update icon for app shortcut.
139 explicit TabHelper(content::WebContents* web_contents);
140 friend class content::WebContentsUserData<TabHelper>;
142 // Displays UI for completion of creating a bookmark hosted app.
143 void FinishCreateBookmarkApp(const extensions::Extension* extension,
144 const WebApplicationInfo& web_app_info);
146 // content::WebContentsObserver overrides.
147 void RenderFrameCreated(content::RenderFrameHost* host) override;
148 void DidNavigateMainFrame(
149 const content::LoadCommittedDetails& details,
150 const content::FrameNavigateParams& params) override;
151 bool OnMessageReceived(const IPC::Message& message) override;
152 bool OnMessageReceived(const IPC::Message& message,
153 content::RenderFrameHost* render_frame_host) override;
154 void DidCloneToNewWebContents(
155 content::WebContents* old_web_contents,
156 content::WebContents* new_web_contents) override;
158 // extensions::ExtensionFunctionDispatcher::Delegate overrides.
159 extensions::WindowController* GetExtensionWindowController() const override;
160 content::WebContents* GetAssociatedWebContents() const override;
162 // Message handlers.
163 void OnDidGetWebApplicationInfo(const WebApplicationInfo& info);
164 void OnInlineWebstoreInstall(content::RenderFrameHost* host,
165 int install_id,
166 int return_route_id,
167 const std::string& webstore_item_id,
168 const GURL& requestor_url,
169 int listeners_mask);
170 void OnGetAppInstallState(content::RenderFrameHost* host,
171 const GURL& requestor_url,
172 int return_route_id,
173 int callback_id);
174 void OnContentScriptsExecuting(
175 content::RenderFrameHost* host,
176 const ScriptExecutionObserver::ExecutingScriptsMap& extension_ids,
177 const GURL& on_url);
179 // App extensions related methods:
181 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
182 // the extension's image asynchronously.
183 void UpdateExtensionAppIcon(const Extension* extension);
185 const Extension* GetExtension(const std::string& extension_app_id);
187 void OnImageLoaded(const gfx::Image& image);
189 // WebstoreStandaloneInstaller::Callback.
190 void OnInlineInstallComplete(int install_id,
191 int return_route_id,
192 bool success,
193 const std::string& error,
194 webstore_install::Result result);
196 // ExtensionReenabler::Callback.
197 void OnReenableComplete(int install_id,
198 int return_route_id,
199 ExtensionReenabler::ReenableResult result);
201 // content::NotificationObserver.
202 void Observe(int type,
203 const content::NotificationSource& source,
204 const content::NotificationDetails& details) override;
206 // Requests application info for the specified page. This is an asynchronous
207 // request. The delegate is notified by way of OnDidGetApplicationInfo when
208 // the data is available.
209 void GetApplicationInfo(WebAppAction action);
211 // Sends our tab ID to |render_frame_host|.
212 void SetTabId(content::RenderFrameHost* render_frame_host);
214 Profile* profile_;
216 // Our content script observers. Declare at top so that it will outlive all
217 // other members, since they might add themselves as observers.
218 base::ObserverList<ScriptExecutionObserver> script_execution_observers_;
220 // If non-null this tab is an app tab and this is the extension the tab was
221 // created for.
222 const Extension* extension_app_;
224 // Icon for extension_app_ (if non-null) or a manually-set icon for
225 // non-extension apps.
226 SkBitmap extension_app_icon_;
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 navigation entry was active when the GetApplicationInfo request was
236 // sent, for verification when the reply returns.
237 int last_committed_nav_entry_unique_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<ActiveScriptController> active_script_controller_;
250 scoped_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_;
252 scoped_ptr<BookmarkAppHelper> bookmark_app_helper_;
254 // Creates WebstoreInlineInstaller instances for inline install triggers.
255 scoped_ptr<WebstoreInlineInstallerFactory> webstore_inline_installer_factory_;
257 // The reenable prompt for disabled extensions, if any.
258 scoped_ptr<ExtensionReenabler> extension_reenabler_;
260 // Vend weak pointers that can be invalidated to stop in-progress loads.
261 base::WeakPtrFactory<TabHelper> image_loader_ptr_factory_;
263 // Generic weak ptr factory for posting callbacks.
264 base::WeakPtrFactory<TabHelper> weak_ptr_factory_;
266 DISALLOW_COPY_AND_ASSIGN(TabHelper);
269 } // namespace extensions
271 #endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_