Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_provider_host.h
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_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
7
8 #include <set>
9 #include <vector>
10
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"
15
16 namespace IPC {
17 class Sender;
18 }
19
20 namespace content {
21
22 class ServiceWorkerContextCore;
23 class ServiceWorkerDispatcherHost;
24 class ServiceWorkerRequestHandler;
25 class ServiceWorkerVersion;
26
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
31 // document.
32 //
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> {
37  public:
38   ServiceWorkerProviderHost(int process_id,
39                             int provider_id,
40                             base::WeakPtr<ServiceWorkerContextCore> context,
41                             ServiceWorkerDispatcherHost* dispatcher_host);
42   ~ServiceWorkerProviderHost();
43
44   int process_id() const { return process_id_; }
45   int provider_id() const { return provider_id_; }
46
47   bool IsHostToRunningServiceWorker() {
48     return running_hosted_version_ != NULL;
49   }
50
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();
55   }
56
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();
61   }
62
63   // The running version, if any, that this provider is providing resource
64   // loads for.
65   ServiceWorkerVersion* running_hosted_version() const {
66     return running_hosted_version_.get();
67   }
68
69   void set_document_url(const GURL& url) { document_url_ = url; }
70   const GURL& document_url() const { return document_url_; }
71
72   // Associate |version| to this provider as its '.active' or '.pending'
73   // version.
74   // Giving NULL to this method will unset the corresponding field.
75   void SetActiveVersion(ServiceWorkerVersion* version);
76   void SetPendingVersion(ServiceWorkerVersion* version);
77
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);
81
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);
86
87   // Dispatches message event to the document.
88   void PostMessage(const base::string16& message,
89                    const std::vector<int>& sent_message_port_ids);
90
91  private:
92   const int process_id_;
93   const int provider_id_;
94   GURL document_url_;
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_;
100
101   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost);
102 };
103
104 }  // namespace content
105
106 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_