1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_RENDERER_SERVICE_WORKER_WEB_SERVICE_WORKER_PROVIDER_IMPL_H_ 6 #define CONTENT_RENDERER_SERVICE_WORKER_WEB_SERVICE_WORKER_PROVIDER_IMPL_H_ 7 8 #include <memory> 9 10 #include "base/compiler_specific.h" 11 #include "base/macros.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/weak_ptr.h" 14 #include "content/common/content_export.h" 15 #include "third_party/blink/public/common/messaging/transferable_message.h" 16 #include "third_party/blink/public/mojom/service_worker/service_worker_error_type.mojom-forward.h" 17 #include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h" 18 #include "third_party/blink/public/mojom/web_feature/web_feature.mojom-forward.h" 19 #include "third_party/blink/public/platform/modules/service_worker/web_service_worker_provider.h" 20 21 namespace blink { 22 class WebURL; 23 class WebServiceWorkerProviderClient; 24 } 25 26 namespace content { 27 28 class ServiceWorkerProviderContext; 29 30 // This class corresponds to one ServiceWorkerContainer interface in 31 // JS context (i.e. navigator.serviceWorker). 32 class CONTENT_EXPORT WebServiceWorkerProviderImpl 33 : public blink::WebServiceWorkerProvider { 34 public: 35 explicit WebServiceWorkerProviderImpl(ServiceWorkerProviderContext* context); 36 ~WebServiceWorkerProviderImpl() override; 37 38 void SetClient(blink::WebServiceWorkerProviderClient* client) override; 39 40 // blink::WebServiceWorkerProvider implementation. 41 void RegisterServiceWorker( 42 const blink::WebURL& web_pattern, 43 const blink::WebURL& web_script_url, 44 blink::mojom::ScriptType script_type, 45 blink::mojom::ServiceWorkerUpdateViaCache update_via_cache, 46 const blink::WebFetchClientSettingsObject& fetch_client_settings_object, 47 std::unique_ptr<WebServiceWorkerRegistrationCallbacks>) override; 48 void GetRegistration( 49 const blink::WebURL& web_document_url, 50 std::unique_ptr<WebServiceWorkerGetRegistrationCallbacks>) override; 51 void GetRegistrations( 52 std::unique_ptr<WebServiceWorkerGetRegistrationsCallbacks>) override; 53 void GetRegistrationForReady(GetRegistrationForReadyCallback) override; 54 bool ValidateScopeAndScriptURL(const blink::WebURL& pattern, 55 const blink::WebURL& script_url, 56 blink::WebString* error_message) override; 57 // Sets the ServiceWorkerContainer#controller for this provider. 58 void SetController(blink::mojom::ServiceWorkerObjectInfoPtr controller, 59 const std::set<blink::mojom::WebFeature>& features, 60 bool should_notify_controller_change); 61 // Posts a message to the ServiceWorkerContainer for this provider. 62 // Corresponds to Client#postMessage(). 63 void PostMessageToClient(blink::mojom::ServiceWorkerObjectInfoPtr source, 64 blink::TransferableMessage message); 65 // For UseCounter purposes. Called when the controller service worker used a 66 // feature. It is counted as if it were a feature usage from the page. 67 void CountFeature(blink::mojom::WebFeature feature); 68 69 private: 70 void OnRegistered( 71 std::unique_ptr<WebServiceWorkerRegistrationCallbacks> callbacks, 72 blink::mojom::ServiceWorkerErrorType error, 73 const base::Optional<std::string>& error_msg, 74 blink::mojom::ServiceWorkerRegistrationObjectInfoPtr registration); 75 76 void OnDidGetRegistration( 77 std::unique_ptr<WebServiceWorkerGetRegistrationCallbacks> callbacks, 78 blink::mojom::ServiceWorkerErrorType error, 79 const base::Optional<std::string>& error_msg, 80 blink::mojom::ServiceWorkerRegistrationObjectInfoPtr registration); 81 82 void OnDidGetRegistrations( 83 std::unique_ptr<WebServiceWorkerGetRegistrationsCallbacks> callbacks, 84 blink::mojom::ServiceWorkerErrorType error, 85 const base::Optional<std::string>& error_msg, 86 base::Optional< 87 std::vector<blink::mojom::ServiceWorkerRegistrationObjectInfoPtr>> 88 infos); 89 90 void OnDidGetRegistrationForReady( 91 GetRegistrationForReadyCallback callback, 92 blink::mojom::ServiceWorkerRegistrationObjectInfoPtr registration); 93 94 scoped_refptr<ServiceWorkerProviderContext> context_; 95 96 // |provider_client_| is implemented by blink::SWContainer and this pointer's 97 // nullified when its execution context is destroyed. (|this| is attached to 98 // the same context, but could live longer until the context is GC'ed) 99 blink::WebServiceWorkerProviderClient* provider_client_; 100 101 base::WeakPtrFactory<WebServiceWorkerProviderImpl> weak_factory_{this}; 102 103 DISALLOW_COPY_AND_ASSIGN(WebServiceWorkerProviderImpl); 104 }; 105 106 } // namespace content 107 108 #endif // CONTENT_RENDERER_SERVICE_WORKER_WEB_SERVICE_WORKER_PROVIDER_IMPL_H_ 109