Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / webui / extensions / extension_settings_handler.h
blob37640beee27490bbc5bc249eb23d51a6fc2c758a
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_
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/memory/scoped_ptr.h"
13 #include "base/scoped_observer.h"
14 #include "chrome/browser/extensions/error_console/error_console.h"
15 #include "chrome/browser/extensions/extension_install_prompt.h"
16 #include "chrome/browser/extensions/extension_install_ui.h"
17 #include "chrome/browser/extensions/extension_management.h"
18 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
19 #include "chrome/browser/extensions/requirements_checker.h"
20 #include "chrome/common/extensions/webstore_install_result.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 "extensions/browser/extension_registry_observer.h"
29 #include "extensions/browser/warning_service.h"
30 #include "url/gurl.h"
32 class ExtensionService;
34 namespace base {
35 class DictionaryValue;
36 class FilePath;
37 class ListValue;
40 namespace content {
41 class WebUIDataSource;
44 namespace user_prefs {
45 class PrefRegistrySyncable;
48 namespace extensions {
49 class Extension;
50 class ExtensionRegistry;
51 class ManagementPolicy;
53 // Information about a page running in an extension, for example a popup bubble,
54 // a background page, or a tab contents.
55 struct ExtensionPage {
56 ExtensionPage(const GURL& url,
57 int render_process_id,
58 int render_view_id,
59 bool incognito,
60 bool generated_background_page);
61 GURL url;
62 int render_process_id;
63 int render_view_id;
64 bool incognito;
65 bool generated_background_page;
68 // Extension Settings UI handler.
69 class ExtensionSettingsHandler
70 : public content::WebUIMessageHandler,
71 public content::NotificationObserver,
72 public content::WebContentsObserver,
73 public ErrorConsole::Observer,
74 public ExtensionInstallPrompt::Delegate,
75 public ExtensionManagement::Observer,
76 public ExtensionPrefsObserver,
77 public ExtensionRegistryObserver,
78 public ExtensionUninstallDialog::Delegate,
79 public WarningService::Observer,
80 public base::SupportsWeakPtr<ExtensionSettingsHandler> {
81 public:
82 ExtensionSettingsHandler();
83 virtual ~ExtensionSettingsHandler();
85 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
87 // Extension Detail JSON Struct for page. |pages| is injected for unit
88 // testing.
89 // Note: |warning_service| can be NULL in unit tests.
90 base::DictionaryValue* CreateExtensionDetailValue(
91 const Extension* extension,
92 const std::vector<ExtensionPage>& pages,
93 const WarningService* warning_service);
95 void GetLocalizedValues(content::WebUIDataSource* source);
97 private:
98 friend class ExtensionUITest;
99 friend class BrokerDelegate;
101 // content::WebContentsObserver implementation.
102 virtual void RenderViewDeleted(
103 content::RenderViewHost* render_view_host) override;
104 virtual void DidStartNavigationToPendingEntry(
105 const GURL& url,
106 content::NavigationController::ReloadType reload_type) override;
108 // Allows injection for testing by friend classes.
109 ExtensionSettingsHandler(ExtensionService* service,
110 ManagementPolicy* policy);
112 // WebUIMessageHandler implementation.
113 virtual void RegisterMessages() override;
115 // ErrorConsole::Observer implementation.
116 virtual void OnErrorAdded(const ExtensionError* error) override;
118 // content::NotificationObserver implementation.
119 virtual void Observe(int type,
120 const content::NotificationSource& source,
121 const content::NotificationDetails& details) override;
123 // ExtensionRegistryObserver implementation.
124 virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
125 const Extension* extension) override;
126 virtual void OnExtensionUnloaded(
127 content::BrowserContext* browser_context,
128 const Extension* extension,
129 UnloadedExtensionInfo::Reason reason) override;
130 virtual void OnExtensionUninstalled(
131 content::BrowserContext* browser_context,
132 const Extension* extension,
133 extensions::UninstallReason reason) override;
135 // ExtensionPrefsObserver implementation.
136 virtual void OnExtensionDisableReasonsChanged(const std::string& extension_id,
137 int disable_reasons) override;
139 // ExtensionManagement::Observer implementation.
140 virtual void OnExtensionManagementSettingsChanged() override;
142 // ExtensionUninstallDialog::Delegate implementation, used for receiving
143 // notification about uninstall confirmation dialog selections.
144 virtual void ExtensionUninstallAccepted() override;
145 virtual void ExtensionUninstallCanceled() override;
147 // WarningService::Observer implementation.
148 virtual void ExtensionWarningsChanged() override;
150 // ExtensionInstallPrompt::Delegate implementation.
151 virtual void InstallUIProceed() override;
152 virtual void InstallUIAbort(bool user_initiated) override;
154 // Called after the App Info Dialog has closed.
155 virtual void AppInfoDialogClosed();
157 // Helper method that reloads all unpacked extensions.
158 void ReloadUnpackedExtensions();
160 // Callback for "requestExtensionsData" message.
161 void HandleRequestExtensionsData(const base::ListValue* args);
163 // Callback for "toggleDeveloperMode" message.
164 void HandleToggleDeveloperMode(const base::ListValue* args);
166 // Callback for "inspect" message.
167 void HandleInspectMessage(const base::ListValue* args);
169 // Callback for "launch" message.
170 void HandleLaunchMessage(const base::ListValue* args);
172 // Callback for "reload" message.
173 void HandleReloadMessage(const base::ListValue* args);
175 // Callback for "repair" message.
176 void HandleRepairMessage(const base::ListValue* args);
178 // Callback for "enable" message.
179 void HandleEnableMessage(const base::ListValue* args);
181 // Callback for "enableIncognito" message.
182 void HandleEnableIncognitoMessage(const base::ListValue* args);
184 // Callback for "enableErrorCollection" message.
185 void HandleEnableErrorCollectionMessage(const base::ListValue* args);
187 // Callback for "allowFileAcces" message.
188 void HandleAllowFileAccessMessage(const base::ListValue* args);
190 // Callback for "allowOnAllUrls" message.
191 void HandleAllowOnAllUrlsMessage(const base::ListValue* args);
193 // Callback for "uninstall" message.
194 void HandleUninstallMessage(const base::ListValue* args);
196 // Callback for "options" message.
197 void HandleOptionsMessage(const base::ListValue* args);
199 // Callback for "permissions" message.
200 void HandlePermissionsMessage(const base::ListValue* args);
202 // Callback for "showButton" message.
203 void HandleShowButtonMessage(const base::ListValue* args);
205 // Callback for "autoupdate" message.
206 void HandleAutoUpdateMessage(const base::ListValue* args);
208 // Callback for the "dismissADTPromo" message.
209 void HandleDismissADTPromoMessage(const base::ListValue* args);
211 // Callback for the "showPath" message.
212 void HandleShowPath(const base::ListValue* args);
214 // Utility for calling JavaScript window.alert in the page.
215 void ShowAlert(const std::string& message);
217 // Utility for callbacks that get an extension ID as the sole argument.
218 // Returns NULL if the extension isn't active.
219 const Extension* GetActiveExtension(const base::ListValue* args);
221 // Forces a UI update if appropriate after a notification is received.
222 void MaybeUpdateAfterNotification();
224 // Register for notifications that we need to reload the page.
225 void MaybeRegisterForNotifications();
227 // Helper that lists the current inspectable html pages for an extension.
228 std::vector<ExtensionPage> GetInspectablePagesForExtension(
229 const Extension* extension, bool extension_is_enabled);
230 void GetInspectablePagesForExtensionProcess(
231 const Extension* extension,
232 const std::set<content::RenderViewHost*>& views,
233 std::vector<ExtensionPage>* result);
234 void GetAppWindowPagesForExtensionProfile(const Extension* extension,
235 Profile* profile,
236 std::vector<ExtensionPage>* result);
238 // Returns the ExtensionUninstallDialog object for this class, creating it if
239 // needed.
240 ExtensionUninstallDialog* GetExtensionUninstallDialog();
242 // Called when the reinstallation is complete.
243 void OnReinstallComplete(bool success,
244 const std::string& error,
245 webstore_install::Result result);
247 // Callback for RequirementsChecker.
248 void OnRequirementsChecked(std::string extension_id,
249 std::vector<std::string> requirement_errors);
251 // Handles the load retry notification sent from
252 // ExtensionService::ReportExtensionLoadError. Attempts to retry loading
253 // extension from |path| if retry is true, otherwise removes |path| from the
254 // vector of currently loading extensions.
256 // Does nothing if |path| is not a currently loading extension this object is
257 // tracking.
258 void HandleLoadRetryMessage(bool retry, const base::FilePath& path);
260 // Our model. Outlives us since it's owned by our containing profile.
261 ExtensionService* extension_service_;
263 // A convenience member, filled once the extension_service_ is known.
264 ManagementPolicy* management_policy_;
266 // Used to show confirmation UI for uninstalling extensions in incognito mode.
267 scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_;
269 // The id of the extension we are prompting the user about.
270 std::string extension_id_prompting_;
272 // If true, we will ignore notifications in ::Observe(). This is needed
273 // to prevent reloading the page when we were the cause of the
274 // notification.
275 bool ignore_notifications_;
277 // The page may be refreshed in response to a RenderViewHost being destroyed,
278 // but the iteration over RenderViewHosts will include the host because the
279 // notification is sent when it is in the process of being deleted (and before
280 // it is removed from the process). Keep a pointer to it so we can exclude
281 // it from the active views.
282 content::RenderViewHost* deleting_rvh_;
283 // Do the same for a deleting RenderWidgetHost ID and RenderProcessHost ID.
284 int deleting_rwh_id_;
285 int deleting_rph_id_;
287 // We want to register for notifications only after we've responded at least
288 // once to the page, otherwise we'd be calling JavaScript functions on objects
289 // that don't exist yet when notifications come in. This variable makes sure
290 // we do so only once.
291 bool registered_for_notifications_;
293 content::NotificationRegistrar registrar_;
295 // This will not be empty when a requirements check is in progress. Doing
296 // another Check() before the previous one is complete will cause the first
297 // one to abort.
298 scoped_ptr<RequirementsChecker> requirements_checker_;
300 // The UI for showing what permissions the extension has.
301 scoped_ptr<ExtensionInstallPrompt> prompt_;
303 ScopedObserver<WarningService, WarningService::Observer>
304 warning_service_observer_;
306 // An observer to listen for when Extension errors are reported.
307 ScopedObserver<ErrorConsole, ErrorConsole::Observer> error_console_observer_;
309 // An observer to listen for notable changes in the ExtensionPrefs, like
310 // a change in Disable Reasons.
311 ScopedObserver<ExtensionPrefs, ExtensionPrefsObserver>
312 extension_prefs_observer_;
314 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
315 extension_registry_observer_;
317 ScopedObserver<ExtensionManagement, ExtensionManagement::Observer>
318 extension_management_observer_;
320 // Whether we found any DISABLE_NOT_VERIFIED extensions and want to kick off
321 // a verification check to try and rescue them.
322 bool should_do_verification_check_;
324 DISALLOW_COPY_AND_ASSIGN(ExtensionSettingsHandler);
327 } // namespace extensions
329 #endif // CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_SETTINGS_HANDLER_H_