1 // Copyright 2013 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_APPS_EPHEMERAL_APP_LAUNCHER_H_
6 #define CHROME_BROWSER_APPS_EPHEMERAL_APP_LAUNCHER_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/scoped_observer.h"
14 #include "chrome/browser/extensions/webstore_standalone_installer.h"
15 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.h"
16 #include "content/public/browser/web_contents_observer.h"
18 class ExtensionEnableFlow;
25 namespace extensions {
27 class ExtensionInstallChecker;
28 class ExtensionRegistry;
31 // EphemeralAppLauncher manages the launching of ephemeral apps. It handles
32 // display of a prompt, initiates install of the app (if necessary) and finally
34 class EphemeralAppLauncher : public extensions::WebstoreStandaloneInstaller,
35 public content::WebContentsObserver,
36 public ExtensionEnableFlowDelegate {
38 typedef base::Callback<void(extensions::webstore_install::Result result,
39 const std::string& error)> LaunchCallback;
41 // Returns true if launching ephemeral apps is enabled.
42 static bool IsFeatureEnabled();
44 // Create for the app launcher.
45 static scoped_refptr<EphemeralAppLauncher> CreateForLauncher(
46 const std::string& webstore_item_id,
48 gfx::NativeWindow parent_window,
49 const LaunchCallback& callback);
51 // Create for a web contents.
52 static scoped_refptr<EphemeralAppLauncher> CreateForWebContents(
53 const std::string& webstore_item_id,
54 content::WebContents* web_contents,
55 const LaunchCallback& callback);
57 // Initiate app launch.
61 EphemeralAppLauncher(const std::string& webstore_item_id,
63 gfx::NativeWindow parent_window,
64 const LaunchCallback& callback);
65 EphemeralAppLauncher(const std::string& webstore_item_id,
66 content::WebContents* web_contents,
67 const LaunchCallback& callback);
69 ~EphemeralAppLauncher() override;
71 // Creates an install checker. Allows tests to mock the install checker.
72 virtual scoped_ptr<extensions::ExtensionInstallChecker>
73 CreateInstallChecker();
75 // WebstoreStandaloneInstaller implementation overridden in tests.
76 scoped_ptr<ExtensionInstallPrompt> CreateInstallUI() override;
77 scoped_ptr<extensions::WebstoreInstaller::Approval> CreateApproval()
81 friend class base::RefCountedThreadSafe<EphemeralAppLauncher>;
82 friend class EphemeralAppLauncherTest;
84 // Returns true if an app that is already installed in extension system can
86 bool CanLaunchInstalledApp(const extensions::Extension* extension,
87 extensions::webstore_install::Result* reason,
90 // Initiates the enable flow for an app before it can be launched.
91 void EnableInstalledApp(const extensions::Extension* extension);
93 // After the ephemeral installation or enable flow are complete, attempts to
94 // launch the app and notify the client of the outcome.
95 void MaybeLaunchApp();
97 // Launches an app. At this point, it is assumed that the app is enabled and
99 void LaunchApp(const extensions::Extension* extension) const;
101 // Navigates to the launch URL of a hosted app in a new browser tab.
102 bool LaunchHostedApp(const extensions::Extension* extension) const;
104 // Notifies the client of the launch outcome.
105 void InvokeCallback(extensions::webstore_install::Result result,
106 const std::string& error);
108 // Aborts the ephemeral install and notifies the client of the outcome.
109 void AbortLaunch(extensions::webstore_install::Result result,
110 const std::string& error);
112 // Determines whether the app can be installed ephemerally.
113 void CheckEphemeralInstallPermitted();
115 // Install checker callback.
116 void OnInstallChecked(int check_failures);
118 // WebstoreStandaloneInstaller implementation.
119 void InitInstallData(
120 extensions::ActiveInstallData* install_data) const override;
121 bool CheckRequestorAlive() const override;
122 const GURL& GetRequestorURL() const override;
123 bool ShouldShowPostInstallUI() const override;
124 bool ShouldShowAppInstalledBubble() const override;
125 content::WebContents* GetWebContents() const override;
126 scoped_refptr<ExtensionInstallPrompt::Prompt> CreateInstallPrompt()
128 bool CheckInlineInstallPermitted(const base::DictionaryValue& webstore_data,
129 std::string* error) const override;
130 bool CheckRequestorPermitted(const base::DictionaryValue& webstore_data,
131 std::string* error) const override;
132 void OnManifestParsed() override;
133 void CompleteInstall(extensions::webstore_install::Result result,
134 const std::string& error) override;
136 // content::WebContentsObserver implementation.
137 void WebContentsDestroyed() override;
139 // ExtensionEnableFlowDelegate implementation.
140 void ExtensionEnableFlowFinished() override;
141 void ExtensionEnableFlowAborted(bool user_initiated) override;
143 LaunchCallback launch_callback_;
145 gfx::NativeWindow parent_window_;
146 scoped_ptr<content::WebContents> dummy_web_contents_;
148 scoped_ptr<ExtensionEnableFlow> extension_enable_flow_;
150 scoped_ptr<extensions::ExtensionInstallChecker> install_checker_;
152 DISALLOW_COPY_AND_ASSIGN(EphemeralAppLauncher);
155 #endif // CHROME_BROWSER_APPS_EPHEMERAL_APP_LAUNCHER_H_