1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/component_updater/component_updater_resource_throttle.h"
7 #include "base/location.h"
8 #include "base/memory/weak_ptr.h"
9 #include "components/component_updater/component_updater_service.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/resource_controller.h"
12 #include "content/public/browser/resource_throttle.h"
14 using content::BrowserThread
;
16 namespace component_updater
{
20 ///////////////////////////////////////////////////////////////////////////////
21 // In charge of blocking url requests until the |crx_id| component has been
22 // updated. This class is touched solely from the IO thread. The UI thread
23 // can post tasks to it via weak pointers. By default the request is blocked
24 // unless the CrxUpdateService calls Unblock().
25 // The lifetime is controlled by Chrome's resource loader so the component
26 // updater cannot touch objects from this class except via weak pointers.
27 class CUResourceThrottle
: public content::ResourceThrottle
,
28 public base::SupportsWeakPtr
<CUResourceThrottle
> {
31 virtual ~CUResourceThrottle();
33 // Overriden from ResourceThrottle.
34 virtual void WillStartRequest(bool* defer
) OVERRIDE
;
35 virtual void WillRedirectRequest(const GURL
& new_url
, bool* defer
) OVERRIDE
;
36 virtual const char* GetNameForLogging() const OVERRIDE
;
38 // Component updater calls this function via PostTask to unblock the request.
41 typedef std::vector
<base::WeakPtr
<CUResourceThrottle
> > WeakPtrVector
;
44 enum State
{ NEW
, BLOCKED
, UNBLOCKED
};
49 CUResourceThrottle::CUResourceThrottle() : state_(NEW
) {
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
53 CUResourceThrottle::~CUResourceThrottle() {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
57 void CUResourceThrottle::WillStartRequest(bool* defer
) {
58 if (state_
!= UNBLOCKED
) {
66 void CUResourceThrottle::WillRedirectRequest(const GURL
& new_url
, bool* defer
) {
67 WillStartRequest(defer
);
70 const char* CUResourceThrottle::GetNameForLogging() const {
71 return "ComponentUpdateResourceThrottle";
74 void CUResourceThrottle::Unblock() {
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
76 if (state_
== BLOCKED
)
77 controller()->Resume();
81 void UnblockThrottleOnUIThread(base::WeakPtr
<CUResourceThrottle
> rt
) {
82 BrowserThread::PostTask(BrowserThread::IO
,
84 base::Bind(&CUResourceThrottle::Unblock
, rt
));
89 content::ResourceThrottle
* GetOnDemandResourceThrottle(
90 ComponentUpdateService
* cus
,
91 const std::string
& crx_id
) {
92 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
94 // We give the raw pointer to the caller, who will delete it at will
95 // and we keep for ourselves a weak pointer to it so we can post tasks
96 // from the UI thread without having to track lifetime directly.
97 CUResourceThrottle
* rt
= new CUResourceThrottle
;
98 BrowserThread::PostTask(
101 base::Bind(&ComponentUpdateService::MaybeThrottle
,
102 base::Unretained(cus
),
104 base::Bind(&UnblockThrottleOnUIThread
, rt
->AsWeakPtr())));
108 } // namespace component_updater