1// Copyright 2013 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. 4 5#ifndef CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_ 6#define CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_ 7 8#include <windows.h> 9#include <bits.h> 10 11#include "base/strings/string16.h" 12#include "base/time/time.h" 13#include "base/timer/timer.h" 14#include "base/win/scoped_comptr.h" 15#include "chrome/browser/component_updater/crx_downloader.h" 16 17namespace base { 18class FilePath; 19} 20 21namespace component_updater { 22 23// Implements a downloader in terms of the BITS service. The public interface 24// of this class and the CrxDownloader overrides are expected to be called 25// from the UI thread. The rest of the class code runs on the FILE thread in 26// a single threaded apartment. Instances of this class are created and 27// destroyed in the UI thread. See the implementation of the class destructor 28// for details regarding the clean up of resources acquired in this class. 29class BackgroundDownloader : public CrxDownloader { 30 protected: 31 friend class CrxDownloader; 32 BackgroundDownloader(scoped_ptr<CrxDownloader> successor, 33 net::URLRequestContextGetter* context_getter, 34 scoped_refptr<base::SequencedTaskRunner> task_runner); 35 virtual ~BackgroundDownloader(); 36 37 private: 38 // Overrides for CrxDownloader. 39 virtual void DoStartDownload(const GURL& url) OVERRIDE; 40 41 // Called asynchronously on the FILE thread at different stages during 42 // the download. |OnDownloading| can be called multiple times. 43 // |EndDownload| switches the execution flow from the FILE to the UI thread. 44 // Accessing any data members of this object on the FILE thread after 45 // calling |EndDownload| is unsafe. 46 void BeginDownload(const GURL& url); 47 void OnDownloading(); 48 void EndDownload(HRESULT hr); 49 50 // Handles the job state transitions to a final state. 51 void OnStateTransferred(); 52 void OnStateError(); 53 void OnStateCancelled(); 54 void OnStateAcknowledged(); 55 56 // Handles the transition to a transient state where the job is in the 57 // queue but not actively transferring data. 58 void OnStateQueued(); 59 60 // Handles the job state transition to a transient, non-final error state. 61 void OnStateTransientError(); 62 63 // Handles the job state corresponding to transferring data. 64 void OnStateTransferring(); 65 66 HRESULT QueueBitsJob(const GURL& url); 67 HRESULT CreateOrOpenJob(const GURL& url); 68 HRESULT InitializeNewJob(const GURL& url); 69 70 // Returns true if at the time of the call, it appears that the job 71 // has not been making progress toward completion. 72 bool IsStuck(); 73 74 // Makes the downloaded file available to the caller by renaming the 75 // temporary file to its destination and removing it from the BITS queue. 76 HRESULT CompleteJob(); 77 78 net::URLRequestContextGetter* context_getter_; 79 scoped_refptr<base::SequencedTaskRunner> task_runner_; 80 81 // The timer and the BITS interface pointers have thread affinity. These 82 // members are initialized on the FILE thread and they must be destroyed 83 // on the FILE thread. 84 scoped_ptr<base::RepeatingTimer<BackgroundDownloader> > timer_; 85 86 base::win::ScopedComPtr<IBackgroundCopyManager> bits_manager_; 87 base::win::ScopedComPtr<IBackgroundCopyJob> job_; 88 89 // Contains the time when the download of the current url has started. 90 base::Time download_start_time_; 91 92 // Contains the time when the BITS job is last seen making progress. 93 base::Time job_stuck_begin_time_; 94 95 // True if EndDownload was called. 96 bool is_completed_; 97 98 // Contains the path of the downloaded file if the download was successful. 99 base::FilePath response_; 100 101 DISALLOW_COPY_AND_ASSIGN(BackgroundDownloader); 102}; 103 104} // namespace component_updater 105 106#endif // CHROME_BROWSER_COMPONENT_UPDATER_BACKGROUND_DOWNLOADER_WIN_H_ 107