- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / tab_helper.h
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_
6 #define CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
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"
25
26 namespace content {
27 struct LoadCommittedDetails;
28 }
29
30 namespace gfx {
31 class Image;
32 }
33
34 namespace extensions {
35 class Extension;
36 class LocationBarController;
37 class ScriptBadgeController;
38 class ScriptBubbleController;
39 class ScriptExecutor;
40 class WebstoreInlineInstallerFactory;
41
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> {
48  public:
49   // Different types of action when web app info is available.
50   // OnDidGetApplicationInfo uses this to dispatch calls.
51   enum WebAppAction {
52     NONE,             // No action at all.
53     CREATE_SHORTCUT,  // Bring up create application shortcut dialog.
54     UPDATE_SHORTCUT   // Update icon for app shortcut.
55   };
56
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 {
60    public:
61     // Map of extensions IDs to the executing script paths.
62     typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap;
63
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();
68
69     // Called when script(s) have executed on a page.
70     //
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,
78         int32 on_page_id,
79         const GURL& on_url) = 0;
80
81    protected:
82     virtual ~ScriptExecutionObserver();
83
84     TabHelper* tab_helper_;
85   };
86
87   virtual ~TabHelper();
88
89   void AddScriptExecutionObserver(ScriptExecutionObserver* observer) {
90     script_execution_observers_.AddObserver(observer);
91   }
92
93   void RemoveScriptExecutionObserver(ScriptExecutionObserver* observer) {
94     script_execution_observers_.RemoveObserver(observer);
95   }
96
97   void CreateApplicationShortcuts();
98   bool CanCreateApplicationShortcuts() const;
99
100   void set_pending_web_app_action(WebAppAction action) {
101     pending_web_app_action_ = action;
102   }
103
104   // App extensions ------------------------------------------------------------
105
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.
109   //
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);
114
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
117   // specified id.
118   void SetExtensionAppById(const std::string& extension_app_id);
119
120   // Set just the app icon, used by panels created by an extension.
121   void SetExtensionAppIconById(const std::string& extension_app_id);
122
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_;
127   }
128
129   // If an app extension has been explicitly set for this WebContents its icon
130   // is returned.
131   //
132   // NOTE: the returned icon is larger than 16x16 (its size is
133   // extension_misc::EXTENSION_ICON_SMALLISH).
134   SkBitmap* GetExtensionAppIcon();
135
136   content::WebContents* web_contents() const {
137     return content::WebContentsObserver::web_contents();
138   }
139
140   ScriptExecutor* script_executor() {
141     return script_executor_.get();
142   }
143
144   LocationBarController* location_bar_controller() {
145     return location_bar_controller_.get();
146   }
147
148   ActiveTabPermissionGranter* active_tab_permission_granter() {
149     return active_tab_permission_granter_.get();
150   }
151
152   ScriptBubbleController* script_bubble_controller() {
153     return script_bubble_controller_.get();
154   }
155
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);
159
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);
164
165  private:
166   explicit TabHelper(content::WebContents* web_contents);
167   friend class content::WebContentsUserData<TabHelper>;
168
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;
179
180   // ExtensionFunctionDispatcher::Delegate overrides.
181   virtual extensions::WindowController* GetExtensionWindowController()
182       const OVERRIDE;
183   virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
184
185   // Message handlers.
186   void OnDidGetApplicationInfo(int32 page_id, const WebApplicationInfo& info);
187   void OnInlineWebstoreInstall(int install_id,
188                                int return_route_id,
189                                const std::string& webstore_item_id,
190                                const GURL& requestor_url);
191   void OnGetAppInstallState(const GURL& requestor_url,
192                             int return_route_id,
193                             int callback_id);
194   void OnRequest(const ExtensionHostMsg_Request_Params& params);
195   void OnContentScriptsExecuting(
196       const ScriptExecutionObserver::ExecutingScriptsMap& extension_ids,
197       int32 page_id,
198       const GURL& on_url);
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);
204
205   // App extensions related methods:
206
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);
210
211   const Extension* GetExtension(const std::string& extension_app_id);
212
213   void OnImageLoaded(const gfx::Image& image);
214
215   // WebstoreStandaloneInstaller::Callback.
216   virtual void OnInlineInstallComplete(int install_id,
217                                        int return_route_id,
218                                        bool success,
219                                        const std::string& error);
220
221   // content::NotificationObserver.
222   virtual void Observe(int type,
223                        const content::NotificationSource& source,
224                        const content::NotificationDetails& details) OVERRIDE;
225
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);
230
231   // Sends our tab ID to |render_view_host|.
232   void SetTabId(content::RenderViewHost* render_view_host);
233
234   // Data for app extensions ---------------------------------------------------
235
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_;
239
240   // If non-null this tab is an app tab and this is the extension the tab was
241   // created for.
242   const Extension* extension_app_;
243
244   // Icon for extension_app_ (if non-null) or a manually-set icon for
245   // non-extension apps.
246   SkBitmap extension_app_icon_;
247
248   // Process any extension messages coming from the tab.
249   ExtensionFunctionDispatcher extension_function_dispatcher_;
250
251   // Cached web app info data.
252   WebApplicationInfo web_app_info_;
253
254   // Which deferred action to perform when OnDidGetApplicationInfo is notified
255   // from a WebContents.
256   WebAppAction pending_web_app_action_;
257
258   content::NotificationRegistrar registrar_;
259
260   scoped_ptr<ScriptExecutor> script_executor_;
261
262   scoped_ptr<LocationBarController> location_bar_controller_;
263
264   scoped_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_;
265
266   scoped_ptr<ScriptBubbleController> script_bubble_controller_;
267
268   Profile* profile_;
269
270   // Vend weak pointers that can be invalidated to stop in-progress loads.
271   base::WeakPtrFactory<TabHelper> image_loader_ptr_factory_;
272
273   // Creates WebstoreInlineInstaller instances for inline install triggers.
274   scoped_ptr<WebstoreInlineInstallerFactory> webstore_inline_installer_factory_;
275
276   DISALLOW_COPY_AND_ASSIGN(TabHelper);
277 };
278
279 }  // namespace extensions
280
281 #endif  // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_