| 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 "content/browser/background_fetch/background_fetch_job_info.h" | 7 #include "base/memory/ptr_util.h" | 
| 8 #include "content/browser/background_fetch/background_fetch_request_info.h" | 8 #include "content/browser/background_fetch/background_fetch_data_manager.h" | 
|  | 9 #include "content/browser/background_fetch/background_fetch_job_controller.h" | 
|  | 10 #include "content/browser/background_fetch/background_fetch_registration_id.h" | 
| 9 #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" | 
| 10 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" | 
| 11 #include "content/public/browser/download_manager.h" |  | 
| 12 #include "content/public/browser/storage_partition.h" |  | 
| 13 | 14 | 
| 14 namespace content { | 15 namespace content { | 
| 15 | 16 | 
| 16 BackgroundFetchContext::BackgroundFetchContext( | 17 BackgroundFetchContext::BackgroundFetchContext( | 
| 17     BrowserContext* browser_context, | 18     BrowserContext* browser_context, | 
| 18     StoragePartition* storage_partition, | 19     StoragePartitionImpl* storage_partition, | 
| 19     const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | 20     const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | 
| 20     : browser_context_(browser_context), | 21     : browser_context_(browser_context), | 
| 21       storage_partition_(storage_partition), | 22       storage_partition_(storage_partition), | 
| 22       service_worker_context_(service_worker_context), | 23       service_worker_context_(service_worker_context), | 
| 23       background_fetch_data_manager_(browser_context) { | 24       background_fetch_data_manager_( | 
|  | 25           base::MakeUnique<BackgroundFetchDataManager>(browser_context)) { | 
| 24   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 26   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| 25   // TODO(harkness): BackgroundFetchContext should have |  | 
| 26   // ServiceWorkerContextObserver as a parent class and should register as an |  | 
| 27   // observer here. |  | 
| 28 } | 27 } | 
| 29 | 28 | 
| 30 BackgroundFetchContext::~BackgroundFetchContext() { | 29 BackgroundFetchContext::~BackgroundFetchContext() { | 
| 31   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 30   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| 32 } | 31 } | 
| 33 | 32 | 
| 34 void BackgroundFetchContext::Init() { |  | 
| 35   DCHECK_CURRENTLY_ON(BrowserThread::UI); |  | 
| 36 } |  | 
| 37 |  | 
| 38 void BackgroundFetchContext::Shutdown() { | 33 void BackgroundFetchContext::Shutdown() { | 
| 39   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 34   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| 40 |  | 
| 41   BrowserThread::PostTask( | 35   BrowserThread::PostTask( | 
| 42       BrowserThread::IO, FROM_HERE, | 36       BrowserThread::IO, FROM_HERE, | 
| 43       base::Bind(&BackgroundFetchContext::ShutdownOnIO, this)); | 37       base::Bind(&BackgroundFetchContext::ShutdownOnIO, this)); | 
| 44 } | 38 } | 
| 45 | 39 | 
| 46 void BackgroundFetchContext::ShutdownOnIO() { | 40 void BackgroundFetchContext::ShutdownOnIO() { | 
| 47   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 41   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 48 | 42   active_fetches_.clear(); | 
| 49   // Call Shutdown on all pending job controllers to give them a chance to flush |  | 
| 50   // any status to the DataManager. |  | 
| 51   for (auto& job : job_map_) |  | 
| 52     job.second->Shutdown(); |  | 
| 53 |  | 
| 54   job_map_.clear(); |  | 
| 55 } | 43 } | 
| 56 | 44 | 
| 57 void BackgroundFetchContext::CreateRequest( | 45 void BackgroundFetchContext::StartFetch( | 
| 58     std::unique_ptr<BackgroundFetchJobInfo> job_info, | 46     const BackgroundFetchRegistrationId& registration_id, | 
| 59     std::vector<std::unique_ptr<BackgroundFetchRequestInfo>> request_infos) { | 47     const std::vector<ServiceWorkerFetchRequest>& requests, | 
|  | 48     const BackgroundFetchOptions& options, | 
|  | 49     const blink::mojom::BackgroundFetchService::FetchCallback& callback) { | 
| 60   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 50   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 61   DCHECK_GE(1U, request_infos.size()); | 51   background_fetch_data_manager_->CreateRegistration( | 
| 62 | 52       registration_id, requests, options, | 
| 63   // Inform the data manager about the new download. | 53       base::BindOnce(&BackgroundFetchContext::DidCreateRegistration, this, | 
| 64   const std::string job_guid = job_info->guid(); | 54                      registration_id, options, callback)); | 
| 65   background_fetch_data_manager_.CreateRequest(std::move(job_info), |  | 
| 66                                                std::move(request_infos)); |  | 
| 67 |  | 
| 68   // Create a controller which drives the processing of the job. It will use |  | 
| 69   // the DataManager to get information about individual requests for the job. |  | 
| 70   job_map_[job_guid] = base::MakeUnique<BackgroundFetchJobController>( |  | 
| 71       job_guid, browser_context_, storage_partition_, |  | 
| 72       &background_fetch_data_manager_, |  | 
| 73       base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this, job_guid)); |  | 
| 74 } | 55 } | 
| 75 | 56 | 
| 76 void BackgroundFetchContext::DidCompleteJob(const std::string& job_guid) { | 57 void BackgroundFetchContext::DidCreateRegistration( | 
| 77   DCHECK(job_map_.find(job_guid) != job_map_.end()); | 58     const BackgroundFetchRegistrationId& registration_id, | 
|  | 59     const BackgroundFetchOptions& options, | 
|  | 60     const blink::mojom::BackgroundFetchService::FetchCallback& callback, | 
|  | 61     blink::mojom::BackgroundFetchError error) { | 
|  | 62   if (error != blink::mojom::BackgroundFetchError::NONE) { | 
|  | 63     callback.Run(error, base::nullopt /* registration */); | 
|  | 64     return; | 
|  | 65   } | 
| 78 | 66 | 
| 79   job_map_.erase(job_guid); | 67   // Create the BackgroundFetchJobController, which will do the actual fetching. | 
|  | 68   CreateController(registration_id, options); | 
| 80 | 69 | 
| 81   // TODO(harkness): Once the caller receives the message, inform the | 70   // Create the BackgroundFetchRegistration the renderer process will receive, | 
| 82   // DataManager that it can clean up the pending job. | 71   // which enables it to resolve the promise telling the developer it worked. | 
|  | 72   BackgroundFetchRegistration registration; | 
|  | 73   registration.tag = registration_id.tag(); | 
|  | 74   registration.icons = options.icons; | 
|  | 75   registration.title = options.title; | 
|  | 76   registration.total_download_size = options.total_download_size; | 
|  | 77 | 
|  | 78   callback.Run(blink::mojom::BackgroundFetchError::NONE, registration); | 
|  | 79 } | 
|  | 80 | 
|  | 81 void BackgroundFetchContext::CreateController( | 
|  | 82     const BackgroundFetchRegistrationId& registration_id, | 
|  | 83     const BackgroundFetchOptions& options) { | 
|  | 84   std::unique_ptr<BackgroundFetchJobController> controller = | 
|  | 85       base::MakeUnique<BackgroundFetchJobController>( | 
|  | 86           registration_id, browser_context_, storage_partition_, | 
|  | 87           background_fetch_data_manager_.get(), | 
|  | 88           base::BindOnce(&BackgroundFetchContext::DidFinishFetch, this)); | 
|  | 89 | 
|  | 90   active_fetches_.insert( | 
|  | 91       std::make_pair(registration_id, std::move(controller))); | 
|  | 92 } | 
|  | 93 | 
|  | 94 void BackgroundFetchContext::DidFinishFetch( | 
|  | 95     const BackgroundFetchRegistrationId& registration_id) { | 
|  | 96   DCHECK_GT(active_fetches_.count(registration_id), 0u); | 
|  | 97 | 
|  | 98   // TODO(peter): Dispatch the `backgroundfetched` or the `backgroundfetchfail` | 
|  | 99   // event to the Service Worker. | 
|  | 100 | 
|  | 101   active_fetches_.erase(registration_id); | 
| 83 } | 102 } | 
| 84 | 103 | 
| 85 }  // namespace content | 104 }  // namespace content | 
| OLD | NEW | 
|---|