| 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 #include "chrome/browser/extensions/webstore_installer.h" | 5 #include "chrome/browser/extensions/webstore_installer.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/extensions/crx_installer.h" | 9 #include "chrome/browser/extensions/crx_installer.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 GURL url(url_string + "?response=redirect&x=" + | 42 GURL url(url_string + "?response=redirect&x=" + |
| 43 net::EscapeQueryParamValue(JoinString(params, '&'), true)); | 43 net::EscapeQueryParamValue(JoinString(params, '&'), true)); |
| 44 DCHECK(url.is_valid()); | 44 DCHECK(url.is_valid()); |
| 45 | 45 |
| 46 return url; | 46 return url; |
| 47 } | 47 } |
| 48 | 48 |
| 49 } // namespace | 49 } // namespace |
| 50 | 50 |
| 51 | 51 |
| 52 WebstoreInstaller::WebstoreInstaller(Profile* profile) | 52 WebstoreInstaller::WebstoreInstaller(Profile* profile, |
| 53 : profile_(profile) { | 53 Delegate* delegate, |
| 54 NavigationController* controller, |
| 55 const std::string& id, |
| 56 int flags) |
| 57 : profile_(profile), |
| 58 delegate_(delegate), |
| 59 controller_(controller), |
| 60 id_(id), |
| 61 flags_(flags) { |
| 62 download_url_ = GetWebstoreInstallUrl(id, flags & FLAG_INLINE_INSTALL ? |
| 63 kInlineInstallSource : kDefaultInstallSource); |
| 64 |
| 54 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, | 65 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
| 55 content::Source<Profile>(profile)); | 66 content::Source<Profile>(profile)); |
| 56 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, | 67 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
| 57 content::Source<CrxInstaller>(NULL)); | 68 content::Source<CrxInstaller>(NULL)); |
| 58 } | 69 } |
| 59 | 70 |
| 60 WebstoreInstaller::~WebstoreInstaller() {} | 71 WebstoreInstaller::~WebstoreInstaller() {} |
| 61 | 72 |
| 62 struct WebstoreInstaller::PendingInstall { | 73 void WebstoreInstaller::Start() { |
| 63 PendingInstall() : delegate(NULL) {} | 74 AddRef(); // Balanced in ReportSuccess and ReportFailure. |
| 64 PendingInstall(const std::string& id, const GURL& url, Delegate* delegate) | |
| 65 : id(id), download_url(url), delegate(delegate) {} | |
| 66 ~PendingInstall() {} | |
| 67 | 75 |
| 68 // The id of the extension. | 76 if (!Extension::IdIsValid(id_)) { |
| 69 std::string id; | 77 ReportFailure(kInvalidIdError); |
| 70 | |
| 71 // The gallery download URL for the extension. | |
| 72 GURL download_url; | |
| 73 | |
| 74 // The delegate for this install. | |
| 75 Delegate* delegate; | |
| 76 }; | |
| 77 | |
| 78 void WebstoreInstaller::InstallExtension( | |
| 79 const std::string& id, Delegate* delegate, int flags) { | |
| 80 if (!Extension::IdIsValid(id)) { | |
| 81 ReportFailure(id, kInvalidIdError); | |
| 82 return; | 78 return; |
| 83 } | 79 } |
| 84 | 80 |
| 85 Browser* browser = BrowserList::GetLastActiveWithProfile(profile_); | 81 // TODO(mihaip): For inline installs, we pretend like the referrer is the |
| 86 if (!browser) { | 82 // gallery, even though this could be an inline install, in order to pass the |
| 87 ReportFailure(id, kNoBrowserError); | 83 // checks in ExtensionService::IsDownloadFromGallery. We should instead pass |
| 88 return; | 84 // the real referrer, track if this is an inline install in the whitelist |
| 89 } | 85 // entry and look that up when checking that this is a valid download. |
| 86 GURL referrer = controller_->GetActiveEntry()->url(); |
| 87 if (flags_ & FLAG_INLINE_INSTALL) |
| 88 referrer = GURL(extension_urls::GetWebstoreItemDetailURLPrefix() + id_); |
| 90 | 89 |
| 91 GURL install_url = GetWebstoreInstallUrl(id, flags & FLAG_INLINE_INSTALL ? | 90 // The download url for the given extension is contained in |download_url_|. |
| 92 kInlineInstallSource : kDefaultInstallSource); | 91 // We will navigate the current tab to this url to start the download. The |
| 93 PendingInstall pending_install = CreatePendingInstall( | 92 // download system will then pass the crx to the CrxInstaller. |
| 94 id, install_url, delegate); | 93 controller_->LoadURL(download_url_, |
| 94 referrer, |
| 95 content::PAGE_TRANSITION_LINK, |
| 96 std::string()); |
| 95 | 97 |
| 96 // The download url for the given |id| is now contained in | |
| 97 // |pending_install.download_url|. We navigate the current tab to this url | |
| 98 // which will result in a download starting. Once completed it will go through | |
| 99 // the normal extension install flow. | |
| 100 NavigationController& controller = | |
| 101 browser->tabstrip_model()->GetActiveTabContents()->controller(); | |
| 102 | |
| 103 // TODO(mihaip, jstritar): For inline installs, we pretend like the referrer | |
| 104 // is the gallery, even though this could be an inline install, in order to | |
| 105 // pass the checks in ExtensionService::IsDownloadFromGallery. We should | |
| 106 // instead pass the real referrer, track if this is an inline install in the | |
| 107 // whitelist entry and look that up when checking that this is a valid | |
| 108 // download. | |
| 109 GURL referrer = controller.GetActiveEntry()->url(); | |
| 110 if (flags & FLAG_INLINE_INSTALL) | |
| 111 referrer = GURL(extension_urls::GetWebstoreItemDetailURLPrefix() + id); | |
| 112 | |
| 113 controller.LoadURL(install_url, | |
| 114 referrer, | |
| 115 content::PAGE_TRANSITION_LINK, | |
| 116 std::string()); | |
| 117 } | 98 } |
| 118 | 99 |
| 119 void WebstoreInstaller::Observe(int type, | 100 void WebstoreInstaller::Observe(int type, |
| 120 const content::NotificationSource& source, | 101 const content::NotificationSource& source, |
| 121 const content::NotificationDetails& details) { | 102 const content::NotificationDetails& details) { |
| 122 switch (type) { | 103 switch (type) { |
| 123 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 104 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
| 124 CHECK(profile_->IsSameProfile(content::Source<Profile>(source).ptr())); | 105 CHECK(profile_->IsSameProfile(content::Source<Profile>(source).ptr())); |
| 125 const Extension* extension = | 106 const Extension* extension = |
| 126 content::Details<const Extension>(details).ptr(); | 107 content::Details<const Extension>(details).ptr(); |
| 127 ReportSuccess(extension->id()); | 108 if (id_ == extension->id()) |
| 109 ReportSuccess(); |
| 128 break; | 110 break; |
| 129 } | 111 } |
| 130 | 112 |
| 131 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: { | 113 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: { |
| 132 CrxInstaller* crx_installer = content::Source<CrxInstaller>(source).ptr(); | 114 CrxInstaller* crx_installer = content::Source<CrxInstaller>(source).ptr(); |
| 133 CHECK(crx_installer); | 115 CHECK(crx_installer); |
| 134 if (!profile_->IsSameProfile(crx_installer->profile())) | 116 if (!profile_->IsSameProfile(crx_installer->profile())) |
| 135 return; | 117 return; |
| 136 | 118 |
| 137 std::string id = GetPendingInstallId( | |
| 138 crx_installer->original_download_url()); | |
| 139 const std::string* error = | 119 const std::string* error = |
| 140 content::Details<const std::string>(details).ptr(); | 120 content::Details<const std::string>(details).ptr(); |
| 141 if (!id.empty()) | 121 if (download_url_ == crx_installer->original_download_url()) |
| 142 ReportFailure(id, *error); | 122 ReportFailure(*error); |
| 143 break; | 123 break; |
| 144 } | 124 } |
| 145 | 125 |
| 146 default: | 126 default: |
| 147 NOTREACHED(); | 127 NOTREACHED(); |
| 148 } | 128 } |
| 149 } | 129 } |
| 150 | 130 |
| 151 bool WebstoreInstaller::ClearPendingInstall( | 131 void WebstoreInstaller::ReportFailure(const std::string& error) { |
| 152 const std::string& id, PendingInstall* install) { | 132 if (delegate_) |
| 153 for (size_t i = 0; i < pending_installs_.size(); ++i) { | 133 delegate_->OnExtensionInstallFailure(id_, error); |
| 154 if (pending_installs_[i].id == id) { | 134 |
| 155 *install = pending_installs_[i]; | 135 Release(); // Balanced in Start(). |
| 156 pending_installs_.erase(pending_installs_.begin() + i); | |
| 157 return true; | |
| 158 } | |
| 159 } | |
| 160 return false; | |
| 161 } | 136 } |
| 162 | 137 |
| 163 const WebstoreInstaller::PendingInstall& | 138 void WebstoreInstaller::ReportSuccess() { |
| 164 WebstoreInstaller::CreatePendingInstall( | 139 if (delegate_) |
| 165 const std::string& id, GURL install_url, Delegate* delegate) { | 140 delegate_->OnExtensionInstallSuccess(id_); |
| 166 PendingInstall pending_install(id, install_url, delegate); | |
| 167 pending_installs_.push_back(pending_install); | |
| 168 | 141 |
| 169 return *(pending_installs_.end() - 1); | 142 Release(); // Balanced in Start(). |
| 170 } | 143 } |
| 171 | |
| 172 | |
| 173 std::string WebstoreInstaller::GetPendingInstallId(const GURL& url) { | |
| 174 if (url.is_empty()) | |
| 175 return std::string(); | |
| 176 | |
| 177 for (size_t i = 0; i < pending_installs_.size(); ++i) { | |
| 178 if (pending_installs_[i].download_url == url) | |
| 179 return pending_installs_[i].id; | |
| 180 } | |
| 181 | |
| 182 return std::string(); | |
| 183 } | |
| 184 | |
| 185 void WebstoreInstaller::ReportFailure( | |
| 186 const std::string& id, const std::string& error) { | |
| 187 PendingInstall install; | |
| 188 if (!ClearPendingInstall(id, &install)) | |
| 189 return; | |
| 190 | |
| 191 if (install.delegate) | |
| 192 install.delegate->OnExtensionInstallFailure(id, error); | |
| 193 } | |
| 194 | |
| 195 void WebstoreInstaller::ReportSuccess(const std::string& id) { | |
| 196 PendingInstall install; | |
| 197 if (!ClearPendingInstall(id, &install)) | |
| 198 return; | |
| 199 | |
| 200 if (install.delegate) | |
| 201 install.delegate->OnExtensionInstallSuccess(id); | |
| 202 } | |
| OLD | NEW |