blob: 7c6f9692fbde993b139aeb025fbb450f47df5ef8 [file] [log] [blame]
[email protected]bdb74a22012-01-25 20:33:331// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]8915f342011-08-29 22:14:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]734bcec2012-10-08 20:29:055#include "chrome/browser/extensions/webstore_standalone_installer.h"
[email protected]8915f342011-08-29 22:14:376
[email protected]8915f342011-08-29 22:14:377#include "base/values.h"
[email protected]8915f342011-08-29 22:14:378#include "chrome/browser/extensions/crx_installer.h"
[email protected]d44ec7b2013-03-15 04:34:349#include "chrome/browser/extensions/extension_install_prompt.h"
[email protected]00b38242012-07-18 18:43:2210#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]655b2b1a2011-10-13 17:13:0611#include "chrome/browser/extensions/extension_service.h"
[email protected]7b3941052013-02-13 01:21:5912#include "chrome/browser/extensions/webstore_data_fetcher.h"
[email protected]8915f342011-08-29 22:14:3713#include "chrome/browser/profiles/profile.h"
[email protected]8915f342011-08-29 22:14:3714#include "chrome/common/extensions/extension.h"
[email protected]10c2d692012-05-11 05:32:2315#include "content/public/browser/web_contents.h"
[email protected]a6483d22013-07-03 22:11:0016#include "url/gurl.h"
[email protected]8915f342011-08-29 22:14:3717
[email protected]26b5e322011-12-23 01:36:4718using content::WebContents;
[email protected]631bb742011-11-02 11:29:3919
[email protected]3d61a7f2012-07-12 19:11:2520namespace extensions {
21
[email protected]8915f342011-08-29 22:14:3722const char kManifestKey[] = "manifest";
23const char kIconUrlKey[] = "icon_url";
24const char kLocalizedNameKey[] = "localized_name";
[email protected]5fdbd6f2011-09-01 17:33:0425const char kLocalizedDescriptionKey[] = "localized_description";
26const char kUsersKey[] = "users";
[email protected]dcde34b32013-07-31 02:28:4527const char kShowUserCountKey[] = "show_user_count";
[email protected]5fdbd6f2011-09-01 17:33:0428const char kAverageRatingKey[] = "average_rating";
29const char kRatingCountKey[] = "rating_count";
[email protected]dd5161a2011-09-14 17:40:1730const char kRedirectUrlKey[] = "redirect_url";
[email protected]8915f342011-08-29 22:14:3731
[email protected]a221ef092011-09-07 01:34:1032const char kInvalidWebstoreItemId[] = "Invalid Chrome Web Store item ID";
33const char kWebstoreRequestError[] =
34 "Could not fetch data from the Chrome Web Store";
35const char kInvalidWebstoreResponseError[] = "Invalid Chrome Web Store reponse";
[email protected]8915f342011-08-29 22:14:3736const char kInvalidManifestError[] = "Invalid manifest";
37const char kUserCancelledError[] = "User cancelled install";
[email protected]d44ec7b2013-03-15 04:34:3438
[email protected]8915f342011-08-29 22:14:3739
[email protected]734bcec2012-10-08 20:29:0540WebstoreStandaloneInstaller::WebstoreStandaloneInstaller(
[email protected]d44ec7b2013-03-15 04:34:3441 const std::string& webstore_item_id,
[email protected]c1b2d042013-02-23 00:31:0442 Profile* profile,
[email protected]c1b2d042013-02-23 00:31:0443 const Callback& callback)
[email protected]d44ec7b2013-03-15 04:34:3444 : id_(webstore_item_id),
[email protected]d2a639e2012-09-17 07:41:2145 callback_(callback),
[email protected]d44ec7b2013-03-15 04:34:3446 profile_(profile),
[email protected]084e35482013-09-25 02:46:1947 install_source_(WebstoreInstaller::INSTALL_SOURCE_INLINE),
[email protected]dcde34b32013-07-31 02:28:4548 show_user_count_(true),
[email protected]c7bf7452011-09-12 21:31:5049 average_rating_(0.0),
[email protected]5f2a4752012-04-27 22:18:5850 rating_count_(0) {
[email protected]c1b2d042013-02-23 00:31:0451 CHECK(!callback_.is_null());
52}
53
[email protected]d44ec7b2013-03-15 04:34:3454WebstoreStandaloneInstaller::~WebstoreStandaloneInstaller() {}
55
56//
57// Private interface implementation.
58//
[email protected]8915f342011-08-29 22:14:3759
[email protected]734bcec2012-10-08 20:29:0560void WebstoreStandaloneInstaller::BeginInstall() {
[email protected]5db2e882012-12-20 10:17:2661 AddRef(); // Balanced in CompleteInstall or WebContentsDestroyed.
[email protected]8915f342011-08-29 22:14:3762
[email protected]3d61a7f2012-07-12 19:11:2563 if (!Extension::IdIsValid(id_)) {
[email protected]8915f342011-08-29 22:14:3764 CompleteInstall(kInvalidWebstoreItemId);
65 return;
66 }
67
[email protected]b4574c02011-11-17 06:19:1368 // Use the requesting page as the referrer both since that is more correct
69 // (it is the page that caused this request to happen) and so that we can
70 // track top sites that trigger inline install requests.
[email protected]7b3941052013-02-13 01:21:5971 webstore_data_fetcher_.reset(new WebstoreDataFetcher(
72 this,
[email protected]c1b2d042013-02-23 00:31:0473 profile_->GetRequestContext(),
[email protected]d44ec7b2013-03-15 04:34:3474 GetRequestorURL(),
[email protected]7b3941052013-02-13 01:21:5975 id_));
76 webstore_data_fetcher_->Start();
[email protected]8915f342011-08-29 22:14:3777}
78
[email protected]f8b23b42013-06-27 20:12:1479scoped_ptr<ExtensionInstallPrompt>
80WebstoreStandaloneInstaller::CreateInstallUI() {
81 return make_scoped_ptr(new ExtensionInstallPrompt(GetWebContents()));
82}
83
[email protected]7b3941052013-02-13 01:21:5984void WebstoreStandaloneInstaller::OnWebstoreRequestFailure() {
85 CompleteInstall(kWebstoreRequestError);
[email protected]8915f342011-08-29 22:14:3786}
87
[email protected]734bcec2012-10-08 20:29:0588void WebstoreStandaloneInstaller::OnWebstoreResponseParseSuccess(
[email protected]8915f342011-08-29 22:14:3789 DictionaryValue* webstore_data) {
[email protected]d44ec7b2013-03-15 04:34:3490 if (!CheckRequestorAlive()) {
[email protected]007b3f82013-04-09 08:46:4591 CompleteInstall(std::string());
[email protected]4693243e2011-09-09 23:52:3792 return;
93 }
94
[email protected]d44ec7b2013-03-15 04:34:3495 std::string error;
[email protected]8915f342011-08-29 22:14:3796
[email protected]d44ec7b2013-03-15 04:34:3497 if (!CheckInlineInstallPermitted(*webstore_data, &error)) {
98 CompleteInstall(error);
[email protected]8915f342011-08-29 22:14:3799 return;
100 }
[email protected]8915f342011-08-29 22:14:37101
[email protected]d44ec7b2013-03-15 04:34:34102 if (!CheckRequestorPermitted(*webstore_data, &error)) {
103 CompleteInstall(error);
[email protected]dd5161a2011-09-14 17:40:17104 return;
105 }
106
107 // Manifest, number of users, average rating and rating count are required.
108 std::string manifest;
109 if (!webstore_data->GetString(kManifestKey, &manifest) ||
110 !webstore_data->GetString(kUsersKey, &localized_user_count_) ||
[email protected]5fdbd6f2011-09-01 17:33:04111 !webstore_data->GetDouble(kAverageRatingKey, &average_rating_) ||
112 !webstore_data->GetInteger(kRatingCountKey, &rating_count_)) {
113 CompleteInstall(kInvalidWebstoreResponseError);
114 return;
115 }
116
[email protected]dcde34b32013-07-31 02:28:45117 // Optional.
118 show_user_count_ = true;
119 webstore_data->GetBoolean(kShowUserCountKey, &show_user_count_);
120
[email protected]c82da8c42012-06-08 19:49:11121 if (average_rating_ < ExtensionInstallPrompt::kMinExtensionRating ||
122 average_rating_ > ExtensionInstallPrompt::kMaxExtensionRating) {
[email protected]5fdbd6f2011-09-01 17:33:04123 CompleteInstall(kInvalidWebstoreResponseError);
124 return;
125 }
126
127 // Localized name and description are optional.
128 if ((webstore_data->HasKey(kLocalizedNameKey) &&
129 !webstore_data->GetString(kLocalizedNameKey, &localized_name_)) ||
130 (webstore_data->HasKey(kLocalizedDescriptionKey) &&
131 !webstore_data->GetString(
132 kLocalizedDescriptionKey, &localized_description_))) {
[email protected]8915f342011-08-29 22:14:37133 CompleteInstall(kInvalidWebstoreResponseError);
134 return;
135 }
136
137 // Icon URL is optional.
138 GURL icon_url;
139 if (webstore_data->HasKey(kIconUrlKey)) {
140 std::string icon_url_string;
141 if (!webstore_data->GetString(kIconUrlKey, &icon_url_string)) {
142 CompleteInstall(kInvalidWebstoreResponseError);
143 return;
144 }
145 icon_url = GURL(extension_urls::GetWebstoreLaunchURL()).Resolve(
146 icon_url_string);
147 if (!icon_url.is_valid()) {
148 CompleteInstall(kInvalidWebstoreResponseError);
149 return;
150 }
151 }
152
[email protected]d44ec7b2013-03-15 04:34:34153 // Assume ownership of webstore_data.
154 webstore_data_.reset(webstore_data);
[email protected]a221ef092011-09-07 01:34:10155
[email protected]007b3f82013-04-09 08:46:45156 scoped_refptr<WebstoreInstallHelper> helper =
157 new WebstoreInstallHelper(this,
158 id_,
159 manifest,
160 std::string(), // We don't have any icon data.
161 icon_url,
162 profile_->GetRequestContext());
[email protected]8915f342011-08-29 22:14:37163 // The helper will call us back via OnWebstoreParseSucces or
164 // OnWebstoreParseFailure.
165 helper->Start();
166}
167
[email protected]734bcec2012-10-08 20:29:05168void WebstoreStandaloneInstaller::OnWebstoreResponseParseFailure(
[email protected]8915f342011-08-29 22:14:37169 const std::string& error) {
170 CompleteInstall(error);
171}
172
[email protected]734bcec2012-10-08 20:29:05173void WebstoreStandaloneInstaller::OnWebstoreParseSuccess(
[email protected]98e4e522011-10-25 13:00:16174 const std::string& id,
[email protected]8915f342011-08-29 22:14:37175 const SkBitmap& icon,
176 base::DictionaryValue* manifest) {
[email protected]d44ec7b2013-03-15 04:34:34177 CHECK_EQ(id_, id);
178
179 if (!CheckRequestorAlive()) {
[email protected]007b3f82013-04-09 08:46:45180 CompleteInstall(std::string());
[email protected]2fd920fb2011-09-08 23:33:00181 return;
182 }
183
[email protected]8915f342011-08-29 22:14:37184 manifest_.reset(manifest);
185 icon_ = icon;
186
[email protected]d44ec7b2013-03-15 04:34:34187 install_prompt_ = CreateInstallPrompt();
188 if (install_prompt_) {
[email protected]f8b23b42013-06-27 20:12:14189 ShowInstallUI();
[email protected]d44ec7b2013-03-15 04:34:34190 // Control flow finishes up in InstallUIProceed or InstallUIAbort.
191 } else {
[email protected]77e9261d2013-02-04 22:01:47192 InstallUIProceed();
[email protected]77e9261d2013-02-04 22:01:47193 }
[email protected]8915f342011-08-29 22:14:37194}
195
[email protected]734bcec2012-10-08 20:29:05196void WebstoreStandaloneInstaller::OnWebstoreParseFailure(
[email protected]98e4e522011-10-25 13:00:16197 const std::string& id,
[email protected]8915f342011-08-29 22:14:37198 InstallHelperResultCode result_code,
199 const std::string& error_message) {
200 CompleteInstall(error_message);
201}
202
[email protected]734bcec2012-10-08 20:29:05203void WebstoreStandaloneInstaller::InstallUIProceed() {
[email protected]d44ec7b2013-03-15 04:34:34204 if (!CheckRequestorAlive()) {
[email protected]007b3f82013-04-09 08:46:45205 CompleteInstall(std::string());
[email protected]2fd920fb2011-09-08 23:33:00206 return;
207 }
208
[email protected]21a5ad62012-04-03 04:48:45209 scoped_ptr<WebstoreInstaller::Approval> approval(
[email protected]89019d62012-05-17 18:47:09210 WebstoreInstaller::Approval::CreateWithNoInstallPrompt(
[email protected]c1b2d042013-02-23 00:31:04211 profile_,
[email protected]89019d62012-05-17 18:47:09212 id_,
[email protected]85290822013-08-23 20:27:38213 scoped_ptr<base::DictionaryValue>(manifest_.get()->DeepCopy()),
214 true));
[email protected]d44ec7b2013-03-15 04:34:34215 approval->skip_post_install_ui = !ShouldShowPostInstallUI();
216 approval->use_app_installed_bubble = ShouldShowAppInstalledBubble();
[email protected]28e910e2013-08-23 04:18:33217 approval->installing_icon = gfx::ImageSkia::CreateFrom1xBitmap(icon_);
[email protected]21a5ad62012-04-03 04:48:45218
[email protected]98e4e522011-10-25 13:00:16219 scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller(
[email protected]d44ec7b2013-03-15 04:34:34220 profile_,
221 this,
222 &(GetWebContents()->GetController()),
223 id_,
224 approval.Pass(),
[email protected]084e35482013-09-25 02:46:19225 install_source_);
[email protected]98e4e522011-10-25 13:00:16226 installer->Start();
[email protected]8915f342011-08-29 22:14:37227}
228
[email protected]734bcec2012-10-08 20:29:05229void WebstoreStandaloneInstaller::InstallUIAbort(bool user_initiated) {
[email protected]8915f342011-08-29 22:14:37230 CompleteInstall(kUserCancelledError);
231}
232
[email protected]734bcec2012-10-08 20:29:05233void WebstoreStandaloneInstaller::OnExtensionInstallSuccess(
234 const std::string& id) {
[email protected]cb08ba22011-10-19 21:41:40235 CHECK_EQ(id_, id);
[email protected]007b3f82013-04-09 08:46:45236 CompleteInstall(std::string());
[email protected]cb08ba22011-10-19 21:41:40237}
238
[email protected]734bcec2012-10-08 20:29:05239void WebstoreStandaloneInstaller::OnExtensionInstallFailure(
[email protected]bcd1eaf72012-10-03 05:42:29240 const std::string& id,
241 const std::string& error,
242 WebstoreInstaller::FailureReason cancelled) {
[email protected]cb08ba22011-10-19 21:41:40243 CHECK_EQ(id_, id);
244 CompleteInstall(error);
245}
246
[email protected]d44ec7b2013-03-15 04:34:34247void WebstoreStandaloneInstaller::AbortInstall() {
248 callback_.Reset();
249 // Abort any in-progress fetches.
[email protected]3eeddd892013-04-17 17:00:11250 if (webstore_data_fetcher_) {
[email protected]d44ec7b2013-03-15 04:34:34251 webstore_data_fetcher_.reset();
252 Release(); // Matches the AddRef in BeginInstall.
253 }
254}
255
[email protected]734bcec2012-10-08 20:29:05256void WebstoreStandaloneInstaller::CompleteInstall(const std::string& error) {
[email protected]0688d8dc2013-02-16 04:10:38257 // Clear webstore_data_fetcher_ so that WebContentsDestroyed will no longer
258 // call Release in case the WebContents is destroyed before this object.
259 scoped_ptr<WebstoreDataFetcher> webstore_data_fetcher(
260 webstore_data_fetcher_.Pass());
[email protected]d2a639e2012-09-17 07:41:21261 if (!callback_.is_null())
262 callback_.Run(error.empty(), error);
[email protected]2fd920fb2011-09-08 23:33:00263
[email protected]5db2e882012-12-20 10:17:26264 Release(); // Matches the AddRef in BeginInstall.
[email protected]8915f342011-08-29 22:14:37265}
[email protected]5f2a4752012-04-27 22:18:58266
[email protected]d44ec7b2013-03-15 04:34:34267void
[email protected]f8b23b42013-06-27 20:12:14268WebstoreStandaloneInstaller::ShowInstallUI() {
[email protected]d44ec7b2013-03-15 04:34:34269 std::string error;
270 localized_extension_for_display_ =
271 ExtensionInstallPrompt::GetLocalizedExtensionForDisplay(
272 manifest_.get(),
273 Extension::REQUIRE_KEY | Extension::FROM_WEBSTORE,
274 id_,
275 localized_name_,
276 localized_description_,
277 &error);
[email protected]dc24976f2013-06-02 21:15:09278 if (!localized_extension_for_display_.get()) {
[email protected]d44ec7b2013-03-15 04:34:34279 CompleteInstall(kInvalidManifestError);
280 return;
[email protected]5f2a4752012-04-27 22:18:58281 }
[email protected]5f2a4752012-04-27 22:18:58282
[email protected]f8b23b42013-06-27 20:12:14283 install_ui_ = CreateInstallUI();
[email protected]dc24976f2013-06-02 21:15:09284 install_ui_->ConfirmStandaloneInstall(
285 this, localized_extension_for_display_.get(), &icon_, *install_prompt_);
[email protected]5f2a4752012-04-27 22:18:58286}
[email protected]3d61a7f2012-07-12 19:11:25287
288} // namespace extensions