Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / shared_worker / shared_worker_host.h
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_SHARED_WORKER_SHARED_WORKER_HOST_H_
6 #define CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_
7
8 #include <list>
9 #include <vector>
10
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string16.h"
14 #include "base/time/time.h"
15 #include "content/browser/shared_worker/shared_worker_message_filter.h"
16 #include "content/browser/shared_worker/worker_document_set.h"
17
18 class GURL;
19
20 namespace IPC {
21 class Message;
22 }
23
24 namespace content {
25 class SharedWorkerMessageFilter;
26 class SharedWorkerInstance;
27
28 // The SharedWorkerHost is the interface that represents the browser side of
29 // the browser <-> worker communication channel.
30 class SharedWorkerHost {
31  public:
32   SharedWorkerHost(SharedWorkerInstance* instance,
33                    SharedWorkerMessageFilter* filter,
34                    int worker_route_id);
35   ~SharedWorkerHost();
36
37   // Sends |message| to the SharedWorker.
38   bool Send(IPC::Message* message);
39
40   // Starts the SharedWorker in the renderer process which is associated with
41   // |filter_|.
42   void Start(bool pause_on_start);
43
44   // Returns true iff the given message from a renderer process was forwarded to
45   // the worker.
46   bool FilterMessage(const IPC::Message& message,
47                      SharedWorkerMessageFilter* filter);
48
49   // Handles the shutdown of the filter. If the worker has no other client,
50   // sends TerminateWorkerContext message to shut it down.
51   void FilterShutdown(SharedWorkerMessageFilter* filter);
52
53   // Shuts down any shared workers that are no longer referenced by active
54   // documents.
55   void DocumentDetached(SharedWorkerMessageFilter* filter,
56                         unsigned long long document_id);
57
58   void WorkerContextClosed();
59   void WorkerScriptLoaded();
60   void WorkerScriptLoadFailed();
61   void WorkerConnected(int message_port_id);
62   void WorkerContextDestroyed();
63   void AllowDatabase(const GURL& url,
64                      const base::string16& name,
65                      const base::string16& display_name,
66                      unsigned long estimated_size,
67                      bool* result);
68   void AllowFileSystem(const GURL& url, scoped_ptr<IPC::Message> reply_msg);
69   void AllowIndexedDB(const GURL& url,
70                       const base::string16& name,
71                       bool* result);
72
73   // Terminates the given worker, i.e. based on a UI action.
74   void TerminateWorker();
75
76   void AddFilter(SharedWorkerMessageFilter* filter, int route_id);
77
78   SharedWorkerInstance* instance() { return instance_.get(); }
79   WorkerDocumentSet* worker_document_set() const {
80     return worker_document_set_.get();
81   }
82   SharedWorkerMessageFilter* container_render_filter() const {
83     return container_render_filter_;
84   }
85   int process_id() const { return worker_process_id_; }
86   int worker_route_id() const { return worker_route_id_; }
87   bool load_failed() const { return load_failed_; }
88   bool closed() const { return closed_; }
89
90  private:
91   // Unique identifier for a worker client.
92   class FilterInfo {
93    public:
94     FilterInfo(SharedWorkerMessageFilter* filter, int route_id)
95         : filter_(filter), route_id_(route_id), message_port_id_(0) {}
96     SharedWorkerMessageFilter* filter() const { return filter_; }
97     int route_id() const { return route_id_; }
98     int message_port_id() const { return message_port_id_; }
99     void set_message_port_id(int id) { message_port_id_ = id; }
100
101    private:
102     SharedWorkerMessageFilter* filter_;
103     int route_id_;
104     int message_port_id_;
105   };
106
107   typedef std::list<FilterInfo> FilterList;
108
109   // Relays |message| to the SharedWorker. Takes care of parsing the message if
110   // it contains a message port and sending it a valid route id.
111   void RelayMessage(const IPC::Message& message,
112                     SharedWorkerMessageFilter* incoming_filter);
113
114   // Return a vector of all the render process/render frame IDs.
115   std::vector<std::pair<int, int> > GetRenderFrameIDsForWorker();
116
117   void RemoveFilters(SharedWorkerMessageFilter* filter);
118   bool HasFilter(SharedWorkerMessageFilter* filter, int route_id) const;
119   void SetMessagePortID(SharedWorkerMessageFilter* filter,
120                         int route_id,
121                         int message_port_id);
122   void AllowFileSystemResponse(scoped_ptr<IPC::Message> reply_msg,
123                                bool allowed);
124   scoped_ptr<SharedWorkerInstance> instance_;
125   scoped_refptr<WorkerDocumentSet> worker_document_set_;
126   base::WeakPtrFactory<SharedWorkerHost> weak_factory_;
127   FilterList filters_;
128   SharedWorkerMessageFilter* container_render_filter_;
129   int worker_process_id_;
130   int worker_route_id_;
131   bool load_failed_;
132   bool closed_;
133   const base::TimeTicks creation_time_;
134   DISALLOW_COPY_AND_ASSIGN(SharedWorkerHost);
135 };
136 }  // namespace content
137
138 #endif  // CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_