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.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/common/content_export.h"
14 #include "webkit/common/resource_type.h"
22 class ServiceWorkerContextCore;
23 class ServiceWorkerDispatcherHost;
24 class ServiceWorkerRequestHandler;
25 class ServiceWorkerVersion;
27 // This class is the browser-process representation of a serice worker
28 // provider. There is a provider per document and the lifetime of this
29 // object is tied to the lifetime of its document in the renderer process.
30 // This class holds service worker state that is scoped to an individual
33 // Note this class can also host a running service worker, in which
34 // case it will observe resource loads made directly by the service worker.
35 class CONTENT_EXPORT ServiceWorkerProviderHost
36 : public base::SupportsWeakPtr<ServiceWorkerProviderHost> {
38 ServiceWorkerProviderHost(int process_id,
40 base::WeakPtr<ServiceWorkerContextCore> context,
41 ServiceWorkerDispatcherHost* dispatcher_host);
42 ~ServiceWorkerProviderHost();
44 int process_id() const { return process_id_; }
45 int provider_id() const { return provider_id_; }
47 bool IsHostToRunningServiceWorker() {
48 return running_hosted_version_ != NULL;
51 // The service worker version that corresponds with
52 // navigator.serviceWorker.active for our document.
53 ServiceWorkerVersion* active_version() const {
54 return active_version_.get();
57 // The service worker version that corresponds with
58 // navigate.serviceWorker.pending for our document.
59 ServiceWorkerVersion* pending_version() const {
60 return pending_version_.get();
63 // The running version, if any, that this provider is providing resource
65 ServiceWorkerVersion* running_hosted_version() const {
66 return running_hosted_version_.get();
69 void set_document_url(const GURL& url) { document_url_ = url; }
70 const GURL& document_url() const { return document_url_; }
72 // Associate |version| to this provider as its '.active' or '.pending'
74 // Giving NULL to this method will unset the corresponding field.
75 void SetActiveVersion(ServiceWorkerVersion* version);
76 void SetPendingVersion(ServiceWorkerVersion* version);
78 // Returns false if the version is not in the expected STARTING in our
79 // our process state. That would be indicative of a bad IPC message.
80 bool SetHostedVersionId(int64 versions_id);
82 // Returns a handler for a request, the handler may return NULL if
83 // the request doesn't require special handling.
84 scoped_ptr<ServiceWorkerRequestHandler> CreateRequestHandler(
85 ResourceType::Type resource_type);
87 // Dispatches message event to the document.
88 void PostMessage(const base::string16& message,
89 const std::vector<int>& sent_message_port_ids);
92 const int process_id_;
93 const int provider_id_;
95 scoped_refptr<ServiceWorkerVersion> active_version_;
96 scoped_refptr<ServiceWorkerVersion> pending_version_;
97 scoped_refptr<ServiceWorkerVersion> running_hosted_version_;
98 base::WeakPtr<ServiceWorkerContextCore> context_;
99 ServiceWorkerDispatcherHost* dispatcher_host_;
101 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost);
104 } // namespace content
106 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_