| 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_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_ | 5 #ifndef CONTENT_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_ |
| 6 #define CONTENT_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_ | 6 #define CONTENT_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "content/public/browser/service_worker_usage_info.h" | 12 #include "content/public/browser/service_worker_usage_info.h" |
| 13 #include "url/gurl.h" | 13 #include "url/gurl.h" |
| 14 | 14 |
| 15 namespace blink { | 15 namespace blink { |
| 16 enum class WebNavigationHintType; | 16 enum class WebNavigationHintType; |
| 17 } | 17 } |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 // Represents the per-StoragePartition ServiceWorker data. | 21 // Represents the per-StoragePartition ServiceWorker data. |
| 22 class ServiceWorkerContext { | 22 class ServiceWorkerContext { |
| 23 public: | 23 public: |
| 24 // https://rawgithub.com/slightlyoff/ServiceWorker/master/spec/service_worker/
index.html#url-scope: | 24 // https://rawgithub.com/slightlyoff/ServiceWorker/master/spec/service_worker/
index.html#url-scope: |
| 25 // roughly, must be of the form "<origin>/<path>/*". | 25 // roughly, must be of the form "<origin>/<path>/*". |
| 26 typedef GURL Scope; | 26 using Scope = GURL; |
| 27 | 27 |
| 28 typedef base::Callback<void(bool success)> ResultCallback; | 28 using ResultCallback = base::Callback<void(bool success)>; |
| 29 | 29 |
| 30 typedef base::Callback<void(const std::vector<ServiceWorkerUsageInfo>& | 30 using GetUsageInfoCallback = base::Callback<void( |
| 31 usage_info)> GetUsageInfoCallback; | 31 const std::vector<ServiceWorkerUsageInfo>& usage_info)>; |
| 32 | 32 |
| 33 typedef base::Callback<void(bool has_service_worker)> | 33 using CheckHasServiceWorkerCallback = |
| 34 CheckHasServiceWorkerCallback; | 34 base::Callback<void(bool has_service_worker)>; |
| 35 |
| 36 using CountExternalRequestsCallback = |
| 37 base::Callback<void(size_t external_request_count)>; |
| 35 | 38 |
| 36 // Registers the header name which should not be passed to the ServiceWorker. | 39 // Registers the header name which should not be passed to the ServiceWorker. |
| 37 // Must be called from the IO thread. | 40 // Must be called from the IO thread. |
| 38 CONTENT_EXPORT static void AddExcludedHeadersForFetchEvent( | 41 CONTENT_EXPORT static void AddExcludedHeadersForFetchEvent( |
| 39 const std::set<std::string>& header_names); | 42 const std::set<std::string>& header_names); |
| 40 | 43 |
| 41 // Returns true if the header name should not be passed to the ServiceWorker. | 44 // Returns true if the header name should not be passed to the ServiceWorker. |
| 42 // Must be called from the IO thread. | 45 // Must be called from the IO thread. |
| 43 static bool IsExcludedHeaderNameForFetchEvent(const std::string& header_name); | 46 static bool IsExcludedHeaderNameForFetchEvent(const std::string& header_name); |
| 44 | 47 |
| 45 // Equivalent to calling navigator.serviceWorker.register(script_url, {scope: | 48 // Equivalent to calling navigator.serviceWorker.register(script_url, {scope: |
| 46 // pattern}) from a renderer, except that |pattern| is an absolute URL instead | 49 // pattern}) from a renderer, except that |pattern| is an absolute URL instead |
| 47 // of relative to some current origin. |callback| is passed true when the JS | 50 // of relative to some current origin. |callback| is passed true when the JS |
| 48 // promise is fulfilled or false when the JS promise is rejected. | 51 // promise is fulfilled or false when the JS promise is rejected. |
| 49 // | 52 // |
| 50 // The registration can fail if: | 53 // The registration can fail if: |
| 51 // * |script_url| is on a different origin from |pattern| | 54 // * |script_url| is on a different origin from |pattern| |
| 52 // * Fetching |script_url| fails. | 55 // * Fetching |script_url| fails. |
| 53 // * |script_url| fails to parse or its top-level execution fails. | 56 // * |script_url| fails to parse or its top-level execution fails. |
| 54 // TODO: The error message for this needs to be available to developers. | 57 // TODO: The error message for this needs to be available to developers. |
| 55 // * Something unexpected goes wrong, like a renderer crash or a full disk. | 58 // * Something unexpected goes wrong, like a renderer crash or a full disk. |
| 56 // | 59 // |
| 57 // This function can be called from any thread, but the callback will always | 60 // This function can be called from any thread, but the callback will always |
| 58 // be called on the UI thread. | 61 // be called on the UI thread. |
| 59 virtual void RegisterServiceWorker(const Scope& pattern, | 62 virtual void RegisterServiceWorker(const Scope& pattern, |
| 60 const GURL& script_url, | 63 const GURL& script_url, |
| 61 const ResultCallback& callback) = 0; | 64 const ResultCallback& callback) = 0; |
| 62 | 65 |
| 66 // Mechanism for embedder to increment/decrement ref count of a service |
| 67 // worker. |
| 68 // Embedders can call StartingExternalRequest() while it is performing some |
| 69 // work with the worker. The worker is considered to be working until embedder |
| 70 // calls FinishedExternalRequest(). This ensures that content/ does not |
| 71 // shut the worker down while embedder is expecting the worker to be kept |
| 72 // alive. |
| 73 // |
| 74 // Must be called from the IO thread. Returns whether or not changing the ref |
| 75 // count succeeded. |
| 76 virtual bool StartingExternalRequest(int64_t service_worker_version_id, |
| 77 const std::string& request_uuid) = 0; |
| 78 virtual bool FinishedExternalRequest(int64_t service_worker_version_id, |
| 79 const std::string& request_uuid) = 0; |
| 80 |
| 63 // Equivalent to calling navigator.serviceWorker.unregister(pattern) from a | 81 // Equivalent to calling navigator.serviceWorker.unregister(pattern) from a |
| 64 // renderer, except that |pattern| is an absolute URL instead of relative to | 82 // renderer, except that |pattern| is an absolute URL instead of relative to |
| 65 // some current origin. |callback| is passed true when the JS promise is | 83 // some current origin. |callback| is passed true when the JS promise is |
| 66 // fulfilled or false when the JS promise is rejected. | 84 // fulfilled or false when the JS promise is rejected. |
| 67 // | 85 // |
| 68 // Unregistration can fail if: | 86 // Unregistration can fail if: |
| 69 // * No Service Worker was registered for |pattern|. | 87 // * No Service Worker was registered for |pattern|. |
| 70 // * Something unexpected goes wrong, like a renderer crash. | 88 // * Something unexpected goes wrong, like a renderer crash. |
| 71 // | 89 // |
| 72 // This function can be called from any thread, but the callback will always | 90 // This function can be called from any thread, but the callback will always |
| (...skipping 16 matching lines...) Expand all Loading... |
| 89 // this still returns true even if there is a Service Worker registration | 107 // this still returns true even if there is a Service Worker registration |
| 90 // which has a longer match for |other_url|. | 108 // which has a longer match for |other_url|. |
| 91 // | 109 // |
| 92 // This function can be called from any thread, but the callback will always | 110 // This function can be called from any thread, but the callback will always |
| 93 // be called on the UI thread. | 111 // be called on the UI thread. |
| 94 virtual void CheckHasServiceWorker( | 112 virtual void CheckHasServiceWorker( |
| 95 const GURL& url, | 113 const GURL& url, |
| 96 const GURL& other_url, | 114 const GURL& other_url, |
| 97 const CheckHasServiceWorkerCallback& callback) = 0; | 115 const CheckHasServiceWorkerCallback& callback) = 0; |
| 98 | 116 |
| 117 // Returns the pending external request count for the worker with the |
| 118 // specified |origin| via |callback|. |
| 119 virtual void CountExternalRequestsForTest( |
| 120 const GURL& origin, |
| 121 const CountExternalRequestsCallback& callback) = 0; |
| 122 |
| 99 // Stops all running workers on the given |origin|. | 123 // Stops all running workers on the given |origin|. |
| 100 // | 124 // |
| 101 // This function can be called from any thread. | 125 // This function can be called from any thread. |
| 102 virtual void StopAllServiceWorkersForOrigin(const GURL& origin) = 0; | 126 virtual void StopAllServiceWorkersForOrigin(const GURL& origin) = 0; |
| 103 | 127 |
| 104 // Stops all running service workers and unregisters all service worker | 128 // Stops all running service workers and unregisters all service worker |
| 105 // registrations. This method is used in LayoutTests to make sure that the | 129 // registrations. This method is used in LayoutTests to make sure that the |
| 106 // existing service worker will not affect the succeeding tests. | 130 // existing service worker will not affect the succeeding tests. |
| 107 // | 131 // |
| 108 // This function can be called from any thread, but the callback will always | 132 // This function can be called from any thread, but the callback will always |
| (...skipping 13 matching lines...) Expand all Loading... |
| 122 const ResultCallback& callback) = 0; | 146 const ResultCallback& callback) = 0; |
| 123 | 147 |
| 124 protected: | 148 protected: |
| 125 ServiceWorkerContext() {} | 149 ServiceWorkerContext() {} |
| 126 virtual ~ServiceWorkerContext() {} | 150 virtual ~ServiceWorkerContext() {} |
| 127 }; | 151 }; |
| 128 | 152 |
| 129 } // namespace content | 153 } // namespace content |
| 130 | 154 |
| 131 #endif // CONTENT_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_ | 155 #endif // CONTENT_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_ |
| OLD | NEW |