Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / extensions / extension_settings_handler.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_UI_WEBUI_EXTENSIONS_EXTENSION_SETTINGS_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_SETTINGS_HANDLER_H_
7
8 #include <set>
9 #include <string>
10 #include <vector>
11
12 #include "base/memory/scoped_ptr.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "base/scoped_observer.h"
15 #include "chrome/browser/extensions/error_console/error_console.h"
16 #include "chrome/browser/extensions/extension_install_prompt.h"
17 #include "chrome/browser/extensions/extension_install_ui.h"
18 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
19 #include "chrome/browser/extensions/extension_warning_service.h"
20 #include "chrome/browser/extensions/requirements_checker.h"
21 #include "content/public/browser/navigation_controller.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h"
24 #include "content/public/browser/web_contents_observer.h"
25 #include "content/public/browser/web_ui_message_handler.h"
26 #include "extensions/browser/extension_prefs.h"
27 #include "extensions/browser/extension_prefs_observer.h"
28 #include "url/gurl.h"
29
30 class ExtensionService;
31
32 namespace base {
33 class DictionaryValue;
34 class FilePath;
35 class ListValue;
36 }
37
38 namespace content {
39 class WebUIDataSource;
40 }
41
42 namespace user_prefs {
43 class PrefRegistrySyncable;
44 }
45
46 namespace extensions {
47 class Extension;
48 class ManagementPolicy;
49
50 // Information about a page running in an extension, for example a popup bubble,
51 // a background page, or a tab contents.
52 struct ExtensionPage {
53   ExtensionPage(const GURL& url,
54                 int render_process_id,
55                 int render_view_id,
56                 bool incognito,
57                 bool generated_background_page);
58   GURL url;
59   int render_process_id;
60   int render_view_id;
61   bool incognito;
62   bool generated_background_page;
63 };
64
65 // Extension Settings UI handler.
66 class ExtensionSettingsHandler
67     : public content::WebUIMessageHandler,
68       public content::NotificationObserver,
69       public content::WebContentsObserver,
70       public ErrorConsole::Observer,
71       public ExtensionInstallPrompt::Delegate,
72       public ExtensionPrefsObserver,
73       public ExtensionUninstallDialog::Delegate,
74       public ExtensionWarningService::Observer,
75       public base::SupportsWeakPtr<ExtensionSettingsHandler> {
76  public:
77   ExtensionSettingsHandler();
78   virtual ~ExtensionSettingsHandler();
79
80   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
81
82   // Extension Detail JSON Struct for page. |pages| is injected for unit
83   // testing.
84   // Note: |warning_service| can be NULL in unit tests.
85   base::DictionaryValue* CreateExtensionDetailValue(
86       const Extension* extension,
87       const std::vector<ExtensionPage>& pages,
88       const ExtensionWarningService* warning_service);
89
90   void GetLocalizedValues(content::WebUIDataSource* source);
91
92  private:
93   friend class ExtensionUITest;
94   friend class BrokerDelegate;
95
96   // content::WebContentsObserver implementation.
97   virtual void RenderViewDeleted(
98       content::RenderViewHost* render_view_host) OVERRIDE;
99   virtual void DidStartNavigationToPendingEntry(
100       const GURL& url,
101       content::NavigationController::ReloadType reload_type) OVERRIDE;
102
103   // Allows injection for testing by friend classes.
104   ExtensionSettingsHandler(ExtensionService* service,
105                            ManagementPolicy* policy);
106
107   // WebUIMessageHandler implementation.
108   virtual void RegisterMessages() OVERRIDE;
109
110   // ErrorConsole::Observer implementation.
111   virtual void OnErrorAdded(const ExtensionError* error) OVERRIDE;
112
113   // content::NotificationObserver implementation.
114   virtual void Observe(int type,
115                        const content::NotificationSource& source,
116                        const content::NotificationDetails& details) OVERRIDE;
117
118   // ExtensionPrefsObserver implementation.
119   virtual void OnExtensionDisableReasonsChanged(const std::string& extension_id,
120                                                 int disable_reasons) OVERRIDE;
121
122   // ExtensionUninstallDialog::Delegate implementation, used for receiving
123   // notification about uninstall confirmation dialog selections.
124   virtual void ExtensionUninstallAccepted() OVERRIDE;
125   virtual void ExtensionUninstallCanceled() OVERRIDE;
126
127   // ExtensionWarningService::Observer implementation.
128   virtual void ExtensionWarningsChanged() OVERRIDE;
129
130   // ExtensionInstallPrompt::Delegate implementation.
131   virtual void InstallUIProceed() OVERRIDE;
132   virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
133
134   // Helper method that reloads all unpacked extensions.
135   void ReloadUnpackedExtensions();
136
137   // Callback for "requestExtensionsData" message.
138   void HandleRequestExtensionsData(const base::ListValue* args);
139
140   // Callback for "toggleDeveloperMode" message.
141   void HandleToggleDeveloperMode(const base::ListValue* args);
142
143   // Callback for "inspect" message.
144   void HandleInspectMessage(const base::ListValue* args);
145
146   // Callback for "launch" message.
147   void HandleLaunchMessage(const base::ListValue* args);
148
149   // Callback for "reload" message.
150   void HandleReloadMessage(const base::ListValue* args);
151
152   // Callback for "enable" message.
153   void HandleEnableMessage(const base::ListValue* args);
154
155   // Callback for "enableIncognito" message.
156   void HandleEnableIncognitoMessage(const base::ListValue* args);
157
158   // Callback for "enableErrorCollection" message.
159   void HandleEnableErrorCollectionMessage(const base::ListValue* args);
160
161   // Callback for "allowFileAcces" message.
162   void HandleAllowFileAccessMessage(const base::ListValue* args);
163
164   // Callback for "uninstall" message.
165   void HandleUninstallMessage(const base::ListValue* args);
166
167   // Callback for "options" message.
168   void HandleOptionsMessage(const base::ListValue* args);
169
170   // Callback for "permissions" message.
171   void HandlePermissionsMessage(const base::ListValue* args);
172
173   // Callback for "showButton" message.
174   void HandleShowButtonMessage(const base::ListValue* args);
175
176   // Callback for "autoupdate" message.
177   void HandleAutoUpdateMessage(const base::ListValue* args);
178
179   // Callback for the "dismissADTPromo" message.
180   void HandleDismissADTPromoMessage(const base::ListValue* args);
181
182   // Utility for calling JavaScript window.alert in the page.
183   void ShowAlert(const std::string& message);
184
185   // Utility for callbacks that get an extension ID as the sole argument.
186   // Returns NULL if the extension isn't active.
187   const Extension* GetActiveExtension(const base::ListValue* args);
188
189   // Forces a UI update if appropriate after a notification is received.
190   void MaybeUpdateAfterNotification();
191
192   // Register for notifications that we need to reload the page.
193   void MaybeRegisterForNotifications();
194
195   // Helper that lists the current inspectable html pages for an extension.
196   std::vector<ExtensionPage> GetInspectablePagesForExtension(
197       const Extension* extension, bool extension_is_enabled);
198   void GetInspectablePagesForExtensionProcess(
199       const Extension* extension,
200       const std::set<content::RenderViewHost*>& views,
201       std::vector<ExtensionPage>* result);
202   void GetAppWindowPagesForExtensionProfile(const Extension* extension,
203                                             Profile* profile,
204                                             std::vector<ExtensionPage>* result);
205
206   // Returns the ExtensionUninstallDialog object for this class, creating it if
207   // needed.
208   ExtensionUninstallDialog* GetExtensionUninstallDialog();
209
210   // Callback for RequirementsChecker.
211   void OnRequirementsChecked(std::string extension_id,
212                              std::vector<std::string> requirement_errors);
213
214   // Handles the load retry notification sent from
215   // ExtensionService::ReportExtensionLoadError. Attempts to retry loading
216   // extension from |path| if retry is true, otherwise removes |path| from the
217   // vector of currently loading extensions.
218   //
219   // Does nothing if |path| is not a currently loading extension this object is
220   // tracking.
221   void HandleLoadRetryMessage(bool retry, const base::FilePath& path);
222
223   // Our model.  Outlives us since it's owned by our containing profile.
224   ExtensionService* extension_service_;
225
226   // A convenience member, filled once the extension_service_ is known.
227   ManagementPolicy* management_policy_;
228
229   // Used to show confirmation UI for uninstalling extensions in incognito mode.
230   scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_;
231
232   // The id of the extension we are prompting the user about.
233   std::string extension_id_prompting_;
234
235   // If true, we will ignore notifications in ::Observe(). This is needed
236   // to prevent reloading the page when we were the cause of the
237   // notification.
238   bool ignore_notifications_;
239
240   // The page may be refreshed in response to a RenderViewHost being destroyed,
241   // but the iteration over RenderViewHosts will include the host because the
242   // notification is sent when it is in the process of being deleted (and before
243   // it is removed from the process). Keep a pointer to it so we can exclude
244   // it from the active views.
245   content::RenderViewHost* deleting_rvh_;
246   // Do the same for a deleting RenderWidgetHost ID and RenderProcessHost ID.
247   int deleting_rwh_id_;
248   int deleting_rph_id_;
249
250   // We want to register for notifications only after we've responded at least
251   // once to the page, otherwise we'd be calling JavaScript functions on objects
252   // that don't exist yet when notifications come in. This variable makes sure
253   // we do so only once.
254   bool registered_for_notifications_;
255
256   content::NotificationRegistrar registrar_;
257
258   PrefChangeRegistrar pref_registrar_;
259
260   // This will not be empty when a requirements check is in progress. Doing
261   // another Check() before the previous one is complete will cause the first
262   // one to abort.
263   scoped_ptr<RequirementsChecker> requirements_checker_;
264
265   // The UI for showing what permissions the extension has.
266   scoped_ptr<ExtensionInstallPrompt> prompt_;
267
268   ScopedObserver<ExtensionWarningService, ExtensionWarningService::Observer>
269       warning_service_observer_;
270
271   // An observer to listen for when Extension errors are reported.
272   ScopedObserver<ErrorConsole, ErrorConsole::Observer> error_console_observer_;
273
274   // An observer to listen for notable changes in the ExtensionPrefs, like
275   // a change in Disable Reasons.
276   ScopedObserver<ExtensionPrefs, ExtensionPrefsObserver>
277       extension_prefs_observer_;
278
279   // Whether we found any DISABLE_NOT_VERIFIED extensions and want to kick off
280   // a verification check to try and rescue them.
281   bool should_do_verification_check_;
282
283   DISALLOW_COPY_AND_ASSIGN(ExtensionSettingsHandler);
284 };
285
286 }  // namespace extensions
287
288 #endif  // CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_SETTINGS_HANDLER_H_