OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/ref_counted.h" |
13 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
14 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
| 16 #include "googleurl/src/gurl.h" |
15 | 17 |
16 class GURL; | 18 class NavigationController; |
17 class Profile; | 19 class Profile; |
18 | 20 |
19 // Downloads and installs extensions from the web store. | 21 // Downloads and installs extensions from the web store. |
20 class WebstoreInstaller : public content::NotificationObserver { | 22 class WebstoreInstaller : public content::NotificationObserver, |
| 23 public base::RefCounted<WebstoreInstaller> { |
21 public: | 24 public: |
22 enum Flag { | 25 enum Flag { |
23 FLAG_NONE = 0, | 26 FLAG_NONE = 0, |
24 | 27 |
25 // Inline installs trigger slightly different behavior (install source | 28 // Inline installs trigger slightly different behavior (install source |
26 // is different, download referrers are the item's page in the gallery). | 29 // is different, download referrers are the item's page in the gallery). |
27 FLAG_INLINE_INSTALL = 1 << 0 | 30 FLAG_INLINE_INSTALL = 1 << 0 |
28 }; | 31 }; |
29 | 32 |
30 class Delegate { | 33 class Delegate { |
31 public: | 34 public: |
32 virtual void OnExtensionInstallSuccess(const std::string& id) = 0; | 35 virtual void OnExtensionInstallSuccess(const std::string& id) = 0; |
33 virtual void OnExtensionInstallFailure(const std::string& id, | 36 virtual void OnExtensionInstallFailure(const std::string& id, |
34 const std::string& error) = 0; | 37 const std::string& error) = 0; |
35 }; | 38 }; |
36 | 39 |
37 explicit WebstoreInstaller(Profile* profile); | 40 |
| 41 // Creates a WebstoreInstaller for downloading and installing the extension |
| 42 // with the given |id| from the Chrome Web Store. If |delegate| is not NULL, |
| 43 // it will be notified when the install succeeds or fails. The installer will |
| 44 // use the specified |controller| to download the extension. Only one |
| 45 // WebstoreInstaller can use a specific controller at any given time. |
| 46 // Note: the delegate should stay alive until being called back. |
| 47 WebstoreInstaller(Profile* profile, |
| 48 Delegate* delegate, |
| 49 NavigationController* controller, |
| 50 const std::string& id, |
| 51 int flags); |
38 virtual ~WebstoreInstaller(); | 52 virtual ~WebstoreInstaller(); |
39 | 53 |
40 // Download and install the extension with the given |id| from the Chrome | 54 // Starts downloading and installing the extension. |
41 // Web Store. If |delegate| is not NULL, it will be notified when the | 55 void Start(); |
42 // install succeeds or fails. | |
43 // Note: the delegate should stay alive until being called back. | |
44 void InstallExtension(const std::string& id, Delegate* delegate, int flags); | |
45 | 56 |
46 // content::NotificationObserver | 57 // content::NotificationObserver |
47 virtual void Observe(int type, | 58 virtual void Observe(int type, |
48 const content::NotificationSource& source, | 59 const content::NotificationSource& source, |
49 const content::NotificationDetails& details) OVERRIDE; | 60 const content::NotificationDetails& details) OVERRIDE; |
50 | 61 |
51 private: | 62 private: |
52 struct PendingInstall; | |
53 | |
54 // Removes the PendingIntall data for the given extension, returning true and | |
55 // setting |install| if there is such data. | |
56 bool ClearPendingInstall(const std::string& id, PendingInstall* install); | |
57 | |
58 // Creates the PendingInstall for the specified extension. | |
59 const PendingInstall& CreatePendingInstall( | |
60 const std::string& id, GURL install_url, Delegate* delegate); | |
61 | |
62 // Gets the extension id for the given gallery install |url|. | |
63 std::string GetPendingInstallId(const GURL& url); | |
64 | |
65 // Reports an install |error| to the delegate for the given extension if this | 63 // Reports an install |error| to the delegate for the given extension if this |
66 // managed its installation. This also removes the associated PendingInstall. | 64 // managed its installation. This also removes the associated PendingInstall. |
67 void ReportFailure(const std::string& id, const std::string& error); | 65 void ReportFailure(const std::string& error); |
68 | 66 |
69 // Reports a successful install to the delegate for the given extension if | 67 // Reports a successful install to the delegate for the given extension if |
70 // this managed its installation. This also removes the associated | 68 // this managed its installation. This also removes the associated |
71 // PendingInstall. | 69 // PendingInstall. |
72 void ReportSuccess(const std::string& id); | 70 void ReportSuccess(); |
73 | 71 |
74 content::NotificationRegistrar registrar_; | 72 content::NotificationRegistrar registrar_; |
75 Profile* profile_; | 73 Profile* profile_; |
76 std::vector<PendingInstall> pending_installs_; | 74 Delegate* delegate_; |
| 75 NavigationController* controller_; |
| 76 std::string id_; |
| 77 int flags_; |
| 78 GURL download_url_; |
77 }; | 79 }; |
78 | 80 |
79 #endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_ | 81 #endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_ |
OLD | NEW |