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_EXTENSIONS_EXTENSION_ENABLE_FLOW_H_
6 #define CHROME_BROWSER_UI_EXTENSIONS_EXTENSION_ENABLE_FLOW_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/scoped_observer.h"
14 #include "chrome/browser/extensions/extension_install_prompt.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "content/public/browser/page_navigator.h"
18 #include "extensions/browser/extension_registry_observer.h"
20 class ExtensionEnableFlowDelegate
;
27 namespace extensions
{
28 class ExtensionRegistry
;
31 // ExtensionEnableFlow performs an UI flow to enable a disabled/terminated
32 // extension. It calls its delegate when enabling is done or is aborted.
33 // Callback on the delegate might be called synchronously if there is no
34 // permission change while the extension is disabled/terminated (or the
35 // extension is enabled already). Otherwise, a re-enable install prompt is
36 // shown to user. The extension is enabled when user acknowledges it or the
37 // flow is aborted when user declines it.
38 class ExtensionEnableFlow
: public ExtensionInstallPrompt::Delegate
,
39 public content::PageNavigator
,
40 public content::NotificationObserver
,
41 public extensions::ExtensionRegistryObserver
{
43 ExtensionEnableFlow(Profile
* profile
,
44 const std::string
& extension_id
,
45 ExtensionEnableFlowDelegate
* delegate
);
46 virtual ~ExtensionEnableFlow();
48 // Starts the flow and the logic continues on |delegate_| after enabling is
49 // finished or aborted. Note that |delegate_| could be called synchronously
50 // before this call returns when there is no need to show UI to finish the
51 // enabling flow. Three variations of the flow are supported:
52 // - one with a parent WebContents
53 // - one with a native parent window
54 // - one with a callback for creating a parent window
55 void StartForWebContents(content::WebContents
* parent_contents
);
56 void StartForNativeWindow(gfx::NativeWindow parent_window
);
57 void StartForCurrentlyNonexistentWindow(
58 base::Callback
<gfx::NativeWindow(void)> window_getter
);
60 const std::string
& extension_id() const { return extension_id_
; }
63 // Runs the enable flow. It starts by checking if the extension is loaded.
64 // If not, it tries to reload it. If the load is asynchronous, wait for the
65 // load to finish before continuing the flow. Otherwise, calls
66 // CheckPermissionAndMaybePromptUser finish the flow.
69 // Checks if there is permission escalation while the extension is
70 // disabled/terminated. If no, enables the extension and notify |delegate_|
71 // synchronously. Otherwise, creates an ExtensionInstallPrompt and asks user
73 void CheckPermissionAndMaybePromptUser();
75 // Creates an ExtensionInstallPrompt in |prompt_|.
78 // Starts/stops observing extension load notifications.
79 void StartObserving();
82 // content::NotificationObserver overrides:
83 virtual void Observe(int type
,
84 const content::NotificationSource
& source
,
85 const content::NotificationDetails
& details
) OVERRIDE
;
87 // extensions::ExtensionRegistryObserver overrides:
88 virtual void OnExtensionLoaded(
89 content::BrowserContext
* browser_context
,
90 const extensions::Extension
* extension
) OVERRIDE
;
91 virtual void OnExtensionUninstalled(
92 content::BrowserContext
* browser_context
,
93 const extensions::Extension
* extension
,
94 extensions::UninstallReason reason
) OVERRIDE
;
96 // ExtensionInstallPrompt::Delegate overrides:
97 virtual void InstallUIProceed() OVERRIDE
;
98 virtual void InstallUIAbort(bool user_initiated
) OVERRIDE
;
100 // content::PageNavigator overrides:
101 virtual content::WebContents
* OpenURL(
102 const content::OpenURLParams
& params
) OVERRIDE
;
104 Profile
* const profile_
;
105 const std::string extension_id_
;
106 ExtensionEnableFlowDelegate
* const delegate_
; // Not owned.
108 // Parent web contents for ExtensionInstallPrompt that may be created during
109 // the flow. Note this is mutually exclusive with |parent_window_| below.
110 content::WebContents
* parent_contents_
;
112 // Parent native window for ExtensionInstallPrompt. Note this is mutually
113 // exclusive with |parent_contents_| above.
114 gfx::NativeWindow parent_window_
;
116 // Called to acquire a parent window for the prompt. This is used for clients
117 // who only want to create a window if it is required.
118 base::Callback
<gfx::NativeWindow(void)> window_getter_
;
120 scoped_ptr
<ExtensionInstallPrompt
> prompt_
;
121 content::NotificationRegistrar registrar_
;
123 // Listen to extension load notification.
124 ScopedObserver
<extensions::ExtensionRegistry
,
125 extensions::ExtensionRegistryObserver
>
126 extension_registry_observer_
;
128 DISALLOW_COPY_AND_ASSIGN(ExtensionEnableFlow
);
131 #endif // CHROME_BROWSER_UI_EXTENSIONS_EXTENSION_ENABLE_FLOW_H_