| OLD | NEW | 
|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "content/browser/background_fetch/background_fetch_context.h" | 5 #include "content/browser/background_fetch/background_fetch_context.h" | 
| 6 | 6 | 
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" | 
| 8 #include "content/browser/background_fetch/background_fetch_data_manager.h" | 8 #include "content/browser/background_fetch/background_fetch_data_manager.h" | 
| 9 #include "content/browser/background_fetch/background_fetch_job_controller.h" | 9 #include "content/browser/background_fetch/background_fetch_job_controller.h" | 
| 10 #include "content/browser/background_fetch/background_fetch_registration_id.h" | 10 #include "content/browser/background_fetch/background_fetch_registration_id.h" | 
| 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 
| 12 #include "content/browser/storage_partition_impl.h" | 12 #include "content/browser/storage_partition_impl.h" | 
| 13 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" | 
|  | 14 #include "net/url_request/url_request_context_getter.h" | 
| 14 #include "url/origin.h" | 15 #include "url/origin.h" | 
| 15 | 16 | 
| 16 namespace content { | 17 namespace content { | 
| 17 | 18 | 
| 18 BackgroundFetchContext::BackgroundFetchContext( | 19 BackgroundFetchContext::BackgroundFetchContext( | 
| 19     BrowserContext* browser_context, | 20     BrowserContext* browser_context, | 
| 20     StoragePartitionImpl* storage_partition, | 21     StoragePartitionImpl* storage_partition, | 
| 21     const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | 22     const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | 
| 22     : browser_context_(browser_context), | 23     : browser_context_(browser_context), | 
| 23       storage_partition_(storage_partition), |  | 
| 24       service_worker_context_(service_worker_context), | 24       service_worker_context_(service_worker_context), | 
| 25       background_fetch_data_manager_( | 25       background_fetch_data_manager_( | 
| 26           base::MakeUnique<BackgroundFetchDataManager>(browser_context)) { | 26           base::MakeUnique<BackgroundFetchDataManager>(browser_context)) { | 
| 27   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 27   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| 28   download_manager_ = BrowserContext::GetDownloadManager(browser_context_); | 28   download_manager_ = BrowserContext::GetDownloadManager(browser_context_); | 
|  | 29   request_context_ = | 
|  | 30       make_scoped_refptr(storage_partition->GetURLRequestContext()); | 
| 29 } | 31 } | 
| 30 | 32 | 
| 31 BackgroundFetchContext::~BackgroundFetchContext() { | 33 BackgroundFetchContext::~BackgroundFetchContext() { | 
| 32   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 34   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| 33 } | 35 } | 
| 34 | 36 | 
| 35 void BackgroundFetchContext::Shutdown() { | 37 void BackgroundFetchContext::Shutdown() { | 
| 36   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 38   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| 37   BrowserThread::PostTask( | 39   BrowserThread::PostTask( | 
| 38       BrowserThread::IO, FROM_HERE, | 40       BrowserThread::IO, FROM_HERE, | 
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 117 | 119 | 
| 118   return controller; | 120   return controller; | 
| 119 } | 121 } | 
| 120 | 122 | 
| 121 void BackgroundFetchContext::CreateController( | 123 void BackgroundFetchContext::CreateController( | 
| 122     const BackgroundFetchRegistrationId& registration_id, | 124     const BackgroundFetchRegistrationId& registration_id, | 
| 123     const BackgroundFetchOptions& options, | 125     const BackgroundFetchOptions& options, | 
| 124     std::vector<BackgroundFetchRequestInfo> initial_requests) { | 126     std::vector<BackgroundFetchRequestInfo> initial_requests) { | 
| 125   std::unique_ptr<BackgroundFetchJobController> controller = | 127   std::unique_ptr<BackgroundFetchJobController> controller = | 
| 126       base::MakeUnique<BackgroundFetchJobController>( | 128       base::MakeUnique<BackgroundFetchJobController>( | 
| 127           registration_id, options, download_manager_, storage_partition_, | 129           registration_id, options, background_fetch_data_manager_.get(), | 
| 128           background_fetch_data_manager_.get(), | 130           download_manager_, request_context_, | 
| 129           base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this)); | 131           base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this)); | 
| 130 | 132 | 
| 131   // TODO(peter): Start actually fetching the files. | 133   // TODO(peter): We should actually be able to use Background Fetch in layout | 
|  | 134   // tests. That requires a download manager and a request context. | 
|  | 135   if (download_manager_ && request_context_) { | 
|  | 136     // Start fetching the |initial_requests| immediately. At some point in the | 
|  | 137     // future we may want a more elaborate scheduling mechanism here. | 
|  | 138     controller->Start(std::move(initial_requests)); | 
|  | 139   } | 
| 132 | 140 | 
| 133   active_fetches_.insert( | 141   active_fetches_.insert( | 
| 134       std::make_pair(registration_id, std::move(controller))); | 142       std::make_pair(registration_id, std::move(controller))); | 
| 135 } | 143 } | 
| 136 | 144 | 
| 137 void BackgroundFetchContext::DidCompleteJob( | 145 void BackgroundFetchContext::DidCompleteJob( | 
| 138     BackgroundFetchJobController* controller) { | 146     BackgroundFetchJobController* controller) { | 
| 139   const BackgroundFetchRegistrationId& registration_id = | 147   const BackgroundFetchRegistrationId& registration_id = | 
| 140       controller->registration_id(); | 148       controller->registration_id(); | 
| 141 | 149 | 
| 142   DCHECK_GT(active_fetches_.count(registration_id), 0u); | 150   DCHECK_GT(active_fetches_.count(registration_id), 0u); | 
| 143 | 151 | 
| 144   if (controller->state() == BackgroundFetchJobController::State::COMPLETED) { | 152   if (controller->state() == BackgroundFetchJobController::State::COMPLETED) { | 
| 145     // TODO(peter): Dispatch the `backgroundfetched` or `backgroundfetchfail` | 153     // TODO(peter): Dispatch the `backgroundfetched` or `backgroundfetchfail` | 
| 146     // event to the Service Worker to inform the developer. | 154     // event to the Service Worker to inform the developer. | 
| 147   } | 155   } | 
| 148 | 156 | 
| 149   active_fetches_.erase(registration_id); | 157   active_fetches_.erase(registration_id); | 
| 150 } | 158 } | 
| 151 | 159 | 
| 152 }  // namespace content | 160 }  // namespace content | 
| OLD | NEW | 
|---|