1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include <set>
9010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include <vector>
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/memory/ref_counted.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/weak_ptr.h"
136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "content/browser/service_worker/service_worker_registration.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/common/content_export.h"
15f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "content/common/service_worker/service_worker_types.h"
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/common/resource_type.h"
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunamespace IPC {
19c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochclass Sender;
20c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
21c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
2203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace storage {
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class BlobStorageContext;
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace content {
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)class ResourceRequestBody;
29c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochclass ServiceWorkerContextCore;
30c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochclass ServiceWorkerDispatcherHost;
315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass ServiceWorkerRequestHandler;
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class ServiceWorkerVersion;
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
34f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// This class is the browser-process representation of a service worker
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// provider. There is a provider per document and the lifetime of this
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// object is tied to the lifetime of its document in the renderer process.
375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// This class holds service worker state that is scoped to an individual
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// document.
395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu//
405c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Note this class can also host a running service worker, in which
415c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// case it will observe resource loads made directly by the service worker.
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class CONTENT_EXPORT ServiceWorkerProviderHost
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : public NON_EXPORTED_BASE(ServiceWorkerRegistration::Listener),
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      public base::SupportsWeakPtr<ServiceWorkerProviderHost> {
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) public:
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ServiceWorkerProviderHost(int process_id,
47c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                            int provider_id,
48c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                            base::WeakPtr<ServiceWorkerContextCore> context,
49c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                            ServiceWorkerDispatcherHost* dispatcher_host);
506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual ~ServiceWorkerProviderHost();
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  int process_id() const { return process_id_; }
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  int provider_id() const { return provider_id_; }
54c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  bool IsHostToRunningServiceWorker() {
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return running_hosted_version_.get() != NULL;
575c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
585c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ServiceWorkerVersion* controlling_version() const {
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return controlling_version_.get();
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
62c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ServiceWorkerVersion* active_version() const {
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return associated_registration_.get() ?
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        associated_registration_->active_version() : NULL;
65c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ServiceWorkerVersion* waiting_version() const {
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return associated_registration_.get() ?
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        associated_registration_->waiting_version() : NULL;
690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ServiceWorkerVersion* installing_version() const {
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return associated_registration_.get() ?
721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        associated_registration_->installing_version() : NULL;
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
755c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // The running version, if any, that this provider is providing resource
765c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // loads for.
775c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  ServiceWorkerVersion* running_hosted_version() const {
785c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    return running_hosted_version_.get();
79c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
80c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
8146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void SetDocumentUrl(const GURL& url);
82c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  const GURL& document_url() const { return document_url_; }
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Associates to |registration| to listen for its version change events.
856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void AssociateRegistration(ServiceWorkerRegistration* registration);
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Clears the associated registration and stop listening to it.
881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void DisassociateRegistration();
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
90c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Returns false if the version is not in the expected STARTING in our
91f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // process state. That would be indicative of a bad IPC message.
92c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool SetHostedVersionId(int64 versions_id);
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
945c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // Returns a handler for a request, the handler may return NULL if
955c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // the request doesn't require special handling.
965c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  scoped_ptr<ServiceWorkerRequestHandler> CreateRequestHandler(
975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      ResourceType resource_type,
9803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
9903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      scoped_refptr<ResourceRequestBody> body);
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Returns true if |registration| can be associated with this provider.
1026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  bool CanAssociateRegistration(ServiceWorkerRegistration* registration);
103f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
1041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // For use by the ServiceWorkerControlleeRequestHandler to disallow
1051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // new registration association while a navigation is occurring and
1061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // an existing registration is being looked for.
1071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void SetAllowAssociation(bool allow) { allow_association_ = allow; }
1081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
109f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Returns true if the context referred to by this host (i.e. |context_|) is
110f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // still alive.
111f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  bool IsContextAlive();
112f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
113010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Dispatches message event to the document.
114010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  void PostMessage(const base::string16& message,
115010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                   const std::vector<int>& sent_message_port_ids);
116010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Adds reference of this host's process to the |pattern|, the reference will
1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // be removed in destructor.
1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void AddScopedProcessReferenceToPattern(const GURL& pattern);
1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) private:
1226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  friend class ServiceWorkerProviderHostTest;
1236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest,
1246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                           UpdateBefore24Hours);
1256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest,
1266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                           UpdateAfter24Hours);
1276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // ServiceWorkerRegistration::Listener overrides.
1296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void OnRegistrationFailed(
1306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ServiceWorkerRegistration* registration) OVERRIDE;
1316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Sets the controller version field to |version| or if |version| is NULL,
1336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // clears the field.
1346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void SetControllerVersionAttribute(ServiceWorkerVersion* version);
1356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
136f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Creates a ServiceWorkerHandle to retain |version| and returns a
137f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // ServiceWorkerInfo with the handle ID to pass to the provider. The
138f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // provider is responsible for releasing the handle.
139f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  ServiceWorkerObjectInfo CreateHandleAndPass(ServiceWorkerVersion* version);
140f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Increase/decrease this host's process reference for |pattern|.
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void IncreaseProcessReference(const GURL& pattern);
1431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void DecreaseProcessReference(const GURL& pattern);
1441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
145f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const int process_id_;
146f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const int provider_id_;
147c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  GURL document_url_;
148116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::vector<GURL> associated_patterns_;
1506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_refptr<ServiceWorkerRegistration> associated_registration_;
1516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
152116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_refptr<ServiceWorkerVersion> controlling_version_;
1535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  scoped_refptr<ServiceWorkerVersion> running_hosted_version_;
154c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  base::WeakPtr<ServiceWorkerContextCore> context_;
155c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ServiceWorkerDispatcherHost* dispatcher_host_;
1561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool allow_association_;
157c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
158c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost);
159f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
160f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
161f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace content
162f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
163f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
164