[email protected] | bdb74a2 | 2012-01-25 20:33:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 734bcec | 2012-10-08 20:29:05 | [diff] [blame] | 5 | #include "chrome/browser/extensions/webstore_standalone_installer.h" |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 6 | |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 7 | #include "base/values.h" |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 8 | #include "base/version.h" |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 9 | #include "chrome/browser/extensions/crx_installer.h" |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_install_prompt.h" |
[email protected] | 453ac12 | 2014-07-28 03:32:13 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_install_ui_util.h" |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 13 | #include "chrome/browser/extensions/install_tracker.h" |
[email protected] | 7b394105 | 2013-02-13 01:21:59 | [diff] [blame] | 14 | #include "chrome/browser/extensions/webstore_data_fetcher.h" |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 15 | #include "chrome/browser/profiles/profile.h" |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 16 | #include "components/crx_file/id_util.h" |
[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 17 | #include "content/public/browser/web_contents.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 18 | #include "extensions/browser/extension_prefs.h" |
[email protected] | 760f743b | 2014-05-28 13:52:02 | [diff] [blame] | 19 | #include "extensions/browser/extension_registry.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 20 | #include "extensions/browser/extension_system.h" |
[email protected] | 411f8ae | 2014-05-22 11:12:23 | [diff] [blame] | 21 | #include "extensions/browser/extension_util.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 22 | #include "extensions/common/extension.h" |
rockot | 9065985 | 2014-09-18 19:31:52 | [diff] [blame] | 23 | #include "extensions/common/extension_urls.h" |
[email protected] | a6483d2 | 2013-07-03 22:11:00 | [diff] [blame] | 24 | #include "url/gurl.h" |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 25 | |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 26 | using content::WebContents; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 27 | |
[email protected] | 3d61a7f | 2012-07-12 19:11:25 | [diff] [blame] | 28 | namespace extensions { |
| 29 | |
[email protected] | a221ef09 | 2011-09-07 01:34:10 | [diff] [blame] | 30 | const char kInvalidWebstoreItemId[] = "Invalid Chrome Web Store item ID"; |
| 31 | const char kWebstoreRequestError[] = |
| 32 | "Could not fetch data from the Chrome Web Store"; |
| 33 | const char kInvalidWebstoreResponseError[] = "Invalid Chrome Web Store reponse"; |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 34 | const char kInvalidManifestError[] = "Invalid manifest"; |
| 35 | const char kUserCancelledError[] = "User cancelled install"; |
[email protected] | f6b88d9 | 2014-01-09 19:45:34 | [diff] [blame] | 36 | const char kExtensionIsBlacklisted[] = "Extension is blacklisted"; |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 37 | const char kInstallInProgressError[] = "An install is already in progress"; |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 38 | |
[email protected] | 734bcec | 2012-10-08 20:29:05 | [diff] [blame] | 39 | WebstoreStandaloneInstaller::WebstoreStandaloneInstaller( |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 40 | const std::string& webstore_item_id, |
[email protected] | c1b2d04 | 2013-02-23 00:31:04 | [diff] [blame] | 41 | Profile* profile, |
[email protected] | c1b2d04 | 2013-02-23 00:31:04 | [diff] [blame] | 42 | const Callback& callback) |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 43 | : id_(webstore_item_id), |
[email protected] | d2a639e | 2012-09-17 07:41:21 | [diff] [blame] | 44 | callback_(callback), |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 45 | profile_(profile), |
[email protected] | 084e3548 | 2013-09-25 02:46:19 | [diff] [blame] | 46 | install_source_(WebstoreInstaller::INSTALL_SOURCE_INLINE), |
[email protected] | dcde34b3 | 2013-07-31 02:28:45 | [diff] [blame] | 47 | show_user_count_(true), |
[email protected] | c7bf745 | 2011-09-12 21:31:50 | [diff] [blame] | 48 | average_rating_(0.0), |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 49 | rating_count_(0) { |
[email protected] | c1b2d04 | 2013-02-23 00:31:04 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | 734bcec | 2012-10-08 20:29:05 | [diff] [blame] | 52 | void WebstoreStandaloneInstaller::BeginInstall() { |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 53 | // Add a ref to keep this alive for WebstoreDataFetcher. |
| 54 | // All code paths from here eventually lead to either CompleteInstall or |
| 55 | // AbortInstall, which both release this ref. |
| 56 | AddRef(); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 57 | |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 58 | if (!crx_file::id_util::IdIsValid(id_)) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 59 | CompleteInstall(webstore_install::INVALID_ID, kInvalidWebstoreItemId); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
[email protected] | ced522c | 2014-07-23 20:23:59 | [diff] [blame] | 63 | webstore_install::Result result = webstore_install::OTHER_ERROR; |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 64 | std::string error; |
| 65 | if (!EnsureUniqueInstall(&result, &error)) { |
| 66 | CompleteInstall(result, error); |
| 67 | return; |
| 68 | } |
| 69 | |
[email protected] | b4574c0 | 2011-11-17 06:19:13 | [diff] [blame] | 70 | // Use the requesting page as the referrer both since that is more correct |
| 71 | // (it is the page that caused this request to happen) and so that we can |
| 72 | // track top sites that trigger inline install requests. |
[email protected] | 7b394105 | 2013-02-13 01:21:59 | [diff] [blame] | 73 | webstore_data_fetcher_.reset(new WebstoreDataFetcher( |
| 74 | this, |
[email protected] | c1b2d04 | 2013-02-23 00:31:04 | [diff] [blame] | 75 | profile_->GetRequestContext(), |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 76 | GetRequestorURL(), |
[email protected] | 7b394105 | 2013-02-13 01:21:59 | [diff] [blame] | 77 | id_)); |
| 78 | webstore_data_fetcher_->Start(); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 81 | // |
| 82 | // Private interface implementation. |
| 83 | // |
| 84 | |
| 85 | WebstoreStandaloneInstaller::~WebstoreStandaloneInstaller() { |
| 86 | } |
| 87 | |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 88 | void WebstoreStandaloneInstaller::RunCallback(bool success, |
| 89 | const std::string& error, |
| 90 | webstore_install::Result result) { |
| 91 | callback_.Run(success, error, result); |
| 92 | } |
| 93 | |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 94 | void WebstoreStandaloneInstaller::AbortInstall() { |
| 95 | callback_.Reset(); |
| 96 | // Abort any in-progress fetches. |
| 97 | if (webstore_data_fetcher_) { |
| 98 | webstore_data_fetcher_.reset(); |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 99 | scoped_active_install_.reset(); |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 100 | Release(); // Matches the AddRef in BeginInstall. |
| 101 | } |
| 102 | } |
| 103 | |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 104 | bool WebstoreStandaloneInstaller::EnsureUniqueInstall( |
| 105 | webstore_install::Result* reason, |
| 106 | std::string* error) { |
| 107 | InstallTracker* tracker = InstallTracker::Get(profile_); |
| 108 | DCHECK(tracker); |
| 109 | |
| 110 | const ActiveInstallData* existing_install_data = |
| 111 | tracker->GetActiveInstall(id_); |
| 112 | if (existing_install_data) { |
benwells | cb4422c1 | 2015-10-13 23:38:46 | [diff] [blame^] | 113 | *reason = webstore_install::INSTALL_IN_PROGRESS; |
| 114 | *error = kInstallInProgressError; |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | |
| 118 | ActiveInstallData install_data(id_); |
| 119 | InitInstallData(&install_data); |
| 120 | scoped_active_install_.reset(new ScopedActiveInstall(tracker, install_data)); |
| 121 | return true; |
| 122 | } |
| 123 | |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 124 | void WebstoreStandaloneInstaller::CompleteInstall( |
| 125 | webstore_install::Result result, |
| 126 | const std::string& error) { |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 127 | scoped_active_install_.reset(); |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 128 | if (!callback_.is_null()) |
[email protected] | ced522c | 2014-07-23 20:23:59 | [diff] [blame] | 129 | callback_.Run(result == webstore_install::SUCCESS, error, result); |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 130 | Release(); // Matches the AddRef in BeginInstall. |
| 131 | } |
| 132 | |
| 133 | void WebstoreStandaloneInstaller::ProceedWithInstallPrompt() { |
| 134 | install_prompt_ = CreateInstallPrompt(); |
dcheng | c704794 | 2014-08-26 05:05:31 | [diff] [blame] | 135 | if (install_prompt_.get()) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 136 | ShowInstallUI(); |
| 137 | // Control flow finishes up in InstallUIProceed or InstallUIAbort. |
| 138 | } else { |
| 139 | InstallUIProceed(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | scoped_refptr<const Extension> |
| 144 | WebstoreStandaloneInstaller::GetLocalizedExtensionForDisplay() { |
| 145 | if (!localized_extension_for_display_.get()) { |
| 146 | DCHECK(manifest_.get()); |
| 147 | if (!manifest_.get()) |
| 148 | return NULL; |
| 149 | |
| 150 | std::string error; |
| 151 | localized_extension_for_display_ = |
| 152 | ExtensionInstallPrompt::GetLocalizedExtensionForDisplay( |
| 153 | manifest_.get(), |
| 154 | Extension::REQUIRE_KEY | Extension::FROM_WEBSTORE, |
| 155 | id_, |
| 156 | localized_name_, |
| 157 | localized_description_, |
| 158 | &error); |
| 159 | } |
| 160 | return localized_extension_for_display_.get(); |
| 161 | } |
| 162 | |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 163 | void WebstoreStandaloneInstaller::InitInstallData( |
| 164 | ActiveInstallData* install_data) const { |
| 165 | // Default implementation sets no properties. |
| 166 | } |
| 167 | |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 168 | void WebstoreStandaloneInstaller::OnManifestParsed() { |
| 169 | ProceedWithInstallPrompt(); |
[email protected] | ce35418b | 2013-11-25 01:22:33 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | f8b23b4 | 2013-06-27 20:12:14 | [diff] [blame] | 172 | scoped_ptr<ExtensionInstallPrompt> |
| 173 | WebstoreStandaloneInstaller::CreateInstallUI() { |
| 174 | return make_scoped_ptr(new ExtensionInstallPrompt(GetWebContents())); |
| 175 | } |
| 176 | |
[email protected] | ce35418b | 2013-11-25 01:22:33 | [diff] [blame] | 177 | scoped_ptr<WebstoreInstaller::Approval> |
| 178 | WebstoreStandaloneInstaller::CreateApproval() const { |
| 179 | scoped_ptr<WebstoreInstaller::Approval> approval( |
| 180 | WebstoreInstaller::Approval::CreateWithNoInstallPrompt( |
| 181 | profile_, |
| 182 | id_, |
| 183 | scoped_ptr<base::DictionaryValue>(manifest_.get()->DeepCopy()), |
| 184 | true)); |
| 185 | approval->skip_post_install_ui = !ShouldShowPostInstallUI(); |
| 186 | approval->use_app_installed_bubble = ShouldShowAppInstalledBubble(); |
| 187 | approval->installing_icon = gfx::ImageSkia::CreateFrom1xBitmap(icon_); |
| 188 | return approval.Pass(); |
| 189 | } |
| 190 | |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 191 | void WebstoreStandaloneInstaller::InstallUIProceed() { |
| 192 | if (!CheckRequestorAlive()) { |
| 193 | CompleteInstall(webstore_install::ABORTED, std::string()); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | scoped_ptr<WebstoreInstaller::Approval> approval = CreateApproval(); |
| 198 | |
| 199 | ExtensionService* extension_service = |
| 200 | ExtensionSystem::Get(profile_)->extension_service(); |
| 201 | const Extension* installed_extension = |
| 202 | extension_service->GetExtensionById(id_, true /* include disabled */); |
| 203 | if (installed_extension) { |
| 204 | std::string install_message; |
| 205 | webstore_install::Result install_result = webstore_install::SUCCESS; |
| 206 | bool done = true; |
| 207 | |
| 208 | if (ExtensionPrefs::Get(profile_)->IsExtensionBlacklisted(id_)) { |
| 209 | // Don't install a blacklisted extension. |
| 210 | install_result = webstore_install::BLACKLISTED; |
| 211 | install_message = kExtensionIsBlacklisted; |
benwells | cb4422c1 | 2015-10-13 23:38:46 | [diff] [blame^] | 212 | } else if (util::IsEphemeralApp(installed_extension->id(), profile_)) { |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 213 | // If the target extension has already been installed ephemerally and is |
| 214 | // up to date, it can be promoted to a regular installed extension and |
| 215 | // downloading from the Web Store is not necessary. |
| 216 | scoped_refptr<const Extension> extension_to_install = |
| 217 | GetLocalizedExtensionForDisplay(); |
| 218 | if (!extension_to_install.get()) { |
| 219 | CompleteInstall(webstore_install::INVALID_MANIFEST, |
| 220 | kInvalidManifestError); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | if (installed_extension->version()->CompareTo( |
| 225 | *extension_to_install->version()) < 0) { |
| 226 | // If the existing extension is out of date, proceed with the install |
| 227 | // to update the extension. |
| 228 | done = false; |
| 229 | } else { |
| 230 | install_ui::ShowPostInstallUIForApproval( |
| 231 | profile_, *approval, installed_extension); |
| 232 | extension_service->PromoteEphemeralApp(installed_extension, false); |
| 233 | } |
| 234 | } else if (!extension_service->IsExtensionEnabled(id_)) { |
| 235 | // If the extension is installed but disabled, and not blacklisted, |
| 236 | // enable it. |
| 237 | extension_service->EnableExtension(id_); |
| 238 | } // else extension is installed and enabled; no work to be done. |
| 239 | |
| 240 | if (done) { |
| 241 | CompleteInstall(install_result, install_message); |
| 242 | return; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller( |
| 247 | profile_, |
| 248 | this, |
| 249 | GetWebContents(), |
| 250 | id_, |
| 251 | approval.Pass(), |
| 252 | install_source_); |
| 253 | installer->Start(); |
| 254 | } |
| 255 | |
| 256 | void WebstoreStandaloneInstaller::InstallUIAbort(bool user_initiated) { |
| 257 | CompleteInstall(webstore_install::USER_CANCELLED, kUserCancelledError); |
| 258 | } |
| 259 | |
[email protected] | 7b394105 | 2013-02-13 01:21:59 | [diff] [blame] | 260 | void WebstoreStandaloneInstaller::OnWebstoreRequestFailure() { |
[email protected] | e263a6b6 | 2014-06-05 23:19:49 | [diff] [blame] | 261 | OnWebStoreDataFetcherDone(); |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 262 | CompleteInstall(webstore_install::WEBSTORE_REQUEST_ERROR, |
| 263 | kWebstoreRequestError); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 264 | } |
| 265 | |
[email protected] | 734bcec | 2012-10-08 20:29:05 | [diff] [blame] | 266 | void WebstoreStandaloneInstaller::OnWebstoreResponseParseSuccess( |
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 267 | scoped_ptr<base::DictionaryValue> webstore_data) { |
[email protected] | e263a6b6 | 2014-06-05 23:19:49 | [diff] [blame] | 268 | OnWebStoreDataFetcherDone(); |
| 269 | |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 270 | if (!CheckRequestorAlive()) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 271 | CompleteInstall(webstore_install::ABORTED, std::string()); |
[email protected] | 4693243e | 2011-09-09 23:52:37 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 275 | std::string error; |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 276 | |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 277 | if (!CheckInlineInstallPermitted(*webstore_data, &error)) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 278 | CompleteInstall(webstore_install::NOT_PERMITTED, error); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 279 | return; |
| 280 | } |
| 281 | |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 282 | if (!CheckRequestorPermitted(*webstore_data, &error)) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 283 | CompleteInstall(webstore_install::NOT_PERMITTED, error); |
[email protected] | dd5161a | 2011-09-14 17:40:17 | [diff] [blame] | 284 | return; |
| 285 | } |
| 286 | |
| 287 | // Manifest, number of users, average rating and rating count are required. |
| 288 | std::string manifest; |
| 289 | if (!webstore_data->GetString(kManifestKey, &manifest) || |
| 290 | !webstore_data->GetString(kUsersKey, &localized_user_count_) || |
[email protected] | 5fdbd6f | 2011-09-01 17:33:04 | [diff] [blame] | 291 | !webstore_data->GetDouble(kAverageRatingKey, &average_rating_) || |
| 292 | !webstore_data->GetInteger(kRatingCountKey, &rating_count_)) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 293 | CompleteInstall(webstore_install::INVALID_WEBSTORE_RESPONSE, |
| 294 | kInvalidWebstoreResponseError); |
[email protected] | 5fdbd6f | 2011-09-01 17:33:04 | [diff] [blame] | 295 | return; |
| 296 | } |
| 297 | |
[email protected] | dcde34b3 | 2013-07-31 02:28:45 | [diff] [blame] | 298 | // Optional. |
| 299 | show_user_count_ = true; |
| 300 | webstore_data->GetBoolean(kShowUserCountKey, &show_user_count_); |
| 301 | |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 302 | if (average_rating_ < ExtensionInstallPrompt::kMinExtensionRating || |
| 303 | average_rating_ > ExtensionInstallPrompt::kMaxExtensionRating) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 304 | CompleteInstall(webstore_install::INVALID_WEBSTORE_RESPONSE, |
| 305 | kInvalidWebstoreResponseError); |
[email protected] | 5fdbd6f | 2011-09-01 17:33:04 | [diff] [blame] | 306 | return; |
| 307 | } |
| 308 | |
| 309 | // Localized name and description are optional. |
| 310 | if ((webstore_data->HasKey(kLocalizedNameKey) && |
| 311 | !webstore_data->GetString(kLocalizedNameKey, &localized_name_)) || |
| 312 | (webstore_data->HasKey(kLocalizedDescriptionKey) && |
| 313 | !webstore_data->GetString( |
| 314 | kLocalizedDescriptionKey, &localized_description_))) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 315 | CompleteInstall(webstore_install::INVALID_WEBSTORE_RESPONSE, |
| 316 | kInvalidWebstoreResponseError); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 317 | return; |
| 318 | } |
| 319 | |
| 320 | // Icon URL is optional. |
| 321 | GURL icon_url; |
| 322 | if (webstore_data->HasKey(kIconUrlKey)) { |
| 323 | std::string icon_url_string; |
| 324 | if (!webstore_data->GetString(kIconUrlKey, &icon_url_string)) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 325 | CompleteInstall(webstore_install::INVALID_WEBSTORE_RESPONSE, |
| 326 | kInvalidWebstoreResponseError); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 327 | return; |
| 328 | } |
| 329 | icon_url = GURL(extension_urls::GetWebstoreLaunchURL()).Resolve( |
| 330 | icon_url_string); |
| 331 | if (!icon_url.is_valid()) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 332 | CompleteInstall(webstore_install::INVALID_WEBSTORE_RESPONSE, |
| 333 | kInvalidWebstoreResponseError); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 334 | return; |
| 335 | } |
| 336 | } |
| 337 | |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 338 | // Assume ownership of webstore_data. |
[email protected] | b3188ec | 2013-11-06 10:26:13 | [diff] [blame] | 339 | webstore_data_ = webstore_data.Pass(); |
[email protected] | a221ef09 | 2011-09-07 01:34:10 | [diff] [blame] | 340 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 341 | scoped_refptr<WebstoreInstallHelper> helper = |
| 342 | new WebstoreInstallHelper(this, |
| 343 | id_, |
| 344 | manifest, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 345 | icon_url, |
| 346 | profile_->GetRequestContext()); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 347 | // The helper will call us back via OnWebstoreParseSucces or |
| 348 | // OnWebstoreParseFailure. |
| 349 | helper->Start(); |
| 350 | } |
| 351 | |
[email protected] | 734bcec | 2012-10-08 20:29:05 | [diff] [blame] | 352 | void WebstoreStandaloneInstaller::OnWebstoreResponseParseFailure( |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 353 | const std::string& error) { |
[email protected] | e263a6b6 | 2014-06-05 23:19:49 | [diff] [blame] | 354 | OnWebStoreDataFetcherDone(); |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 355 | CompleteInstall(webstore_install::INVALID_WEBSTORE_RESPONSE, error); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | 734bcec | 2012-10-08 20:29:05 | [diff] [blame] | 358 | void WebstoreStandaloneInstaller::OnWebstoreParseSuccess( |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 359 | const std::string& id, |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 360 | const SkBitmap& icon, |
| 361 | base::DictionaryValue* manifest) { |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 362 | CHECK_EQ(id_, id); |
| 363 | |
| 364 | if (!CheckRequestorAlive()) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 365 | CompleteInstall(webstore_install::ABORTED, std::string()); |
[email protected] | 2fd920fb | 2011-09-08 23:33:00 | [diff] [blame] | 366 | return; |
| 367 | } |
| 368 | |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 369 | manifest_.reset(manifest); |
| 370 | icon_ = icon; |
| 371 | |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 372 | OnManifestParsed(); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 373 | } |
| 374 | |
[email protected] | 734bcec | 2012-10-08 20:29:05 | [diff] [blame] | 375 | void WebstoreStandaloneInstaller::OnWebstoreParseFailure( |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 376 | const std::string& id, |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 377 | InstallHelperResultCode result_code, |
| 378 | const std::string& error_message) { |
[email protected] | ced522c | 2014-07-23 20:23:59 | [diff] [blame] | 379 | webstore_install::Result install_result = webstore_install::OTHER_ERROR; |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 380 | switch (result_code) { |
| 381 | case WebstoreInstallHelper::Delegate::MANIFEST_ERROR: |
| 382 | install_result = webstore_install::INVALID_MANIFEST; |
| 383 | break; |
| 384 | case WebstoreInstallHelper::Delegate::ICON_ERROR: |
| 385 | install_result = webstore_install::ICON_ERROR; |
| 386 | break; |
| 387 | default: |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | CompleteInstall(install_result, error_message); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 392 | } |
| 393 | |
[email protected] | 734bcec | 2012-10-08 20:29:05 | [diff] [blame] | 394 | void WebstoreStandaloneInstaller::OnExtensionInstallSuccess( |
| 395 | const std::string& id) { |
[email protected] | cb08ba2 | 2011-10-19 21:41:40 | [diff] [blame] | 396 | CHECK_EQ(id_, id); |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 397 | CompleteInstall(webstore_install::SUCCESS, std::string()); |
[email protected] | cb08ba2 | 2011-10-19 21:41:40 | [diff] [blame] | 398 | } |
| 399 | |
[email protected] | 734bcec | 2012-10-08 20:29:05 | [diff] [blame] | 400 | void WebstoreStandaloneInstaller::OnExtensionInstallFailure( |
[email protected] | bcd1eaf7 | 2012-10-03 05:42:29 | [diff] [blame] | 401 | const std::string& id, |
| 402 | const std::string& error, |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 403 | WebstoreInstaller::FailureReason reason) { |
[email protected] | cb08ba2 | 2011-10-19 21:41:40 | [diff] [blame] | 404 | CHECK_EQ(id_, id); |
[email protected] | cb08ba2 | 2011-10-19 21:41:40 | [diff] [blame] | 405 | |
[email protected] | ced522c | 2014-07-23 20:23:59 | [diff] [blame] | 406 | webstore_install::Result install_result = webstore_install::OTHER_ERROR; |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 407 | switch (reason) { |
| 408 | case WebstoreInstaller::FAILURE_REASON_CANCELLED: |
| 409 | install_result = webstore_install::USER_CANCELLED; |
| 410 | break; |
| 411 | case WebstoreInstaller::FAILURE_REASON_DEPENDENCY_NOT_FOUND: |
| 412 | case WebstoreInstaller::FAILURE_REASON_DEPENDENCY_NOT_SHARED_MODULE: |
| 413 | install_result = webstore_install::MISSING_DEPENDENCIES; |
| 414 | break; |
| 415 | default: |
| 416 | break; |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 417 | } |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 418 | |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 419 | CompleteInstall(install_result, error); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 420 | } |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 421 | |
[email protected] | e263a6b6 | 2014-06-05 23:19:49 | [diff] [blame] | 422 | void WebstoreStandaloneInstaller::ShowInstallUI() { |
dcheng | c704794 | 2014-08-26 05:05:31 | [diff] [blame] | 423 | scoped_refptr<const Extension> localized_extension = |
| 424 | GetLocalizedExtensionForDisplay(); |
dcheng | 40eae71 | 2014-08-27 16:56:10 | [diff] [blame] | 425 | if (!localized_extension.get()) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 426 | CompleteInstall(webstore_install::INVALID_MANIFEST, kInvalidManifestError); |
[email protected] | d44ec7b | 2013-03-15 04:34:34 | [diff] [blame] | 427 | return; |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 428 | } |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 429 | |
[email protected] | f8b23b4 | 2013-06-27 20:12:14 | [diff] [blame] | 430 | install_ui_ = CreateInstallUI(); |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 431 | install_ui_->ConfirmStandaloneInstall( |
dcheng | 40eae71 | 2014-08-27 16:56:10 | [diff] [blame] | 432 | this, localized_extension.get(), &icon_, install_prompt_); |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 433 | } |
[email protected] | 3d61a7f | 2012-07-12 19:11:25 | [diff] [blame] | 434 | |
[email protected] | e263a6b6 | 2014-06-05 23:19:49 | [diff] [blame] | 435 | void WebstoreStandaloneInstaller::OnWebStoreDataFetcherDone() { |
| 436 | // An instance of this class is passed in as a delegate for the |
| 437 | // WebstoreInstallHelper, ExtensionInstallPrompt and WebstoreInstaller, and |
| 438 | // therefore needs to remain alive until they are done. Clear the webstore |
| 439 | // data fetcher to avoid calling Release in AbortInstall while any of these |
| 440 | // operations are in progress. |
| 441 | webstore_data_fetcher_.reset(); |
| 442 | } |
| 443 | |
[email protected] | 3d61a7f | 2012-07-12 19:11:25 | [diff] [blame] | 444 | } // namespace extensions |