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