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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_HANDLE_H_ 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_HANDLE_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/weak_ptr.h" 12 #include "content/browser/service_worker/service_worker_version.h" 13 #include "content/common/content_export.h" 14 #include "content/common/service_worker/service_worker_types.h" 15 16 namespace IPC { 17 class Sender; 18 } 19 20 namespace content { 21 22 class ServiceWorkerContextCore; 23 class ServiceWorkerRegistration; 24 25 // Roughly corresponds to one ServiceWorker object in the renderer process 26 // (WebServiceWorkerImpl). 27 // Has references to the corresponding ServiceWorkerVersion and 28 // ServiceWorkerRegistration (therefore they're guaranteed to be alive while 29 // this handle is around). 30 class CONTENT_EXPORT ServiceWorkerHandle 31 : NON_EXPORTED_BASE(public ServiceWorkerVersion::Listener) { 32 public: 33 // Creates a handle for a live version. The version's corresponding 34 // registration must be also alive. 35 // This may return NULL if |context|.get() or |version| is NULL. 36 // |sender| and |thread_id| will be used to send messages to the 37 // corresponding WebServiceWorkerImpl (which should live on |thread_id| 38 // in the child process). 39 static scoped_ptr<ServiceWorkerHandle> Create( 40 base::WeakPtr<ServiceWorkerContextCore> context, 41 IPC::Sender* sender, 42 int thread_id, 43 int provider_id, 44 ServiceWorkerVersion* version); 45 46 ServiceWorkerHandle(base::WeakPtr<ServiceWorkerContextCore> context, 47 IPC::Sender* sender, 48 int thread_id, 49 int provider_id, 50 ServiceWorkerRegistration* registration, 51 ServiceWorkerVersion* version); 52 virtual ~ServiceWorkerHandle(); 53 54 // ServiceWorkerVersion::Listener overrides. 55 virtual void OnWorkerStarted(ServiceWorkerVersion* version) OVERRIDE; 56 virtual void OnWorkerStopped(ServiceWorkerVersion* version) OVERRIDE; 57 virtual void OnErrorReported(ServiceWorkerVersion* version, 58 const base::string16& error_message, 59 int line_number, 60 int column_number, 61 const GURL& source_url) OVERRIDE; 62 virtual void OnReportConsoleMessage(ServiceWorkerVersion* version, 63 int source_identifier, 64 int message_level, 65 const base::string16& message, 66 int line_number, 67 const GURL& source_url) OVERRIDE; 68 virtual void OnVersionStateChanged(ServiceWorkerVersion* version) OVERRIDE; 69 70 ServiceWorkerObjectInfo GetObjectInfo(); 71 72 int thread_id() const { return thread_id_; } 73 int provider_id() const { return provider_id_; } 74 int handle_id() const { return handle_id_; } 75 ServiceWorkerRegistration* registration() { return registration_.get(); } 76 ServiceWorkerVersion* version() { return version_.get(); } 77 78 bool HasNoRefCount() const { return ref_count_ <= 0; } 79 void IncrementRefCount(); 80 void DecrementRefCount(); 81 82 private: 83 base::WeakPtr<ServiceWorkerContextCore> context_; 84 IPC::Sender* sender_; // Not owned, it should always outlive this. 85 const int thread_id_; 86 const int provider_id_; 87 const int handle_id_; 88 int ref_count_; // Created with 1. 89 scoped_refptr<ServiceWorkerRegistration> registration_; 90 scoped_refptr<ServiceWorkerVersion> version_; 91 92 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerHandle); 93 }; 94 95 } // namespace content 96 97 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_HANDLE_H_ 98