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 10 #include "base/basictypes.h" 11 #include "base/compiler_specific.h" 12 #include "chrome/browser/extensions/extension_install_prompt.h" 13 #include "content/public/browser/notification_observer.h" 14 #include "content/public/browser/notification_registrar.h" 15 #include "content/public/browser/page_navigator.h" 16 17 class ExtensionEnableFlowDelegate; 18 19 namespace content { 20 class PageNavigator; 21 class WebContents; 22 } 23 24 // ExtensionEnableFlow performs an UI flow to enable a disabled/terminated 25 // extension. It calls its delegate when enabling is done or is aborted. 26 // Callback on the delegate might be called synchronously if there is no 27 // permission change while the extension is disabled/terminated (or the 28 // extension is enabled already). Otherwise, a re-enable install prompt is 29 // shown to user. The extension is enabled when user acknowledges it or the 30 // flow is aborted when user declines it. 31 class ExtensionEnableFlow : public ExtensionInstallPrompt::Delegate, 32 public content::PageNavigator, 33 public content::NotificationObserver { 34 public: 35 ExtensionEnableFlow(Profile* profile, 36 const std::string& extension_id, 37 ExtensionEnableFlowDelegate* delegate); 38 virtual ~ExtensionEnableFlow(); 39 40 // Starts the flow and the logic continues on |delegate_| after enabling is 41 // finished or aborted. Note that |delegate_| could be called synchronously 42 // before this call returns when there is no need to show UI to finish the 43 // enabling flow. Two variations of the flow are supported: one with a 44 // parent WebContents and the other with a native parent window. 45 void StartForWebContents(content::WebContents* parent_contents); 46 void StartForNativeWindow(gfx::NativeWindow parent_window); 47 48 const std::string& extension_id() const { return extension_id_; } 49 50 private: 51 // Runs the enable flow. It starts by checking if the extension is loaded. 52 // If not, it tries to reload it. If the load is asynchronous, wait for the 53 // load to finish before continuing the flow. Otherwise, calls 54 // CheckPermissionAndMaybePromptUser finish the flow. 55 void Run(); 56 57 // Checks if there is permission escalation while the extension is 58 // disabled/terminated. If no, enables the extension and notify |delegate_| 59 // synchronously. Otherwise, creates an ExtensionInstallPrompt and asks user 60 // to confirm. 61 void CheckPermissionAndMaybePromptUser(); 62 63 // Creates an ExtensionInstallPrompt in |prompt_|. 64 void CreatePrompt(); 65 66 // Starts/stops observing extension load notifications. 67 void StartObserving(); 68 void StopObserving(); 69 70 // content::NotificationObserver overrides: 71 virtual void Observe(int type, 72 const content::NotificationSource& source, 73 const content::NotificationDetails& details) OVERRIDE; 74 75 // ExtensionInstallPrompt::Delegate overrides: 76 virtual void InstallUIProceed() OVERRIDE; 77 virtual void InstallUIAbort(bool user_initiated) OVERRIDE; 78 79 // content::PageNavigator overrides: 80 virtual content::WebContents* OpenURL( 81 const content::OpenURLParams& params) OVERRIDE; 82 83 Profile* const profile_; 84 const std::string extension_id_; 85 ExtensionEnableFlowDelegate* const delegate_; // Not owned. 86 87 // Parent web contents for ExtensionInstallPrompt that may be created during 88 // the flow. Note this is mutually exclusive with |parent_window_| below. 89 content::WebContents* parent_contents_; 90 91 // Parent native window for ExtensionInstallPrompt. Note this is mutually 92 // exclusive with |parent_contents_| above. 93 gfx::NativeWindow parent_window_; 94 95 scoped_ptr<ExtensionInstallPrompt> prompt_; 96 content::NotificationRegistrar registrar_; 97 98 DISALLOW_COPY_AND_ASSIGN(ExtensionEnableFlow); 99 }; 100 101 #endif // CHROME_BROWSER_UI_EXTENSIONS_EXTENSION_ENABLE_FLOW_H_ 102