| 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 #ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_CONTEXT_H_ |   5 #ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_CONTEXT_H_ | 
|   6 #define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_CONTEXT_H_ |   6 #define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_CONTEXT_H_ | 
|   7  |   7  | 
|   8 #include <unordered_map> |   8 #include <map> | 
 |   9 #include <vector> | 
|   9  |  10  | 
|  10 #include "base/macros.h" |  11 #include "base/macros.h" | 
|  11 #include "base/memory/ref_counted.h" |  12 #include "base/memory/ref_counted.h" | 
|  12 #include "content/browser/background_fetch/background_fetch_data_manager.h" |  | 
|  13 #include "content/browser/background_fetch/background_fetch_job_controller.h" |  | 
|  14 #include "content/common/content_export.h" |  13 #include "content/common/content_export.h" | 
|  15 #include "content/public/browser/browser_thread.h" |  14 #include "content/public/browser/browser_thread.h" | 
 |  15 #include "third_party/WebKit/public/platform/modules/background_fetch/background
    _fetch.mojom.h" | 
|  16  |  16  | 
|  17 namespace content { |  17 namespace content { | 
|  18  |  18  | 
|  19 class BackgroundFetchJobInfo; |  19 class BackgroundFetchDataManager; | 
|  20 class BackgroundFetchRequestInfo; |  20 class BackgroundFetchJobController; | 
 |  21 struct BackgroundFetchOptions; | 
 |  22 class BackgroundFetchRegistrationId; | 
|  21 class BrowserContext; |  23 class BrowserContext; | 
|  22 class ServiceWorkerContextWrapper; |  24 class ServiceWorkerContextWrapper; | 
 |  25 struct ServiceWorkerFetchRequest; | 
 |  26 class StoragePartitionImpl; | 
|  23  |  27  | 
|  24 // The BackgroundFetchContext is the central moderator of ongoing background |  28 // The BackgroundFetchContext is the central moderator of ongoing background | 
|  25 // fetch requests from the Mojo service and from other callers. |  29 // fetch requests from the Mojo service and from other callers. | 
|  26 // Background Fetch requests function similar to normal fetches except that |  30 // Background Fetch requests function similar to normal fetches except that | 
|  27 // they are persistent across Chromium or service worker shutdown. |  31 // they are persistent across Chromium or service worker shutdown. | 
|  28 class CONTENT_EXPORT BackgroundFetchContext |  32 class CONTENT_EXPORT BackgroundFetchContext | 
|  29     : public base::RefCountedThreadSafe<BackgroundFetchContext, |  33     : public base::RefCountedThreadSafe<BackgroundFetchContext, | 
|  30                                         BrowserThread::DeleteOnUIThread> { |  34                                         BrowserThread::DeleteOnUIThread> { | 
|  31  public: |  35  public: | 
|  32   // The BackgroundFetchContext will watch the ServiceWorkerContextWrapper so |  36   // The BackgroundFetchContext will watch the ServiceWorkerContextWrapper so | 
|  33   // that it can respond to service worker events such as unregister. |  37   // that it can respond to service worker events such as unregister. | 
|  34   BackgroundFetchContext( |  38   BackgroundFetchContext( | 
|  35       BrowserContext* browser_context, |  39       BrowserContext* browser_context, | 
|  36       StoragePartition* storage_partition, |  40       StoragePartitionImpl* storage_partition, | 
|  37       const scoped_refptr<ServiceWorkerContextWrapper>& context); |  41       const scoped_refptr<ServiceWorkerContextWrapper>& context); | 
|  38  |  42  | 
|  39   // Init and Shutdown are for use on the UI thread when the StoragePartition is |  | 
|  40   // being setup and torn down. |  | 
|  41   void Init(); |  | 
|  42  |  | 
|  43   // Shutdown must be called before deleting this. Call on the UI thread. |  43   // Shutdown must be called before deleting this. Call on the UI thread. | 
|  44   void Shutdown(); |  44   void Shutdown(); | 
|  45  |  45  | 
|  46   BackgroundFetchDataManager* GetDataManagerForTesting() { |  46   // Starts a Background Fetch for the |registration_id|. The |requests| will be | 
|  47     return &background_fetch_data_manager_; |  47   // asynchronously fetched. The |callback| will be invoked when the fetch has | 
|  48   } |  48   // been registered, or an error occurred that avoids it from doing so. | 
 |  49   void StartFetch( | 
 |  50       const BackgroundFetchRegistrationId& registration_id, | 
 |  51       const std::vector<ServiceWorkerFetchRequest>& requests, | 
 |  52       const BackgroundFetchOptions& options, | 
 |  53       const blink::mojom::BackgroundFetchService::FetchCallback& callback); | 
|  49  |  54  | 
|  50  private: |  55  private: | 
|  51   friend class base::DeleteHelper<BackgroundFetchContext>; |  56   friend class base::DeleteHelper<BackgroundFetchContext>; | 
|  52   friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |  57   friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 
|  53   friend class base::RefCountedThreadSafe<BackgroundFetchContext, |  58   friend class base::RefCountedThreadSafe<BackgroundFetchContext, | 
|  54                                           BrowserThread::DeleteOnUIThread>; |  59                                           BrowserThread::DeleteOnUIThread>; | 
|  55  |  60  | 
|  56   ~BackgroundFetchContext(); |  61   ~BackgroundFetchContext(); | 
|  57  |  62  | 
|  58   void CreateRequest( |  63   // Shuts down the active Job Controllers on the IO thread. | 
|  59       std::unique_ptr<BackgroundFetchJobInfo> job_info, |  | 
|  60       std::vector<std::unique_ptr<BackgroundFetchRequestInfo>> request_infos); |  | 
|  61  |  | 
|  62   // Callback for the JobController when the job is complete. |  | 
|  63   void DidCompleteJob(const std::string& job_guid); |  | 
|  64  |  | 
|  65   void ShutdownOnIO(); |  64   void ShutdownOnIO(); | 
|  66  |  65  | 
|  67   // |this| is owned by the BrowserContext via the StoragePartition. |  66   // Creates a new Job Controller for the given |registration_id| and |options|, | 
 |  67   // which will start fetching the files that are part of the registration. | 
 |  68   void CreateController(const BackgroundFetchRegistrationId& registration_id, | 
 |  69                         const BackgroundFetchOptions& options); | 
 |  70  | 
 |  71   // Called when a new registration has been created by the data manager. | 
 |  72   void DidCreateRegistration( | 
 |  73       const BackgroundFetchRegistrationId& registration_id, | 
 |  74       const BackgroundFetchOptions& options, | 
 |  75       const blink::mojom::BackgroundFetchService::FetchCallback& callback, | 
 |  76       blink::mojom::BackgroundFetchError error); | 
 |  77  | 
 |  78   // Called when a Job Controller has finished processing a Background Fetch | 
 |  79   // registration, as identified by |registration_id|. | 
 |  80   void DidFinishFetch(const BackgroundFetchRegistrationId& registration_id); | 
 |  81  | 
 |  82   // |this| is owned by the BrowserContext via the StoragePartitionImpl. | 
|  68   BrowserContext* browser_context_; |  83   BrowserContext* browser_context_; | 
|  69   StoragePartition* storage_partition_; |  84   StoragePartitionImpl* storage_partition_; | 
|  70  |  85  | 
|  71   scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |  86   scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | 
|  72   BackgroundFetchDataManager background_fetch_data_manager_; |  87   std::unique_ptr<BackgroundFetchDataManager> background_fetch_data_manager_; | 
|  73  |  88  | 
|  74   std::unordered_map<std::string, std::unique_ptr<BackgroundFetchJobController>> |  89   // Map of the Background Fetch fetches that are currently in-progress. | 
|  75       job_map_; |  90   std::map<BackgroundFetchRegistrationId, | 
 |  91            std::unique_ptr<BackgroundFetchJobController>> | 
 |  92       active_fetches_; | 
|  76  |  93  | 
|  77   DISALLOW_COPY_AND_ASSIGN(BackgroundFetchContext); |  94   DISALLOW_COPY_AND_ASSIGN(BackgroundFetchContext); | 
|  78 }; |  95 }; | 
|  79  |  96  | 
|  80 }  // namespace content |  97 }  // namespace content | 
|  81  |  98  | 
|  82 #endif  // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_CONTEXT_H_ |  99 #endif  // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_CONTEXT_H_ | 
| OLD | NEW |