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