Commit 78efe2e9 authored by tommycli@chromium.org's avatar tommycli@chromium.org

Componentize component_updater: Split content::ResourceThrottle from CUS.

BUG=371463

Review URL: https://codereview.chromium.org/421393002

Cr-Commit-Position: refs/heads/master@{#288367}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288367 0039d316-1c4b-4281-b951-d872f2087c98
parent a397ec0f
// Copyright 2014 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/component_updater/component_updater_resource_throttle.h"
#include "base/location.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/component_updater/component_updater_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_controller.h"
#include "content/public/browser/resource_throttle.h"
using content::BrowserThread;
namespace component_updater {
namespace {
///////////////////////////////////////////////////////////////////////////////
// In charge of blocking url requests until the |crx_id| component has been
// updated. This class is touched solely from the IO thread. The UI thread
// can post tasks to it via weak pointers. By default the request is blocked
// unless the CrxUpdateService calls Unblock().
// The lifetime is controlled by Chrome's resource loader so the component
// updater cannot touch objects from this class except via weak pointers.
class CUResourceThrottle : public content::ResourceThrottle,
public base::SupportsWeakPtr<CUResourceThrottle> {
public:
CUResourceThrottle();
virtual ~CUResourceThrottle();
// Overriden from ResourceThrottle.
virtual void WillStartRequest(bool* defer) OVERRIDE;
virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE;
virtual const char* GetNameForLogging() const OVERRIDE;
// Component updater calls this function via PostTask to unblock the request.
void Unblock();
typedef std::vector<base::WeakPtr<CUResourceThrottle> > WeakPtrVector;
private:
enum State { NEW, BLOCKED, UNBLOCKED };
State state_;
};
CUResourceThrottle::CUResourceThrottle() : state_(NEW) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
}
CUResourceThrottle::~CUResourceThrottle() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
}
void CUResourceThrottle::WillStartRequest(bool* defer) {
if (state_ != UNBLOCKED) {
state_ = BLOCKED;
*defer = true;
} else {
*defer = false;
}
}
void CUResourceThrottle::WillRedirectRequest(const GURL& new_url, bool* defer) {
WillStartRequest(defer);
}
const char* CUResourceThrottle::GetNameForLogging() const {
return "ComponentUpdateResourceThrottle";
}
void CUResourceThrottle::Unblock() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (state_ == BLOCKED)
controller()->Resume();
state_ = UNBLOCKED;
}
void UnblockThrottleOnUIThread(base::WeakPtr<CUResourceThrottle> rt) {
BrowserThread::PostTask(BrowserThread::IO,
FROM_HERE,
base::Bind(&CUResourceThrottle::Unblock, rt));
}
} // namespace
content::ResourceThrottle* GetOnDemandResourceThrottle(
ComponentUpdateService* cus,
const std::string& crx_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// We give the raw pointer to the caller, who will delete it at will
// and we keep for ourselves a weak pointer to it so we can post tasks
// from the UI thread without having to track lifetime directly.
CUResourceThrottle* rt = new CUResourceThrottle;
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&ComponentUpdateService::MaybeThrottle,
base::Unretained(cus),
crx_id,
base::Bind(&UnblockThrottleOnUIThread, rt->AsWeakPtr())));
return rt;
}
} // namespace component_updater
// Copyright 2014 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.
#ifndef CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_RESOURCE_THROTTLE_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_RESOURCE_THROTTLE_H_
#include <string>
namespace content {
class ResourceThrottle;
}
namespace component_updater {
class ComponentUpdateService;
// Returns a network resource throttle. It means that a component will be
// downloaded and installed before the resource is unthrottled. This function
// can be called from the IO thread.
content::ResourceThrottle* GetOnDemandResourceThrottle(
ComponentUpdateService* cus,
const std::string& crx_id);
} // namespace component_updater
#endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_RESOURCE_THROTTLE_H_
......@@ -8,6 +8,7 @@
#include <string>
#include <vector>
#include "base/callback_forward.h"
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/version.h"
......@@ -171,6 +172,21 @@ class ComponentUpdateService {
// proactively triggered outside the normal component update service schedule.
virtual OnDemandUpdater& GetOnDemandUpdater() = 0;
// This method is used to trigger an on-demand update for component |crx_id|.
// This can be used when loading a resource that depends on this component.
//
// |callback| is called on the main thread once the on-demand update is
// complete, regardless of success. |callback| may be called immediately
// within the method body.
//
// Additionally, this function implements an embedder-defined cooldown
// interval between on demand update attempts. This behavior is intended
// to be defensive against programming bugs, usually triggered by web fetches,
// where the on-demand functionality is invoked too often. If this function
// is called while still on cooldown, |callback| will be called immediately.
virtual void MaybeThrottle(const std::string& crx_id,
const base::Closure& callback) = 0;
// Returns a task runner suitable for use by component installers.
virtual scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() = 0;
......@@ -183,7 +199,6 @@ class ComponentUpdateService {
CrxUpdateItem* item) const = 0;
friend class ::ComponentsUI;
FRIEND_TEST_ALL_PREFIXES(ComponentUpdaterTest, ResourceThrottleLiveNoUpdate);
};
typedef ComponentUpdateService::Observer ServiceObserver;
......@@ -192,17 +207,6 @@ class OnDemandUpdater {
public:
virtual ~OnDemandUpdater() {}
// Returns a network resource throttle. It means that a component will be
// downloaded and installed before the resource is unthrottled. This function
// can be called from the IO thread. The function implements a cooldown
// interval of 30 minutes. That means it will ineffective to call the
// function before the cooldown interval has passed. This behavior is intended
// to be defensive against programming bugs, usually triggered by web fetches,
// where the on-demand functionality is invoked too often.
virtual content::ResourceThrottle* GetOnDemandResourceThrottle(
net::URLRequest* request,
const std::string& crx_id) = 0;
private:
friend class OnDemandTester;
friend class ::ComponentsUI;
......
......@@ -17,8 +17,6 @@
namespace component_updater {
class CUResourceThrottle;
// This is the one and only per-item state structure. Designed to be hosted
// in a std::vector or a std::list. The two main members are |component|
// which is supplied by the the component updater client and |status| which
......@@ -110,7 +108,7 @@ struct CrxUpdateItem {
std::vector<CrxDownloader::DownloadMetrics> download_metrics;
std::vector<base::WeakPtr<CUResourceThrottle> > throttles;
std::vector<base::Closure> ready_callbacks;
CrxUpdateItem();
~CrxUpdateItem();
......
......@@ -12,6 +12,7 @@
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/component_updater/component_updater_resource_throttle.h"
#include "chrome/browser/component_updater/component_updater_utils.h"
#include "chrome/browser/component_updater/test/test_configurator.h"
#include "chrome/browser/component_updater/test/test_installer.h"
......@@ -1190,9 +1191,7 @@ content::ResourceThrottle* RequestTestResourceThrottle(
NULL,
&context);
content::ResourceThrottle* rt =
cus->GetOnDemandUpdater().GetOnDemandResourceThrottle(&url_request,
crx_id);
content::ResourceThrottle* rt = GetOnDemandResourceThrottle(cus, crx_id);
rt->set_controller_for_testing(controller);
controller->SetThrottle(rt);
return rt;
......
......@@ -12,7 +12,7 @@
#include "base/logging.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/component_updater/component_updater_service.h"
#include "chrome/browser/component_updater/component_updater_resource_throttle.h"
#include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/download/download_request_limiter.h"
......@@ -248,7 +248,7 @@ void AppendComponentUpdaterThrottles(
// We got a component we need to install, so throttle the resource
// until the component is installed.
throttles->push_back(
cus->GetOnDemandUpdater().GetOnDemandResourceThrottle(request, crx_id));
component_updater::GetOnDemandResourceThrottle(cus, crx_id));
}
}
......
......@@ -279,6 +279,8 @@
'browser/component_updater/component_unpacker.h',
'browser/component_updater/component_updater_ping_manager.cc',
'browser/component_updater/component_updater_ping_manager.h',
'browser/component_updater/component_updater_resource_throttle.cc',
'browser/component_updater/component_updater_resource_throttle.h',
'browser/component_updater/component_updater_service.cc',
'browser/component_updater/component_updater_service.h',
'browser/component_updater/component_updater_utils.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment