This source file includes following definitions.
- GetDownloadFilePath
- UseSeparateWebstoreDownloadDirectory
- GetWebstoreInstallURL
- OnExtensionDownloadStarted
- OnExtensionDownloadProgress
- is_ephemeral
- CreateWithInstallPrompt
- CreateForSharedModule
- CreateWithNoInstallPrompt
- GetAssociatedApproval
- download_started_
- Start
- Observe
- InvalidateDelegate
- SetDownloadDirectoryForTests
- OnDownloadStarted
- OnDownloadUpdated
- OnDownloadDestroyed
- DownloadNextPendingModule
- DownloadCrx
- StartDownload
- UpdateDownloadProgress
- ReportFailure
- ReportSuccess
- RecordInterrupt
#include "chrome/browser/extensions/webstore_installer.h"
#include <vector>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/metrics/sparse_histogram.h"
#include "base/path_service.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/download/download_crx_util.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/download/download_stats.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "chrome/browser/extensions/install_tracker.h"
#include "chrome/browser/extensions/install_tracker_factory.h"
#include "chrome/browser/extensions/install_verifier.h"
#include "chrome/browser/extensions/shared_module_service.h"
#include "chrome/browser/omaha_query_params/omaha_query_params.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_constants.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/download_save_info.h"
#include "content/public/browser/download_url_parameters.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/shared_module_info.h"
#include "net/base/escape.h"
#include "url/gurl.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/drive/file_system_util.h"
#endif
using chrome::OmahaQueryParams;
using content::BrowserContext;
using content::BrowserThread;
using content::DownloadItem;
using content::DownloadManager;
using content::NavigationController;
using content::DownloadUrlParameters;
namespace {
const char kApprovalKey[] = "extensions.webstore_installer";
const char kInvalidIdError[] = "Invalid id";
const char kDownloadDirectoryError[] = "Could not create download directory";
const char kDownloadCanceledError[] = "Download canceled";
const char kInstallCanceledError[] = "Install canceled";
const char kDownloadInterruptedError[] = "Download interrupted";
const char kInvalidDownloadError[] =
"Download was not a valid extension or user script";
const char kDependencyNotFoundError[] = "Dependency not found";
const char kDependencyNotSharedModuleError[] =
"Dependency is not shared module";
const char kInlineInstallSource[] = "inline";
const char kDefaultInstallSource[] = "ondemand";
const char kAppLauncherInstallSource[] = "applauncher";
const size_t kTimeRemainingMinutesThreshold = 1u;
const base::FilePath::CharType kWebstoreDownloadFolder[] =
FILE_PATH_LITERAL("Webstore Downloads");
base::FilePath* g_download_directory_for_tests = NULL;
void GetDownloadFilePath(
const base::FilePath& download_directory,
const std::string& id,
const base::Callback<void(const base::FilePath&)>& callback) {
if (!base::DirectoryExists(download_directory)) {
if (!base::CreateDirectory(download_directory)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(callback, base::FilePath()));
return;
}
}
std::string random_number =
base::Uint64ToString(base::RandGenerator(kuint16max));
base::FilePath file =
download_directory.AppendASCII(id + "_" + random_number + ".crx");
int uniquifier =
base::GetUniquePathNumber(file, base::FilePath::StringType());
if (uniquifier > 0) {
file = file.InsertBeforeExtensionASCII(
base::StringPrintf(" (%d)", uniquifier));
}
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(callback, file));
}
bool UseSeparateWebstoreDownloadDirectory() {
const char kFieldTrial[] = "WebstoreDownloadDirectory";
const char kSeparateDirectoryUnderUDD[] = "SeparateDirectoryUnderUDD";
std::string field_trial_group =
base::FieldTrialList::FindFullName(kFieldTrial);
return field_trial_group == kSeparateDirectoryUnderUDD;
}
}
namespace extensions {
GURL WebstoreInstaller::GetWebstoreInstallURL(
const std::string& extension_id,
InstallSource source) {
std::string install_source;
switch (source) {
case INSTALL_SOURCE_INLINE:
install_source = kInlineInstallSource;
break;
case INSTALL_SOURCE_APP_LAUNCHER:
install_source = kAppLauncherInstallSource;
break;
case INSTALL_SOURCE_OTHER:
install_source = kDefaultInstallSource;
}
CommandLine* cmd_line = CommandLine::ForCurrentProcess();
if (cmd_line->HasSwitch(switches::kAppsGalleryDownloadURL)) {
std::string download_url =
cmd_line->GetSwitchValueASCII(switches::kAppsGalleryDownloadURL);
return GURL(base::StringPrintf(download_url.c_str(),
extension_id.c_str()));
}
std::vector<std::string> params;
params.push_back("id=" + extension_id);
if (!install_source.empty())
params.push_back("installsource=" + install_source);
params.push_back("uc");
std::string url_string = extension_urls::GetWebstoreUpdateUrl().spec();
GURL url(url_string + "?response=redirect&" +
OmahaQueryParams::Get(OmahaQueryParams::CRX) + "&x=" +
net::EscapeQueryParamValue(JoinString(params, '&'), true));
DCHECK(url.is_valid());
return url;
}
void WebstoreInstaller::Delegate::OnExtensionDownloadStarted(
const std::string& id,
content::DownloadItem* item) {
}
void WebstoreInstaller::Delegate::OnExtensionDownloadProgress(
const std::string& id,
content::DownloadItem* item) {
}
WebstoreInstaller::Approval::Approval()
: profile(NULL),
use_app_installed_bubble(false),
skip_post_install_ui(false),
skip_install_dialog(false),
enable_launcher(false),
manifest_check_level(MANIFEST_CHECK_LEVEL_STRICT),
is_ephemeral(false) {
}
scoped_ptr<WebstoreInstaller::Approval>
WebstoreInstaller::Approval::CreateWithInstallPrompt(Profile* profile) {
scoped_ptr<Approval> result(new Approval());
result->profile = profile;
return result.Pass();
}
scoped_ptr<WebstoreInstaller::Approval>
WebstoreInstaller::Approval::CreateForSharedModule(Profile* profile) {
scoped_ptr<Approval> result(new Approval());
result->profile = profile;
result->skip_install_dialog = true;
result->manifest_check_level = MANIFEST_CHECK_LEVEL_NONE;
return result.Pass();
}
scoped_ptr<WebstoreInstaller::Approval>
WebstoreInstaller::Approval::CreateWithNoInstallPrompt(
Profile* profile,
const std::string& extension_id,
scoped_ptr<base::DictionaryValue> parsed_manifest,
bool strict_manifest_check) {
scoped_ptr<Approval> result(new Approval());
result->extension_id = extension_id;
result->profile = profile;
result->manifest = scoped_ptr<Manifest>(
new Manifest(Manifest::INVALID_LOCATION,
scoped_ptr<base::DictionaryValue>(
parsed_manifest->DeepCopy())));
result->skip_install_dialog = true;
result->manifest_check_level = strict_manifest_check ?
MANIFEST_CHECK_LEVEL_STRICT : MANIFEST_CHECK_LEVEL_LOOSE;
return result.Pass();
}
WebstoreInstaller::Approval::~Approval() {}
const WebstoreInstaller::Approval* WebstoreInstaller::GetAssociatedApproval(
const DownloadItem& download) {
return static_cast<const Approval*>(download.GetUserData(kApprovalKey));
}
WebstoreInstaller::WebstoreInstaller(Profile* profile,
Delegate* delegate,
content::WebContents* web_contents,
const std::string& id,
scoped_ptr<Approval> approval,
InstallSource source)
: content::WebContentsObserver(web_contents),
profile_(profile),
delegate_(delegate),
id_(id),
install_source_(source),
download_item_(NULL),
approval_(approval.release()),
total_modules_(0),
download_started_(false) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(web_contents);
registrar_.Add(this, chrome::NOTIFICATION_CRX_INSTALLER_DONE,
content::NotificationService::AllSources());
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
content::Source<Profile>(profile->GetOriginalProfile()));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR,
content::Source<CrxInstaller>(NULL));
}
void WebstoreInstaller::Start() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
AddRef();
if (!Extension::IdIsValid(id_)) {
ReportFailure(kInvalidIdError, FAILURE_REASON_OTHER);
return;
}
ExtensionService* extension_service =
ExtensionSystem::Get(profile_)->extension_service();
if (approval_.get() && approval_->dummy_extension) {
SharedModuleService::ImportStatus status =
extension_service->shared_module_service()->CheckImports(
approval_->dummy_extension,
&pending_modules_,
&pending_modules_);
if (status == SharedModuleService::IMPORT_STATUS_UNRECOVERABLE) {
ReportFailure(kDependencyNotSharedModuleError,
FAILURE_REASON_DEPENDENCY_NOT_SHARED_MODULE);
return;
}
}
SharedModuleInfo::ImportInfo info;
info.extension_id = id_;
pending_modules_.push_back(info);
total_modules_ = pending_modules_.size();
std::set<std::string> ids;
std::list<SharedModuleInfo::ImportInfo>::const_iterator i;
for (i = pending_modules_.begin(); i != pending_modules_.end(); ++i) {
ids.insert(i->extension_id);
}
ExtensionSystem::Get(profile_)->install_verifier()->AddProvisional(ids);
std::string name;
if (!approval_->manifest->value()->GetString(manifest_keys::kName, &name)) {
NOTREACHED();
}
extensions::InstallTracker* tracker =
extensions::InstallTrackerFactory::GetForProfile(profile_);
extensions::InstallObserver::ExtensionInstallParams params(
id_,
name,
approval_->installing_icon,
approval_->manifest->is_app(),
approval_->manifest->is_platform_app());
params.is_ephemeral = approval_->is_ephemeral;
tracker->OnBeginExtensionInstall(params);
tracker->OnBeginExtensionDownload(id_);
DownloadNextPendingModule();
}
void WebstoreInstaller::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_CRX_INSTALLER_DONE: {
const Extension* extension =
content::Details<const Extension>(details).ptr();
CrxInstaller* installer = content::Source<CrxInstaller>(source).ptr();
if (extension == NULL && download_item_ != NULL &&
installer->download_url() == download_item_->GetURL() &&
installer->profile()->IsSameProfile(profile_)) {
ReportFailure(kInstallCanceledError, FAILURE_REASON_CANCELLED);
}
break;
}
case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
CHECK(profile_->IsSameProfile(content::Source<Profile>(source).ptr()));
const Extension* extension =
content::Details<const InstalledExtensionInfo>(details)->extension;
if (pending_modules_.empty())
return;
SharedModuleInfo::ImportInfo info = pending_modules_.front();
if (extension->id() != info.extension_id)
return;
pending_modules_.pop_front();
if (pending_modules_.empty()) {
CHECK_EQ(extension->id(), id_);
ReportSuccess();
} else {
const Version version_required(info.minimum_version);
if (version_required.IsValid() &&
extension->version()->CompareTo(version_required) < 0) {
ReportFailure(kDependencyNotFoundError,
FAILURE_REASON_DEPENDENCY_NOT_FOUND);
} else if (!SharedModuleInfo::IsSharedModule(extension)) {
ReportFailure(kDependencyNotSharedModuleError,
FAILURE_REASON_DEPENDENCY_NOT_SHARED_MODULE);
} else {
DownloadNextPendingModule();
}
}
break;
}
case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: {
CrxInstaller* crx_installer = content::Source<CrxInstaller>(source).ptr();
CHECK(crx_installer);
if (!profile_->IsSameProfile(crx_installer->profile()))
return;
const base::string16* error =
content::Details<const base::string16>(details).ptr();
const std::string utf8_error = base::UTF16ToUTF8(*error);
if (download_url_ == crx_installer->original_download_url())
ReportFailure(utf8_error, FAILURE_REASON_OTHER);
break;
}
default:
NOTREACHED();
}
}
void WebstoreInstaller::InvalidateDelegate() {
delegate_ = NULL;
}
void WebstoreInstaller::SetDownloadDirectoryForTests(
base::FilePath* directory) {
g_download_directory_for_tests = directory;
}
WebstoreInstaller::~WebstoreInstaller() {
if (download_item_) {
download_item_->RemoveObserver(this);
download_item_ = NULL;
}
}
void WebstoreInstaller::OnDownloadStarted(
DownloadItem* item,
content::DownloadInterruptReason interrupt_reason) {
if (!item) {
DCHECK_NE(content::DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason);
ReportFailure(content::DownloadInterruptReasonToString(interrupt_reason),
FAILURE_REASON_OTHER);
return;
}
DCHECK_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason);
DCHECK(!pending_modules_.empty());
download_item_ = item;
download_item_->AddObserver(this);
if (pending_modules_.size() > 1) {
scoped_ptr<Approval> approval = Approval::CreateForSharedModule(profile_);
const SharedModuleInfo::ImportInfo& info = pending_modules_.front();
approval->extension_id = info.extension_id;
const Version version_required(info.minimum_version);
if (version_required.IsValid()) {
approval->minimum_version.reset(
new Version(version_required));
}
download_item_->SetUserData(kApprovalKey, approval.release());
} else {
if (approval_)
download_item_->SetUserData(kApprovalKey, approval_.release());
}
if (!download_started_) {
if (delegate_)
delegate_->OnExtensionDownloadStarted(id_, download_item_);
download_started_ = true;
}
}
void WebstoreInstaller::OnDownloadUpdated(DownloadItem* download) {
CHECK_EQ(download_item_, download);
switch (download->GetState()) {
case DownloadItem::CANCELLED:
ReportFailure(kDownloadCanceledError, FAILURE_REASON_CANCELLED);
break;
case DownloadItem::INTERRUPTED:
RecordInterrupt(download);
ReportFailure(kDownloadInterruptedError, FAILURE_REASON_OTHER);
break;
case DownloadItem::COMPLETE:
if (!download_crx_util::IsExtensionDownload(*download)) {
ReportFailure(kInvalidDownloadError, FAILURE_REASON_OTHER);
} else if (pending_modules_.empty()) {
if (delegate_)
delegate_->OnExtensionDownloadProgress(id_, download);
extensions::InstallTracker* tracker =
extensions::InstallTrackerFactory::GetForProfile(profile_);
tracker->OnDownloadProgress(id_, 100);
}
download_progress_timer_.Stop();
break;
case DownloadItem::IN_PROGRESS: {
if (delegate_ && pending_modules_.size() == 1) {
delegate_->OnExtensionDownloadProgress(id_, download);
}
UpdateDownloadProgress();
break;
}
default:
break;
}
}
void WebstoreInstaller::OnDownloadDestroyed(DownloadItem* download) {
CHECK_EQ(download_item_, download);
download_item_->RemoveObserver(this);
download_item_ = NULL;
}
void WebstoreInstaller::DownloadNextPendingModule() {
CHECK(!pending_modules_.empty());
if (pending_modules_.size() == 1) {
DCHECK_EQ(id_, pending_modules_.front().extension_id);
DownloadCrx(id_, install_source_);
} else {
DownloadCrx(pending_modules_.front().extension_id, INSTALL_SOURCE_OTHER);
}
}
void WebstoreInstaller::DownloadCrx(
const std::string& extension_id,
InstallSource source) {
download_url_ = GetWebstoreInstallURL(extension_id, source);
base::FilePath download_path;
if (UseSeparateWebstoreDownloadDirectory()) {
base::FilePath user_data_dir;
PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
download_path = user_data_dir.Append(kWebstoreDownloadFolder);
} else {
download_path = DownloadPrefs::FromDownloadManager(
BrowserContext::GetDownloadManager(profile_))->DownloadPath();
}
base::FilePath download_directory(g_download_directory_for_tests ?
*g_download_directory_for_tests : download_path);
#if defined(OS_CHROMEOS)
if (drive::util::IsUnderDriveMountPoint(download_directory)) {
download_directory = DownloadPrefs::FromBrowserContext(
profile_)->GetDefaultDownloadDirectoryForProfile();
}
#endif
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(&GetDownloadFilePath, download_directory, id_,
base::Bind(&WebstoreInstaller::StartDownload, this)));
}
void WebstoreInstaller::StartDownload(const base::FilePath& file) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (file.empty()) {
ReportFailure(kDownloadDirectoryError, FAILURE_REASON_OTHER);
return;
}
DownloadManager* download_manager =
BrowserContext::GetDownloadManager(profile_);
if (!download_manager) {
ReportFailure(kDownloadDirectoryError, FAILURE_REASON_OTHER);
return;
}
content::WebContents* contents = web_contents();
if (!contents) {
ReportFailure(kDownloadDirectoryError, FAILURE_REASON_OTHER);
return;
}
if (!contents->GetRenderProcessHost()) {
ReportFailure(kDownloadDirectoryError, FAILURE_REASON_OTHER);
return;
}
if (!contents->GetRenderViewHost()) {
ReportFailure(kDownloadDirectoryError, FAILURE_REASON_OTHER);
return;
}
content::NavigationController& controller = contents->GetController();
if (!controller.GetBrowserContext()) {
ReportFailure(kDownloadDirectoryError, FAILURE_REASON_OTHER);
return;
}
if (!controller.GetBrowserContext()->GetResourceContext()) {
ReportFailure(kDownloadDirectoryError, FAILURE_REASON_OTHER);
return;
}
RecordDownloadSource(DOWNLOAD_INITIATED_BY_WEBSTORE_INSTALLER);
int render_process_host_id = contents->GetRenderProcessHost()->GetID();
int render_view_host_routing_id =
contents->GetRenderViewHost()->GetRoutingID();
content::ResourceContext* resource_context =
controller.GetBrowserContext()->GetResourceContext();
scoped_ptr<DownloadUrlParameters> params(new DownloadUrlParameters(
download_url_,
render_process_host_id,
render_view_host_routing_id ,
resource_context));
params->set_file_path(file);
if (controller.GetVisibleEntry())
params->set_referrer(
content::Referrer(controller.GetVisibleEntry()->GetURL(),
blink::WebReferrerPolicyDefault));
params->set_callback(base::Bind(&WebstoreInstaller::OnDownloadStarted, this));
download_manager->DownloadUrl(params.Pass());
}
void WebstoreInstaller::UpdateDownloadProgress() {
if (!download_item_ ||
download_item_->GetState() != DownloadItem::IN_PROGRESS) {
download_progress_timer_.Stop();
return;
}
int percent = download_item_->PercentComplete();
if (percent >= 0) {
int finished_modules = total_modules_ - pending_modules_.size();
percent = (percent + (finished_modules * 100)) / total_modules_;
extensions::InstallTracker* tracker =
extensions::InstallTrackerFactory::GetForProfile(profile_);
tracker->OnDownloadProgress(id_, percent);
}
base::TimeDelta time_remaining;
if (download_item_->TimeRemaining(&time_remaining) &&
time_remaining >
base::TimeDelta::FromSeconds(kTimeRemainingMinutesThreshold)) {
download_progress_timer_.Start(
FROM_HERE,
base::TimeDelta::FromSeconds(kTimeRemainingMinutesThreshold),
this,
&WebstoreInstaller::UpdateDownloadProgress);
} else {
download_progress_timer_.Stop();
}
}
void WebstoreInstaller::ReportFailure(const std::string& error,
FailureReason reason) {
if (delegate_) {
delegate_->OnExtensionInstallFailure(id_, error, reason);
delegate_ = NULL;
}
extensions::InstallTracker* tracker =
extensions::InstallTrackerFactory::GetForProfile(profile_);
tracker->OnInstallFailure(id_);
Release();
}
void WebstoreInstaller::ReportSuccess() {
if (delegate_) {
delegate_->OnExtensionInstallSuccess(id_);
delegate_ = NULL;
}
Release();
}
void WebstoreInstaller::RecordInterrupt(const DownloadItem* download) const {
UMA_HISTOGRAM_SPARSE_SLOWLY("Extensions.WebstoreDownload.InterruptReason",
download->GetLastReason());
const int kNumBuckets = 30;
const int64 kMaxSizeKb = 1 << kNumBuckets;
UMA_HISTOGRAM_CUSTOM_COUNTS(
"Extensions.WebstoreDownload.InterruptReceivedKBytes",
download->GetReceivedBytes() / 1024,
1,
kMaxSizeKb,
kNumBuckets);
int64 total_bytes = download->GetTotalBytes();
if (total_bytes >= 0) {
UMA_HISTOGRAM_CUSTOM_COUNTS(
"Extensions.WebstoreDownload.InterruptTotalKBytes",
total_bytes / 1024,
1,
kMaxSizeKb,
kNumBuckets);
}
UMA_HISTOGRAM_BOOLEAN(
"Extensions.WebstoreDownload.InterruptTotalSizeUnknown",
total_bytes <= 0);
}
}