- add sources.
[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 "ui/shell_dialogs/select_file_dialog.h"
27 #include "url/gurl.h"
28
29 class ExtensionService;
30
31 namespace base {
32 class DictionaryValue;
33 class FilePath;
34 class ListValue;
35 }
36
37 namespace content {
38 class WebUIDataSource;
39 }
40
41 namespace user_prefs {
42 class PrefRegistrySyncable;
43 }
44
45 namespace extensions {
46 class Extension;
47 class ManagementPolicy;
48
49 // Information about a page running in an extension, for example a popup bubble,
50 // a background page, or a tab contents.
51 struct ExtensionPage {
52   ExtensionPage(const GURL& url,
53                 int render_process_id,
54                 int render_view_id,
55                 bool incognito,
56                 bool generated_background_page);
57   GURL url;
58   int render_process_id;
59   int render_view_id;
60   bool incognito;
61   bool generated_background_page;
62 };
63
64 // Extension Settings UI handler.
65 class ExtensionSettingsHandler
66     : public content::WebUIMessageHandler,
67       public content::NotificationObserver,
68       public content::WebContentsObserver,
69       public ui::SelectFileDialog::Listener,
70       public ErrorConsole::Observer,
71       public ExtensionInstallPrompt::Delegate,
72       public ExtensionUninstallDialog::Delegate,
73       public ExtensionWarningService::Observer,
74       public base::SupportsWeakPtr<ExtensionSettingsHandler> {
75  public:
76   ExtensionSettingsHandler();
77   virtual ~ExtensionSettingsHandler();
78
79   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
80
81   // Extension Detail JSON Struct for page. |pages| is injected for unit
82   // testing.
83   // Note: |warning_service| can be NULL in unit tests.
84   base::DictionaryValue* CreateExtensionDetailValue(
85       const Extension* extension,
86       const std::vector<ExtensionPage>& pages,
87       const ExtensionWarningService* warning_service);
88
89   void GetLocalizedValues(content::WebUIDataSource* source);
90
91  private:
92   friend class ExtensionUITest;
93   friend class BrokerDelegate;
94
95   // content::WebContentsObserver implementation.
96   virtual void RenderViewDeleted(
97       content::RenderViewHost* render_view_host) OVERRIDE;
98   virtual void NavigateToPendingEntry(
99       const GURL& url,
100       content::NavigationController::ReloadType reload_type) OVERRIDE;
101
102   // Allows injection for testing by friend classes.
103   ExtensionSettingsHandler(ExtensionService* service,
104                            ManagementPolicy* policy);
105
106   // WebUIMessageHandler implementation.
107   virtual void RegisterMessages() OVERRIDE;
108
109   // SelectFileDialog::Listener implementation.
110   virtual void FileSelected(const base::FilePath& path,
111                             int index,
112                             void* params) OVERRIDE;
113   virtual void MultiFilesSelected(
114       const std::vector<base::FilePath>& files, void* params) OVERRIDE;
115   virtual void FileSelectionCanceled(void* params) OVERRIDE;
116
117   // ErrorConsole::Observer implementation.
118   virtual void OnErrorAdded(const ExtensionError* error) OVERRIDE;
119
120   // content::NotificationObserver implementation.
121   virtual void Observe(int type,
122                        const content::NotificationSource& source,
123                        const content::NotificationDetails& details) OVERRIDE;
124
125   // ExtensionUninstallDialog::Delegate implementation, used for receiving
126   // notification about uninstall confirmation dialog selections.
127   virtual void ExtensionUninstallAccepted() OVERRIDE;
128   virtual void ExtensionUninstallCanceled() OVERRIDE;
129
130   // ExtensionWarningService::Observer implementation.
131   virtual void ExtensionWarningsChanged() OVERRIDE;
132
133   // ExtensionInstallPrompt::Delegate implementation.
134   virtual void InstallUIProceed() OVERRIDE;
135   virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
136
137   // Helper method that reloads all unpacked extensions.
138   void ReloadUnpackedExtensions();
139
140   // Callback for "requestExtensionsData" message.
141   void HandleRequestExtensionsData(const base::ListValue* args);
142
143   // Callback for "toggleDeveloperMode" message.
144   void HandleToggleDeveloperMode(const base::ListValue* args);
145
146   // Callback for "inspect" message.
147   void HandleInspectMessage(const base::ListValue* args);
148
149   // Callback for "launch" message.
150   void HandleLaunchMessage(const ListValue* args);
151
152   // Callback for "reload" message.
153   void HandleReloadMessage(const base::ListValue* args);
154
155   // Callback for "enable" message.
156   void HandleEnableMessage(const base::ListValue* args);
157
158   // Callback for "enableIncognito" message.
159   void HandleEnableIncognitoMessage(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 "loadUnpackedExtension" message.
180   void HandleLoadUnpackedExtensionMessage(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 GetShellWindowPagesForExtensionProfile(
203       const Extension* extension,
204       Profile* profile,
205       std::vector<ExtensionPage>* result);
206
207   // Returns the ExtensionUninstallDialog object for this class, creating it if
208   // needed.
209   ExtensionUninstallDialog* GetExtensionUninstallDialog();
210
211   // Callback for RequirementsChecker.
212   void OnRequirementsChecked(std::string extension_id,
213                              std::vector<std::string> requirement_errors);
214
215   // Our model.  Outlives us since it's owned by our containing profile.
216   ExtensionService* extension_service_;
217
218   // A convenience member, filled once the extension_service_ is known.
219   ManagementPolicy* management_policy_;
220
221   // Used to pick the directory when loading an extension.
222   scoped_refptr<ui::SelectFileDialog> load_extension_dialog_;
223
224   // Used to start the |load_extension_dialog_| in the last directory that was
225   // loaded.
226   base::FilePath last_unpacked_directory_;
227
228   // Used to show confirmation UI for uninstalling extensions in incognito mode.
229   scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_;
230
231   // The id of the extension we are prompting the user about.
232   std::string extension_id_prompting_;
233
234   // If true, we will ignore notifications in ::Observe(). This is needed
235   // to prevent reloading the page when we were the cause of the
236   // notification.
237   bool ignore_notifications_;
238
239   // The page may be refreshed in response to a RenderViewHost being destroyed,
240   // but the iteration over RenderViewHosts will include the host because the
241   // notification is sent when it is in the process of being deleted (and before
242   // it is removed from the process). Keep a pointer to it so we can exclude
243   // it from the active views.
244   content::RenderViewHost* deleting_rvh_;
245   // Do the same for a deleting RenderWidgetHost ID and RenderProcessHost ID.
246   int deleting_rwh_id_;
247   int deleting_rph_id_;
248
249   // We want to register for notifications only after we've responded at least
250   // once to the page, otherwise we'd be calling JavaScript functions on objects
251   // that don't exist yet when notifications come in. This variable makes sure
252   // we do so only once.
253   bool registered_for_notifications_;
254
255   content::NotificationRegistrar registrar_;
256
257   PrefChangeRegistrar pref_registrar_;
258
259   // This will not be empty when a requirements check is in progress. Doing
260   // another Check() before the previous one is complete will cause the first
261   // one to abort.
262   scoped_ptr<RequirementsChecker> requirements_checker_;
263
264   // The UI for showing what permissions the extension has.
265   scoped_ptr<ExtensionInstallPrompt> prompt_;
266
267   ScopedObserver<ExtensionWarningService, ExtensionWarningService::Observer>
268       warning_service_observer_;
269
270   // An observer to listen for when Extension errors are reported.
271   ScopedObserver<ErrorConsole, ErrorConsole::Observer> error_console_observer_;
272
273   DISALLOW_COPY_AND_ASSIGN(ExtensionSettingsHandler);
274 };
275
276 }  // namespace extensions
277
278 #endif  // CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_SETTINGS_HANDLER_H_