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_UI_WEBUI_EXTENSIONS_EXTENSION_SETTINGS_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_SETTINGS_HANDLER_H_
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"
30 class ExtensionService;
33 class DictionaryValue;
39 class WebUIDataSource;
42 namespace user_prefs {
43 class PrefRegistrySyncable;
46 namespace extensions {
48 class ManagementPolicy;
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,
57 bool generated_background_page);
59 int render_process_id;
62 bool generated_background_page;
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> {
77 ExtensionSettingsHandler();
78 virtual ~ExtensionSettingsHandler();
80 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
82 // Extension Detail JSON Struct for page. |pages| is injected for unit
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);
90 void GetLocalizedValues(content::WebUIDataSource* source);
93 friend class ExtensionUITest;
94 friend class BrokerDelegate;
96 // content::WebContentsObserver implementation.
97 virtual void RenderViewDeleted(
98 content::RenderViewHost* render_view_host) OVERRIDE;
99 virtual void DidStartNavigationToPendingEntry(
101 content::NavigationController::ReloadType reload_type) OVERRIDE;
103 // Allows injection for testing by friend classes.
104 ExtensionSettingsHandler(ExtensionService* service,
105 ManagementPolicy* policy);
107 // WebUIMessageHandler implementation.
108 virtual void RegisterMessages() OVERRIDE;
110 // ErrorConsole::Observer implementation.
111 virtual void OnErrorAdded(const ExtensionError* error) OVERRIDE;
113 // content::NotificationObserver implementation.
114 virtual void Observe(int type,
115 const content::NotificationSource& source,
116 const content::NotificationDetails& details) OVERRIDE;
118 // ExtensionPrefsObserver implementation.
119 virtual void OnExtensionDisableReasonsChanged(const std::string& extension_id,
120 int disable_reasons) OVERRIDE;
122 // ExtensionUninstallDialog::Delegate implementation, used for receiving
123 // notification about uninstall confirmation dialog selections.
124 virtual void ExtensionUninstallAccepted() OVERRIDE;
125 virtual void ExtensionUninstallCanceled() OVERRIDE;
127 // ExtensionWarningService::Observer implementation.
128 virtual void ExtensionWarningsChanged() OVERRIDE;
130 // ExtensionInstallPrompt::Delegate implementation.
131 virtual void InstallUIProceed() OVERRIDE;
132 virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
134 // Helper method that reloads all unpacked extensions.
135 void ReloadUnpackedExtensions();
137 // Callback for "requestExtensionsData" message.
138 void HandleRequestExtensionsData(const base::ListValue* args);
140 // Callback for "toggleDeveloperMode" message.
141 void HandleToggleDeveloperMode(const base::ListValue* args);
143 // Callback for "inspect" message.
144 void HandleInspectMessage(const base::ListValue* args);
146 // Callback for "launch" message.
147 void HandleLaunchMessage(const base::ListValue* args);
149 // Callback for "reload" message.
150 void HandleReloadMessage(const base::ListValue* args);
152 // Callback for "enable" message.
153 void HandleEnableMessage(const base::ListValue* args);
155 // Callback for "enableIncognito" message.
156 void HandleEnableIncognitoMessage(const base::ListValue* args);
158 // Callback for "enableErrorCollection" message.
159 void HandleEnableErrorCollectionMessage(const base::ListValue* args);
161 // Callback for "allowFileAcces" message.
162 void HandleAllowFileAccessMessage(const base::ListValue* args);
164 // Callback for "uninstall" message.
165 void HandleUninstallMessage(const base::ListValue* args);
167 // Callback for "options" message.
168 void HandleOptionsMessage(const base::ListValue* args);
170 // Callback for "permissions" message.
171 void HandlePermissionsMessage(const base::ListValue* args);
173 // Callback for "showButton" message.
174 void HandleShowButtonMessage(const base::ListValue* args);
176 // Callback for "autoupdate" message.
177 void HandleAutoUpdateMessage(const base::ListValue* args);
179 // Callback for the "dismissADTPromo" message.
180 void HandleDismissADTPromoMessage(const base::ListValue* args);
182 // Utility for calling JavaScript window.alert in the page.
183 void ShowAlert(const std::string& message);
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);
189 // Forces a UI update if appropriate after a notification is received.
190 void MaybeUpdateAfterNotification();
192 // Register for notifications that we need to reload the page.
193 void MaybeRegisterForNotifications();
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,
204 std::vector<ExtensionPage>* result);
206 // Returns the ExtensionUninstallDialog object for this class, creating it if
208 ExtensionUninstallDialog* GetExtensionUninstallDialog();
210 // Callback for RequirementsChecker.
211 void OnRequirementsChecked(std::string extension_id,
212 std::vector<std::string> requirement_errors);
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.
219 // Does nothing if |path| is not a currently loading extension this object is
221 void HandleLoadRetryMessage(bool retry, const base::FilePath& path);
223 // Our model. Outlives us since it's owned by our containing profile.
224 ExtensionService* extension_service_;
226 // A convenience member, filled once the extension_service_ is known.
227 ManagementPolicy* management_policy_;
229 // Used to show confirmation UI for uninstalling extensions in incognito mode.
230 scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_;
232 // The id of the extension we are prompting the user about.
233 std::string extension_id_prompting_;
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
238 bool ignore_notifications_;
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_;
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_;
256 content::NotificationRegistrar registrar_;
258 PrefChangeRegistrar pref_registrar_;
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
263 scoped_ptr<RequirementsChecker> requirements_checker_;
265 // The UI for showing what permissions the extension has.
266 scoped_ptr<ExtensionInstallPrompt> prompt_;
268 ScopedObserver<ExtensionWarningService, ExtensionWarningService::Observer>
269 warning_service_observer_;
271 // An observer to listen for when Extension errors are reported.
272 ScopedObserver<ErrorConsole, ErrorConsole::Observer> error_console_observer_;
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_;
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_;
283 DISALLOW_COPY_AND_ASSIGN(ExtensionSettingsHandler);
286 } // namespace extensions
288 #endif // CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_SETTINGS_HANDLER_H_