[email protected] | 764bc25 | 2013-01-08 03:19:03 | [diff] [blame] | 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_EXTENSIONS_EXTENSION_ENABLE_FLOW_H_ |
| 6 | #define CHROME_BROWSER_UI_EXTENSIONS_EXTENSION_ENABLE_FLOW_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
[email protected] | 50736a1 | 2013-09-26 08:58:34 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | 764bc25 | 2013-01-08 03:19:03 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 12 | #include "base/macros.h" |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
[email protected] | a97883a88 | 2014-05-13 11:49:25 | [diff] [blame] | 14 | #include "base/scoped_observer.h" |
[email protected] | 764bc25 | 2013-01-08 03:19:03 | [diff] [blame] | 15 | #include "chrome/browser/extensions/extension_install_prompt.h" |
[email protected] | 7b1e364a | 2013-08-09 15:36:12 | [diff] [blame] | 16 | #include "content/public/browser/notification_observer.h" |
| 17 | #include "content/public/browser/notification_registrar.h" |
[email protected] | a97883a88 | 2014-05-13 11:49:25 | [diff] [blame] | 18 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | 764bc25 | 2013-01-08 03:19:03 | [diff] [blame] | 19 | |
| 20 | class ExtensionEnableFlowDelegate; |
| 21 | |
[email protected] | e237703 | 2013-01-18 17:53:22 | [diff] [blame] | 22 | namespace content { |
[email protected] | e237703 | 2013-01-18 17:53:22 | [diff] [blame] | 23 | class WebContents; |
| 24 | } |
| 25 | |
[email protected] | a97883a88 | 2014-05-13 11:49:25 | [diff] [blame] | 26 | namespace extensions { |
| 27 | class ExtensionRegistry; |
| 28 | } |
| 29 | |
[email protected] | 764bc25 | 2013-01-08 03:19:03 | [diff] [blame] | 30 | // ExtensionEnableFlow performs an UI flow to enable a disabled/terminated |
| 31 | // extension. It calls its delegate when enabling is done or is aborted. |
| 32 | // Callback on the delegate might be called synchronously if there is no |
| 33 | // permission change while the extension is disabled/terminated (or the |
| 34 | // extension is enabled already). Otherwise, a re-enable install prompt is |
| 35 | // shown to user. The extension is enabled when user acknowledges it or the |
| 36 | // flow is aborted when user declines it. |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 37 | class ExtensionEnableFlow : public content::NotificationObserver, |
[email protected] | a97883a88 | 2014-05-13 11:49:25 | [diff] [blame] | 38 | public extensions::ExtensionRegistryObserver { |
[email protected] | 764bc25 | 2013-01-08 03:19:03 | [diff] [blame] | 39 | public: |
| 40 | ExtensionEnableFlow(Profile* profile, |
| 41 | const std::string& extension_id, |
| 42 | ExtensionEnableFlowDelegate* delegate); |
dcheng | 5dd5ff6 | 2014-10-21 12:42:38 | [diff] [blame] | 43 | ~ExtensionEnableFlow() override; |
[email protected] | 764bc25 | 2013-01-08 03:19:03 | [diff] [blame] | 44 | |
| 45 | // Starts the flow and the logic continues on |delegate_| after enabling is |
[email protected] | e237703 | 2013-01-18 17:53:22 | [diff] [blame] | 46 | // finished or aborted. Note that |delegate_| could be called synchronously |
| 47 | // before this call returns when there is no need to show UI to finish the |
[email protected] | 50736a1 | 2013-09-26 08:58:34 | [diff] [blame] | 48 | // enabling flow. Three variations of the flow are supported: |
| 49 | // - one with a parent WebContents |
| 50 | // - one with a native parent window |
| 51 | // - one with a callback for creating a parent window |
[email protected] | e237703 | 2013-01-18 17:53:22 | [diff] [blame] | 52 | void StartForWebContents(content::WebContents* parent_contents); |
| 53 | void StartForNativeWindow(gfx::NativeWindow parent_window); |
[email protected] | 50736a1 | 2013-09-26 08:58:34 | [diff] [blame] | 54 | void StartForCurrentlyNonexistentWindow( |
| 55 | base::Callback<gfx::NativeWindow(void)> window_getter); |
[email protected] | 764bc25 | 2013-01-08 03:19:03 | [diff] [blame] | 56 | |
| 57 | const std::string& extension_id() const { return extension_id_; } |
| 58 | |
| 59 | private: |
[email protected] | 7b1e364a | 2013-08-09 15:36:12 | [diff] [blame] | 60 | // Runs the enable flow. It starts by checking if the extension is loaded. |
| 61 | // If not, it tries to reload it. If the load is asynchronous, wait for the |
| 62 | // load to finish before continuing the flow. Otherwise, calls |
| 63 | // CheckPermissionAndMaybePromptUser finish the flow. |
[email protected] | e237703 | 2013-01-18 17:53:22 | [diff] [blame] | 64 | void Run(); |
| 65 | |
[email protected] | 7b1e364a | 2013-08-09 15:36:12 | [diff] [blame] | 66 | // Checks if there is permission escalation while the extension is |
| 67 | // disabled/terminated. If no, enables the extension and notify |delegate_| |
| 68 | // synchronously. Otherwise, creates an ExtensionInstallPrompt and asks user |
| 69 | // to confirm. |
| 70 | void CheckPermissionAndMaybePromptUser(); |
| 71 | |
[email protected] | e237703 | 2013-01-18 17:53:22 | [diff] [blame] | 72 | // Creates an ExtensionInstallPrompt in |prompt_|. |
| 73 | void CreatePrompt(); |
| 74 | |
[email protected] | 7b1e364a | 2013-08-09 15:36:12 | [diff] [blame] | 75 | // Starts/stops observing extension load notifications. |
| 76 | void StartObserving(); |
| 77 | void StopObserving(); |
| 78 | |
| 79 | // content::NotificationObserver overrides: |
dcheng | 5dd5ff6 | 2014-10-21 12:42:38 | [diff] [blame] | 80 | void Observe(int type, |
| 81 | const content::NotificationSource& source, |
| 82 | const content::NotificationDetails& details) override; |
[email protected] | 7b1e364a | 2013-08-09 15:36:12 | [diff] [blame] | 83 | |
[email protected] | a97883a88 | 2014-05-13 11:49:25 | [diff] [blame] | 84 | // extensions::ExtensionRegistryObserver overrides: |
dcheng | 5dd5ff6 | 2014-10-21 12:42:38 | [diff] [blame] | 85 | void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 86 | const extensions::Extension* extension) override; |
| 87 | void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 88 | const extensions::Extension* extension, |
| 89 | extensions::UninstallReason reason) override; |
[email protected] | a97883a88 | 2014-05-13 11:49:25 | [diff] [blame] | 90 | |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 91 | void InstallPromptDone(ExtensionInstallPrompt::Result result); |
[email protected] | 764bc25 | 2013-01-08 03:19:03 | [diff] [blame] | 92 | |
[email protected] | 764bc25 | 2013-01-08 03:19:03 | [diff] [blame] | 93 | Profile* const profile_; |
| 94 | const std::string extension_id_; |
| 95 | ExtensionEnableFlowDelegate* const delegate_; // Not owned. |
| 96 | |
[email protected] | e237703 | 2013-01-18 17:53:22 | [diff] [blame] | 97 | // Parent web contents for ExtensionInstallPrompt that may be created during |
| 98 | // the flow. Note this is mutually exclusive with |parent_window_| below. |
| 99 | content::WebContents* parent_contents_; |
| 100 | |
| 101 | // Parent native window for ExtensionInstallPrompt. Note this is mutually |
| 102 | // exclusive with |parent_contents_| above. |
| 103 | gfx::NativeWindow parent_window_; |
| 104 | |
[email protected] | 50736a1 | 2013-09-26 08:58:34 | [diff] [blame] | 105 | // Called to acquire a parent window for the prompt. This is used for clients |
| 106 | // who only want to create a window if it is required. |
| 107 | base::Callback<gfx::NativeWindow(void)> window_getter_; |
| 108 | |
dcheng | 9603ab9 | 2016-04-08 04:17:32 | [diff] [blame] | 109 | std::unique_ptr<ExtensionInstallPrompt> prompt_; |
[email protected] | 7b1e364a | 2013-08-09 15:36:12 | [diff] [blame] | 110 | content::NotificationRegistrar registrar_; |
[email protected] | 764bc25 | 2013-01-08 03:19:03 | [diff] [blame] | 111 | |
[email protected] | a97883a88 | 2014-05-13 11:49:25 | [diff] [blame] | 112 | // Listen to extension load notification. |
| 113 | ScopedObserver<extensions::ExtensionRegistry, |
| 114 | extensions::ExtensionRegistryObserver> |
| 115 | extension_registry_observer_; |
| 116 | |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 117 | base::WeakPtrFactory<ExtensionEnableFlow> weak_ptr_factory_; |
| 118 | |
[email protected] | 764bc25 | 2013-01-08 03:19:03 | [diff] [blame] | 119 | DISALLOW_COPY_AND_ASSIGN(ExtensionEnableFlow); |
| 120 | }; |
| 121 | |
| 122 | #endif // CHROME_BROWSER_UI_EXTENSIONS_EXTENSION_ENABLE_FLOW_H_ |