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 "ui/shell_dialogs/select_file_dialog.h"
29 class ExtensionService
;
32 class DictionaryValue
;
38 class WebUIDataSource
;
41 namespace user_prefs
{
42 class PrefRegistrySyncable
;
45 namespace extensions
{
47 class ManagementPolicy
;
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
,
56 bool generated_background_page
);
58 int render_process_id
;
61 bool generated_background_page
;
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
> {
76 ExtensionSettingsHandler();
77 virtual ~ExtensionSettingsHandler();
79 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
81 // Extension Detail JSON Struct for page. |pages| is injected for unit
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
);
89 void GetLocalizedValues(content::WebUIDataSource
* source
);
92 friend class ExtensionUITest
;
93 friend class BrokerDelegate
;
95 // content::WebContentsObserver implementation.
96 virtual void RenderViewDeleted(
97 content::RenderViewHost
* render_view_host
) OVERRIDE
;
98 virtual void DidStartNavigationToPendingEntry(
100 content::NavigationController::ReloadType reload_type
) OVERRIDE
;
102 // Allows injection for testing by friend classes.
103 ExtensionSettingsHandler(ExtensionService
* service
,
104 ManagementPolicy
* policy
);
106 // WebUIMessageHandler implementation.
107 virtual void RegisterMessages() OVERRIDE
;
109 // SelectFileDialog::Listener implementation.
110 virtual void FileSelected(const base::FilePath
& path
,
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
;
117 // ErrorConsole::Observer implementation.
118 virtual void OnErrorAdded(const ExtensionError
* error
) OVERRIDE
;
120 // content::NotificationObserver implementation.
121 virtual void Observe(int type
,
122 const content::NotificationSource
& source
,
123 const content::NotificationDetails
& details
) OVERRIDE
;
125 // ExtensionUninstallDialog::Delegate implementation, used for receiving
126 // notification about uninstall confirmation dialog selections.
127 virtual void ExtensionUninstallAccepted() OVERRIDE
;
128 virtual void ExtensionUninstallCanceled() OVERRIDE
;
130 // ExtensionWarningService::Observer implementation.
131 virtual void ExtensionWarningsChanged() OVERRIDE
;
133 // ExtensionInstallPrompt::Delegate implementation.
134 virtual void InstallUIProceed() OVERRIDE
;
135 virtual void InstallUIAbort(bool user_initiated
) OVERRIDE
;
137 // Helper method that reloads all unpacked extensions.
138 void ReloadUnpackedExtensions();
140 // Callback for "requestExtensionsData" message.
141 void HandleRequestExtensionsData(const base::ListValue
* args
);
143 // Callback for "toggleDeveloperMode" message.
144 void HandleToggleDeveloperMode(const base::ListValue
* args
);
146 // Callback for "inspect" message.
147 void HandleInspectMessage(const base::ListValue
* args
);
149 // Callback for "launch" message.
150 void HandleLaunchMessage(const base::ListValue
* args
);
152 // Callback for "reload" message.
153 void HandleReloadMessage(const base::ListValue
* args
);
155 // Callback for "enable" message.
156 void HandleEnableMessage(const base::ListValue
* args
);
158 // Callback for "enableIncognito" message.
159 void HandleEnableIncognitoMessage(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 "loadUnpackedExtension" message.
180 void HandleLoadUnpackedExtensionMessage(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 GetShellWindowPagesForExtensionProfile(
203 const Extension
* extension
,
205 std::vector
<ExtensionPage
>* result
);
207 // Returns the ExtensionUninstallDialog object for this class, creating it if
209 ExtensionUninstallDialog
* GetExtensionUninstallDialog();
211 // Callback for RequirementsChecker.
212 void OnRequirementsChecked(std::string extension_id
,
213 std::vector
<std::string
> requirement_errors
);
215 // Our model. Outlives us since it's owned by our containing profile.
216 ExtensionService
* extension_service_
;
218 // A convenience member, filled once the extension_service_ is known.
219 ManagementPolicy
* management_policy_
;
221 // Used to pick the directory when loading an extension.
222 scoped_refptr
<ui::SelectFileDialog
> load_extension_dialog_
;
224 // Used to start the |load_extension_dialog_| in the last directory that was
226 base::FilePath last_unpacked_directory_
;
228 // Used to show confirmation UI for uninstalling extensions in incognito mode.
229 scoped_ptr
<ExtensionUninstallDialog
> extension_uninstall_dialog_
;
231 // The id of the extension we are prompting the user about.
232 std::string extension_id_prompting_
;
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
237 bool ignore_notifications_
;
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_
;
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_
;
255 content::NotificationRegistrar registrar_
;
257 PrefChangeRegistrar pref_registrar_
;
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
262 scoped_ptr
<RequirementsChecker
> requirements_checker_
;
264 // The UI for showing what permissions the extension has.
265 scoped_ptr
<ExtensionInstallPrompt
> prompt_
;
267 ScopedObserver
<ExtensionWarningService
, ExtensionWarningService::Observer
>
268 warning_service_observer_
;
270 // An observer to listen for when Extension errors are reported.
271 ScopedObserver
<ErrorConsole
, ErrorConsole::Observer
> error_console_observer_
;
273 // Whether we found any DISABLE_NOT_VERIFIED extensions and want to kick off
274 // a verification check to try and rescue them.
275 bool should_do_verification_check_
;
277 DISALLOW_COPY_AND_ASSIGN(ExtensionSettingsHandler
);
280 } // namespace extensions
282 #endif // CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_SETTINGS_HANDLER_H_