[email protected] | 609dc3f | 2012-01-10 00:04:05 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [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 | |
| 5 | #include "chrome/browser/extensions/webstore_installer.h" |
| 6 | |
[email protected] | 1dba4b9 | 2012-01-26 02:59:27 | [diff] [blame] | 7 | #include "base/basictypes.h" |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 8 | #include "base/bind.h" |
| 9 | #include "base/command_line.h" |
| 10 | #include "base/file_util.h" |
[email protected] | 1dba4b9 | 2012-01-26 02:59:27 | [diff] [blame] | 11 | #include "base/rand_util.h" |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 12 | #include "base/stringprintf.h" |
[email protected] | 99dfecd | 2011-10-18 01:11:50 | [diff] [blame] | 13 | #include "base/string_util.h" |
[email protected] | 1dba4b9 | 2012-01-26 02:59:27 | [diff] [blame] | 14 | #include "base/string_number_conversions.h" |
[email protected] | fc67082 | 2011-12-17 09:33:49 | [diff] [blame] | 15 | #include "base/utf_string_conversions.h" |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 16 | #include "chrome/browser/browser_process.h" |
[email protected] | 609dc3f | 2012-01-10 00:04:05 | [diff] [blame] | 17 | #include "chrome/browser/download/download_prefs.h" |
[email protected] | 14365e3 | 2012-01-24 01:12:22 | [diff] [blame] | 18 | #include "chrome/browser/download/download_util.h" |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 19 | #include "chrome/browser/extensions/crx_installer.h" |
| 20 | #include "chrome/browser/profiles/profile.h" |
| 21 | #include "chrome/browser/tabs/tab_strip_model.h" |
| 22 | #include "chrome/browser/ui/browser_list.h" |
| 23 | #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 24 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 25 | #include "chrome/common/chrome_switches.h" |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 26 | #include "chrome/common/extensions/extension.h" |
[email protected] | 99dfecd | 2011-10-18 01:11:50 | [diff] [blame] | 27 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 28 | #include "content/browser/download/download_types.h" |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 29 | #include "content/public/browser/browser_thread.h" |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 30 | #include "content/public/browser/download_manager.h" |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 31 | #include "content/public/browser/navigation_controller.h" |
[email protected] | ad23a09 | 2011-12-28 07:02:04 | [diff] [blame] | 32 | #include "content/public/browser/navigation_entry.h" |
[email protected] | 86ab86b | 2011-10-19 03:07:55 | [diff] [blame] | 33 | #include "content/public/browser/notification_details.h" |
| 34 | #include "content/public/browser/notification_source.h" |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 35 | #include "googleurl/src/gurl.h" |
[email protected] | 99dfecd | 2011-10-18 01:11:50 | [diff] [blame] | 36 | #include "net/base/escape.h" |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 37 | |
[email protected] | 545ab7a | 2011-11-03 14:54:21 | [diff] [blame] | 38 | using content::BrowserThread; |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 39 | using content::NavigationController; |
[email protected] | 545ab7a | 2011-11-03 14:54:21 | [diff] [blame] | 40 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 41 | namespace { |
| 42 | |
| 43 | const char kInvalidIdError[] = "Invalid id"; |
| 44 | const char kNoBrowserError[] = "No browser found"; |
[email protected] | 9c265d0 | 2011-12-29 22:13:43 | [diff] [blame] | 45 | const char kDownloadDirectoryError[] = "Could not create download directory"; |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 46 | |
[email protected] | 99dfecd | 2011-10-18 01:11:50 | [diff] [blame] | 47 | const char kInlineInstallSource[] = "inline"; |
| 48 | const char kDefaultInstallSource[] = ""; |
| 49 | |
[email protected] | 9c265d0 | 2011-12-29 22:13:43 | [diff] [blame] | 50 | FilePath* g_download_directory_for_tests = NULL; |
| 51 | |
[email protected] | e577c59 | 2011-10-25 22:53:30 | [diff] [blame] | 52 | GURL GetWebstoreInstallURL( |
[email protected] | 99dfecd | 2011-10-18 01:11:50 | [diff] [blame] | 53 | const std::string& extension_id, const std::string& install_source) { |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 54 | CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 55 | if (cmd_line->HasSwitch(switches::kAppsGalleryDownloadURL)) { |
| 56 | std::string download_url = |
| 57 | cmd_line->GetSwitchValueASCII(switches::kAppsGalleryDownloadURL); |
| 58 | return GURL(base::StringPrintf(download_url.c_str(), |
| 59 | extension_id.c_str())); |
| 60 | } |
[email protected] | 99dfecd | 2011-10-18 01:11:50 | [diff] [blame] | 61 | std::vector<std::string> params; |
| 62 | params.push_back("id=" + extension_id); |
| 63 | if (!install_source.empty()) { |
| 64 | params.push_back("installsource=" + install_source); |
| 65 | } |
| 66 | params.push_back("lang=" + g_browser_process->GetApplicationLocale()); |
| 67 | params.push_back("uc"); |
| 68 | std::string url_string = extension_urls::GetWebstoreUpdateUrl(true).spec(); |
| 69 | |
| 70 | GURL url(url_string + "?response=redirect&x=" + |
| 71 | net::EscapeQueryParamValue(JoinString(params, '&'), true)); |
| 72 | DCHECK(url.is_valid()); |
| 73 | |
| 74 | return url; |
| 75 | } |
| 76 | |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 77 | // Must be executed on the FILE thread. |
[email protected] | 4d43111a | 2012-01-13 03:04:59 | [diff] [blame] | 78 | void GetDownloadFilePath(const FilePath& download_directory, |
[email protected] | 609dc3f | 2012-01-10 00:04:05 | [diff] [blame] | 79 | const std::string& id, |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 80 | const base::Callback<void(FilePath)>& callback) { |
[email protected] | 4d43111a | 2012-01-13 03:04:59 | [diff] [blame] | 81 | const FilePath& directory(g_download_directory_for_tests ? |
| 82 | *g_download_directory_for_tests : download_directory); |
[email protected] | 9c265d0 | 2011-12-29 22:13:43 | [diff] [blame] | 83 | |
| 84 | // Ensure the download directory exists. TODO(asargent) - make this use |
| 85 | // common code from the downloads system. |
| 86 | if (!file_util::DirectoryExists(directory)) { |
| 87 | if (!file_util::CreateDirectory(directory)) { |
| 88 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 89 | base::Bind(callback, FilePath())); |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | |
[email protected] | 1dba4b9 | 2012-01-26 02:59:27 | [diff] [blame] | 94 | // This is to help avoid a race condition between when we generate this |
| 95 | // filename and when the download starts writing to it (think concurrently |
| 96 | // running sharded browser tests installing the same test file, for |
| 97 | // instance). |
| 98 | std::string random_number = |
| 99 | base::Uint64ToString(base::RandGenerator(kuint16max)); |
| 100 | |
| 101 | FilePath file = directory.AppendASCII(id + "_" + random_number + ".crx"); |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 102 | |
[email protected] | e285afa | 2012-01-31 23:16:39 | [diff] [blame] | 103 | int uniquifier = file_util::GetUniquePathNumber(file, FILE_PATH_LITERAL("")); |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 104 | if (uniquifier > 0) |
[email protected] | e285afa | 2012-01-31 23:16:39 | [diff] [blame] | 105 | file = file.InsertBeforeExtensionASCII(StringPrintf(" (%d)", uniquifier)); |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 106 | |
| 107 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 108 | base::Bind(callback, file)); |
| 109 | } |
| 110 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 111 | } // namespace |
| 112 | |
| 113 | |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 114 | WebstoreInstaller::WebstoreInstaller(Profile* profile, |
| 115 | Delegate* delegate, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 116 | NavigationController* controller, |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 117 | const std::string& id, |
| 118 | int flags) |
| 119 | : profile_(profile), |
| 120 | delegate_(delegate), |
| 121 | controller_(controller), |
| 122 | id_(id), |
| 123 | flags_(flags) { |
[email protected] | e577c59 | 2011-10-25 22:53:30 | [diff] [blame] | 124 | download_url_ = GetWebstoreInstallURL(id, flags & FLAG_INLINE_INSTALL ? |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 125 | kInlineInstallSource : kDefaultInstallSource); |
| 126 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 127 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
[email protected] | 86ab86b | 2011-10-19 03:07:55 | [diff] [blame] | 128 | content::Source<Profile>(profile)); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 129 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
[email protected] | 86ab86b | 2011-10-19 03:07:55 | [diff] [blame] | 130 | content::Source<CrxInstaller>(NULL)); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | WebstoreInstaller::~WebstoreInstaller() {} |
| 134 | |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 135 | void WebstoreInstaller::Start() { |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 136 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 137 | AddRef(); // Balanced in ReportSuccess and ReportFailure. |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 138 | |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 139 | if (!Extension::IdIsValid(id_)) { |
| 140 | ReportFailure(kInvalidIdError); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
[email protected] | 609dc3f | 2012-01-10 00:04:05 | [diff] [blame] | 144 | FilePath download_path = DownloadPrefs::FromDownloadManager( |
| 145 | profile_->GetDownloadManager())->download_path(); |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 146 | BrowserThread::PostTask( |
| 147 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 609dc3f | 2012-01-10 00:04:05 | [diff] [blame] | 148 | base::Bind(&GetDownloadFilePath, download_path, id_, |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 149 | base::Bind(&WebstoreInstaller::StartDownload, |
| 150 | base::Unretained(this)))); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 151 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void WebstoreInstaller::Observe(int type, |
[email protected] | 86ab86b | 2011-10-19 03:07:55 | [diff] [blame] | 155 | const content::NotificationSource& source, |
| 156 | const content::NotificationDetails& details) { |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 157 | switch (type) { |
| 158 | case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
[email protected] | 86ab86b | 2011-10-19 03:07:55 | [diff] [blame] | 159 | CHECK(profile_->IsSameProfile(content::Source<Profile>(source).ptr())); |
| 160 | const Extension* extension = |
| 161 | content::Details<const Extension>(details).ptr(); |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 162 | if (id_ == extension->id()) |
| 163 | ReportSuccess(); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | |
| 167 | case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: { |
[email protected] | 86ab86b | 2011-10-19 03:07:55 | [diff] [blame] | 168 | CrxInstaller* crx_installer = content::Source<CrxInstaller>(source).ptr(); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 169 | CHECK(crx_installer); |
| 170 | if (!profile_->IsSameProfile(crx_installer->profile())) |
| 171 | return; |
| 172 | |
[email protected] | fc67082 | 2011-12-17 09:33:49 | [diff] [blame] | 173 | // TODO(rdevlin.cronin): Continue removing std::string errors and |
| 174 | // replacing with string16 |
| 175 | const string16* error = content::Details<const string16>(details).ptr(); |
| 176 | const std::string utf8_error = UTF16ToUTF8(*error); |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 177 | if (download_url_ == crx_installer->original_download_url()) |
[email protected] | fc67082 | 2011-12-17 09:33:49 | [diff] [blame] | 178 | ReportFailure(utf8_error); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 179 | break; |
| 180 | } |
| 181 | |
| 182 | default: |
| 183 | NOTREACHED(); |
| 184 | } |
| 185 | } |
| 186 | |
[email protected] | 9c265d0 | 2011-12-29 22:13:43 | [diff] [blame] | 187 | void WebstoreInstaller::SetDownloadDirectoryForTests(FilePath* directory) { |
| 188 | g_download_directory_for_tests = directory; |
| 189 | } |
| 190 | |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 191 | void WebstoreInstaller::StartDownload(FilePath file) { |
| 192 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 193 | |
[email protected] | 9c265d0 | 2011-12-29 22:13:43 | [diff] [blame] | 194 | if (file.empty()) { |
| 195 | ReportFailure(kDownloadDirectoryError); |
| 196 | return; |
| 197 | } |
| 198 | |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 199 | // TODO(mihaip): For inline installs, we pretend like the referrer is the |
| 200 | // gallery, even though this could be an inline install, in order to pass the |
| 201 | // checks in ExtensionService::IsDownloadFromGallery. We should instead pass |
| 202 | // the real referrer, track if this is an inline install in the whitelist |
| 203 | // entry and look that up when checking that this is a valid download. |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 204 | GURL referrer = controller_->GetActiveEntry()->GetURL(); |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 205 | if (flags_ & FLAG_INLINE_INSTALL) |
| 206 | referrer = GURL(extension_urls::GetWebstoreItemDetailURLPrefix() + id_); |
| 207 | |
| 208 | DownloadSaveInfo save_info; |
| 209 | save_info.file_path = file; |
| 210 | |
| 211 | // The download url for the given extension is contained in |download_url_|. |
| 212 | // We will navigate the current tab to this url to start the download. The |
| 213 | // download system will then pass the crx to the CrxInstaller. |
[email protected] | 731809e | 2012-02-15 21:56:48 | [diff] [blame] | 214 | download_util::RecordDownloadSource( |
| 215 | download_util::INITIATED_BY_WEBSTORE_INSTALLER); |
[email protected] | 0d4e30c | 2012-01-28 00:47:53 | [diff] [blame] | 216 | profile_->GetDownloadManager()->DownloadUrl( |
| 217 | download_url_, referrer, "", |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 218 | false, -1, save_info, controller_->GetWebContents()); |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 219 | } |
| 220 | |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 221 | void WebstoreInstaller::ReportFailure(const std::string& error) { |
| 222 | if (delegate_) |
| 223 | delegate_->OnExtensionInstallFailure(id_, error); |
| 224 | |
| 225 | Release(); // Balanced in Start(). |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 226 | } |
| 227 | |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 228 | void WebstoreInstaller::ReportSuccess() { |
| 229 | if (delegate_) |
| 230 | delegate_->OnExtensionInstallSuccess(id_); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 231 | |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 232 | Release(); // Balanced in Start(). |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 233 | } |