1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/extensions/install_limiter.h"
#include <string>
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/chromeos/extensions/install_limiter_factory.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "extensions/browser/notification_types.h"
using content::BrowserThread;
namespace {
int64 GetFileSizeOnBlockingPool(const base::FilePath& file) {
DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
// Get file size. In case of error, sets 0 as file size to let the installer
// run and fail.
int64 size;
return base::GetFileSize(file, &size) ? size : 0;
}
} // namespace
namespace extensions {
////////////////////////////////////////////////////////////////////////////////
// InstallLimiter::DeferredInstall
InstallLimiter::DeferredInstall::DeferredInstall(
const scoped_refptr<CrxInstaller>& installer,
const base::FilePath& path)
: installer(installer),
path(path) {
}
InstallLimiter::DeferredInstall::~DeferredInstall() {
}
////////////////////////////////////////////////////////////////////////////////
// InstallLimiter
InstallLimiter* InstallLimiter::Get(Profile* profile) {
return InstallLimiterFactory::GetForProfile(profile);
}
InstallLimiter::InstallLimiter() : disabled_for_test_(false) {
}
InstallLimiter::~InstallLimiter() {
}
void InstallLimiter::DisableForTest() {
disabled_for_test_ = true;
}
void InstallLimiter::Add(const scoped_refptr<CrxInstaller>& installer,
const base::FilePath& path) {
// No deferred installs when disabled for test.
if (disabled_for_test_) {
installer->InstallCrx(path);
return;
}
base::PostTaskAndReplyWithResult(
BrowserThread::GetBlockingPool(),
FROM_HERE,
base::Bind(&GetFileSizeOnBlockingPool, path),
base::Bind(&InstallLimiter::AddWithSize, AsWeakPtr(), installer, path));
}
void InstallLimiter::AddWithSize(
const scoped_refptr<CrxInstaller>& installer,
const base::FilePath& path,
int64 size) {
const int64 kBigAppSizeThreshold = 1048576; // 1MB
if (size <= kBigAppSizeThreshold) {
RunInstall(installer, path);
// Stop wait timer and let install notification drive deferred installs.
wait_timer_.Stop();
return;
}
deferred_installs_.push(DeferredInstall(installer, path));
// When there are no running installs, wait a bit before running deferred
// installs to allow small app install to take precedence, especially when a
// big app is the first one in the list.
if (running_installers_.empty() && !wait_timer_.IsRunning()) {
const int kMaxWaitTimeInMs = 5000; // 5 seconds.
wait_timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kMaxWaitTimeInMs),
this, &InstallLimiter::CheckAndRunDeferrredInstalls);
}
}
void InstallLimiter::CheckAndRunDeferrredInstalls() {
if (deferred_installs_.empty() || !running_installers_.empty())
return;
const DeferredInstall& deferred = deferred_installs_.front();
RunInstall(deferred.installer, deferred.path);
deferred_installs_.pop();
}
void InstallLimiter::RunInstall(const scoped_refptr<CrxInstaller>& installer,
const base::FilePath& path) {
registrar_.Add(this,
extensions::NOTIFICATION_CRX_INSTALLER_DONE,
content::Source<CrxInstaller>(installer.get()));
installer->InstallCrx(path);
running_installers_.insert(installer);
}
void InstallLimiter::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(extensions::NOTIFICATION_CRX_INSTALLER_DONE, type);
registrar_.Remove(this, extensions::NOTIFICATION_CRX_INSTALLER_DONE, source);
const scoped_refptr<CrxInstaller> installer =
content::Source<extensions::CrxInstaller>(source).ptr();
running_installers_.erase(installer);
CheckAndRunDeferrredInstalls();
}
} // namespace extensions
|