| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_SERVICE_WORKER_SERVICE_WORKER_FETCH_DISPATCHER_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_DISPATCHER_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_DISPATCHER_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_DISPATCHER_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "content/browser/service_worker/service_worker_metrics.h" | 11 #include "content/browser/service_worker/service_worker_metrics.h" |
| 12 #include "content/common/content_export.h" | 12 #include "content/common/content_export.h" |
| 13 #include "content/common/service_worker/service_worker_status_code.h" | 13 #include "content/common/service_worker/service_worker_status_code.h" |
| 14 #include "content/common/service_worker/service_worker_types.h" | 14 #include "content/common/service_worker/service_worker_types.h" |
| 15 #include "content/public/common/resource_type.h" | 15 #include "content/public/common/resource_type.h" |
| 16 #include "net/log/net_log.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 | 19 |
| 19 class ServiceWorkerVersion; | 20 class ServiceWorkerVersion; |
| 20 | 21 |
| 21 // A helper class to dispatch fetch event to a service worker. | 22 // A helper class to dispatch fetch event to a service worker. |
| 22 class CONTENT_EXPORT ServiceWorkerFetchDispatcher { | 23 class CONTENT_EXPORT ServiceWorkerFetchDispatcher { |
| 23 public: | 24 public: |
| 24 typedef base::Callback<void(ServiceWorkerStatusCode, | 25 typedef base::Callback<void(ServiceWorkerStatusCode, |
| 25 ServiceWorkerFetchEventResult, | 26 ServiceWorkerFetchEventResult, |
| 26 const ServiceWorkerResponse&, | 27 const ServiceWorkerResponse&, |
| 27 const scoped_refptr<ServiceWorkerVersion>&)> | 28 const scoped_refptr<ServiceWorkerVersion>&)> |
| 28 FetchCallback; | 29 FetchCallback; |
| 29 | 30 |
| 30 ServiceWorkerFetchDispatcher( | 31 ServiceWorkerFetchDispatcher( |
| 31 std::unique_ptr<ServiceWorkerFetchRequest> request, | 32 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 32 ServiceWorkerVersion* version, | 33 ServiceWorkerVersion* version, |
| 33 ResourceType resource_type, | 34 ResourceType resource_type, |
| 35 const net::BoundNetLog& net_log, |
| 34 const base::Closure& prepare_callback, | 36 const base::Closure& prepare_callback, |
| 35 const FetchCallback& fetch_callback); | 37 const FetchCallback& fetch_callback); |
| 36 ~ServiceWorkerFetchDispatcher(); | 38 ~ServiceWorkerFetchDispatcher(); |
| 37 | 39 |
| 38 // Dispatches a fetch event to the |version| given in ctor, and fires | 40 // Dispatches a fetch event to the |version| given in ctor, and fires |
| 39 // |callback| (also given in ctor) when finishes. | 41 // |fetch_callback| (also given in ctor) when finishes. It runs |
| 42 // |prepare_callback| as an intermediate step once the version is activated |
| 43 // and running. |
| 40 void Run(); | 44 void Run(); |
| 41 | 45 |
| 42 private: | 46 private: |
| 47 void DidWaitForActivation(); |
| 43 void StartWorker(); | 48 void StartWorker(); |
| 49 void DidStartWorker(); |
| 50 void DidFailToStartWorker(ServiceWorkerStatusCode status); |
| 44 void DispatchFetchEvent(); | 51 void DispatchFetchEvent(); |
| 45 void DidPrepare(); | 52 void DidFailToDispatch(ServiceWorkerStatusCode status); |
| 46 void DidFail(ServiceWorkerStatusCode status); | 53 void DidFail(ServiceWorkerStatusCode status); |
| 47 void DidFinish(int request_id, | 54 void DidFinish(int request_id, |
| 48 ServiceWorkerFetchEventResult fetch_result, | 55 ServiceWorkerFetchEventResult fetch_result, |
| 49 const ServiceWorkerResponse& response); | 56 const ServiceWorkerResponse& response); |
| 57 void Complete(ServiceWorkerStatusCode status, |
| 58 ServiceWorkerFetchEventResult fetch_result, |
| 59 const ServiceWorkerResponse& response); |
| 50 | 60 |
| 51 ServiceWorkerMetrics::EventType GetEventType() const; | 61 ServiceWorkerMetrics::EventType GetEventType() const; |
| 52 | 62 |
| 53 scoped_refptr<ServiceWorkerVersion> version_; | 63 scoped_refptr<ServiceWorkerVersion> version_; |
| 64 net::BoundNetLog net_log_; |
| 54 base::Closure prepare_callback_; | 65 base::Closure prepare_callback_; |
| 55 FetchCallback fetch_callback_; | 66 FetchCallback fetch_callback_; |
| 56 std::unique_ptr<ServiceWorkerFetchRequest> request_; | 67 std::unique_ptr<ServiceWorkerFetchRequest> request_; |
| 57 ResourceType resource_type_; | 68 ResourceType resource_type_; |
| 69 bool did_complete_; |
| 58 base::WeakPtrFactory<ServiceWorkerFetchDispatcher> weak_factory_; | 70 base::WeakPtrFactory<ServiceWorkerFetchDispatcher> weak_factory_; |
| 59 | 71 |
| 60 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFetchDispatcher); | 72 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFetchDispatcher); |
| 61 }; | 73 }; |
| 62 | 74 |
| 63 } // namespace content | 75 } // namespace content |
| 64 | 76 |
| 65 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_DISPATCHER_H_ | 77 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_DISPATCHER_H_ |
| OLD | NEW |