| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_ | 5 #ifndef CHROME_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_ |
| 6 #define CHROME_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_ | 6 #define CHROME_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/task.h" | 11 #include "base/task.h" |
| 12 #include "chrome/browser/worker_host/worker_document_set.h" |
| 12 #include "chrome/common/child_process_host.h" | 13 #include "chrome/common/child_process_host.h" |
| 13 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 14 #include "ipc/ipc_channel.h" | 15 #include "ipc/ipc_channel.h" |
| 15 | 16 |
| 17 struct ViewHostMsg_CreateWorker_Params; |
| 18 |
| 16 class WorkerProcessHost : public ChildProcessHost { | 19 class WorkerProcessHost : public ChildProcessHost { |
| 17 public: | 20 public: |
| 21 |
| 18 // Contains information about each worker instance, needed to forward messages | 22 // Contains information about each worker instance, needed to forward messages |
| 19 // between the renderer and worker processes. | 23 // between the renderer and worker processes. |
| 20 class WorkerInstance { | 24 class WorkerInstance { |
| 21 public: | 25 public: |
| 22 WorkerInstance(const GURL& url, | 26 WorkerInstance(const GURL& url, |
| 23 bool shared, | 27 bool shared, |
| 24 bool off_the_record, | 28 bool off_the_record, |
| 25 const string16& name, | 29 const string16& name, |
| 26 int renderer_id, | |
| 27 int render_view_route_id, | |
| 28 int worker_route_id); | 30 int worker_route_id); |
| 29 | 31 |
| 30 // Unique identifier for a worker client. | 32 // Unique identifier for a worker client. |
| 31 typedef std::pair<IPC::Message::Sender*, int> SenderInfo; | 33 typedef std::pair<IPC::Message::Sender*, int> SenderInfo; |
| 32 | 34 |
| 33 // APIs to manage the sender list for a given instance. | 35 // APIs to manage the sender list for a given instance. |
| 34 void AddSender(IPC::Message::Sender* sender, int sender_route_id); | 36 void AddSender(IPC::Message::Sender* sender, int sender_route_id); |
| 35 void RemoveSender(IPC::Message::Sender* sender, int sender_route_id); | 37 void RemoveSender(IPC::Message::Sender* sender, int sender_route_id); |
| 36 void RemoveSenders(IPC::Message::Sender* sender); | 38 void RemoveSenders(IPC::Message::Sender* sender); |
| 37 bool HasSender(IPC::Message::Sender* sender, int sender_route_id) const; | 39 bool HasSender(IPC::Message::Sender* sender, int sender_route_id) const; |
| 40 bool RendererIsParent(int renderer_id, int render_view_route_id) const; |
| 38 int NumSenders() const { return senders_.size(); } | 41 int NumSenders() const { return senders_.size(); } |
| 39 // Returns the single sender (must only be one). | 42 // Returns the single sender (must only be one). |
| 40 SenderInfo GetSender() const; | 43 SenderInfo GetSender() const; |
| 41 | 44 |
| 42 // Checks if this WorkerInstance matches the passed url/name params | 45 // Checks if this WorkerInstance matches the passed url/name params |
| 43 // (per the comparison algorithm in the WebWorkers spec). This API only | 46 // (per the comparison algorithm in the WebWorkers spec). This API only |
| 44 // applies to shared workers. | 47 // applies to shared workers. |
| 45 bool Matches( | 48 bool Matches( |
| 46 const GURL& url, const string16& name, bool off_the_record) const; | 49 const GURL& url, const string16& name, bool off_the_record) const; |
| 47 | 50 |
| 48 // Adds a document to a shared worker's document set. | 51 // Shares the passed instance's WorkerDocumentSet with this instance. This |
| 49 void AddToDocumentSet(IPC::Message::Sender* parent, | 52 // instance's current WorkerDocumentSet is dereferenced (and freed if this |
| 50 unsigned long long document_id); | 53 // is the only reference) as a result. |
| 51 | 54 void ShareDocumentSet(const WorkerInstance& instance) { |
| 52 // Checks to see if a document is in a shared worker's document set. | 55 worker_document_set_ = instance.worker_document_set_; |
| 53 bool IsInDocumentSet(IPC::Message::Sender* parent, | |
| 54 unsigned long long document_id) const; | |
| 55 | |
| 56 // Removes a specific document from a shared worker's document set when | |
| 57 // that document is detached. | |
| 58 void RemoveFromDocumentSet(IPC::Message::Sender* parent, | |
| 59 unsigned long long document_id); | |
| 60 | |
| 61 // Copies the document set from one instance to another | |
| 62 void CopyDocumentSet(const WorkerInstance& instance) { | |
| 63 document_set_ = instance.document_set_; | |
| 64 }; | 56 }; |
| 65 | 57 |
| 66 // Invoked when a render process exits, to remove all associated documents | |
| 67 // from a shared worker's document set. | |
| 68 void RemoveAllAssociatedDocuments(IPC::Message::Sender* parent); | |
| 69 | |
| 70 bool IsDocumentSetEmpty() const { return document_set_.empty(); } | |
| 71 | |
| 72 | |
| 73 // Accessors | 58 // Accessors |
| 74 bool shared() const { return shared_; } | 59 bool shared() const { return shared_; } |
| 75 bool off_the_record() const { return off_the_record_; } | 60 bool off_the_record() const { return off_the_record_; } |
| 76 bool closed() const { return closed_; } | 61 bool closed() const { return closed_; } |
| 77 void set_closed(bool closed) { closed_ = closed; } | 62 void set_closed(bool closed) { closed_ = closed; } |
| 78 const GURL& url() const { return url_; } | 63 const GURL& url() const { return url_; } |
| 79 const string16 name() const { return name_; } | 64 const string16 name() const { return name_; } |
| 80 int renderer_id() const { return renderer_id_; } | |
| 81 int render_view_route_id() const { return render_view_route_id_; } | |
| 82 int worker_route_id() const { return worker_route_id_; } | 65 int worker_route_id() const { return worker_route_id_; } |
| 66 WorkerDocumentSet* worker_document_set() const { |
| 67 return worker_document_set_; |
| 68 } |
| 83 | 69 |
| 84 private: | 70 private: |
| 85 // Unique identifier for an associated document. | |
| 86 typedef std::pair<IPC::Message::Sender*, unsigned long long> DocumentInfo; | |
| 87 typedef std::list<DocumentInfo> DocumentSet; | |
| 88 // Set of all senders (clients) associated with this worker. | 71 // Set of all senders (clients) associated with this worker. |
| 89 typedef std::list<SenderInfo> SenderList; | 72 typedef std::list<SenderInfo> SenderList; |
| 90 GURL url_; | 73 GURL url_; |
| 91 bool shared_; | 74 bool shared_; |
| 92 bool off_the_record_; | 75 bool off_the_record_; |
| 93 bool closed_; | 76 bool closed_; |
| 94 string16 name_; | 77 string16 name_; |
| 95 int renderer_id_; | |
| 96 int render_view_route_id_; | |
| 97 int worker_route_id_; | 78 int worker_route_id_; |
| 98 SenderList senders_; | 79 SenderList senders_; |
| 99 DocumentSet document_set_; | 80 scoped_refptr<WorkerDocumentSet> worker_document_set_; |
| 100 }; | 81 }; |
| 101 | 82 |
| 102 explicit WorkerProcessHost(ResourceDispatcherHost* resource_dispatcher_host); | 83 explicit WorkerProcessHost(ResourceDispatcherHost* resource_dispatcher_host); |
| 103 ~WorkerProcessHost(); | 84 ~WorkerProcessHost(); |
| 104 | 85 |
| 105 // Starts the process. Returns true iff it succeeded. | 86 // Starts the process. Returns true iff it succeeded. |
| 106 bool Init(); | 87 bool Init(); |
| 107 | 88 |
| 108 // Creates a worker object in the process. | 89 // Creates a worker object in the process. |
| 109 void CreateWorker(const WorkerInstance& instance); | 90 void CreateWorker(const WorkerInstance& instance); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 135 // Called when a message arrives from the worker process. | 116 // Called when a message arrives from the worker process. |
| 136 void OnMessageReceived(const IPC::Message& message); | 117 void OnMessageReceived(const IPC::Message& message); |
| 137 | 118 |
| 138 // Called when the app invokes close() from within worker context. | 119 // Called when the app invokes close() from within worker context. |
| 139 void OnWorkerContextClosed(int worker_route_id); | 120 void OnWorkerContextClosed(int worker_route_id); |
| 140 | 121 |
| 141 // Called if a worker tries to connect to a shared worker. | 122 // Called if a worker tries to connect to a shared worker. |
| 142 void OnLookupSharedWorker(const GURL& url, | 123 void OnLookupSharedWorker(const GURL& url, |
| 143 const string16& name, | 124 const string16& name, |
| 144 unsigned long long document_id, | 125 unsigned long long document_id, |
| 126 int render_view_route_id, |
| 145 int* route_id, | 127 int* route_id, |
| 146 bool* url_error); | 128 bool* url_error); |
| 147 | 129 |
| 148 // Given a Sender, returns the callback that generates a new routing id. | 130 // Given a Sender, returns the callback that generates a new routing id. |
| 149 static CallbackWithReturnValue<int>::Type* GetNextRouteIdCallback( | 131 static CallbackWithReturnValue<int>::Type* GetNextRouteIdCallback( |
| 150 IPC::Message::Sender* sender); | 132 IPC::Message::Sender* sender); |
| 151 | 133 |
| 152 // Relays a message to the given endpoint. Takes care of parsing the message | 134 // Relays a message to the given endpoint. Takes care of parsing the message |
| 153 // if it contains a message port and sending it a valid route id. | 135 // if it contains a message port and sending it a valid route id. |
| 154 static void RelayMessage(const IPC::Message& message, | 136 static void RelayMessage(const IPC::Message& message, |
| 155 IPC::Message::Sender* sender, | 137 IPC::Message::Sender* sender, |
| 156 int route_id, | 138 int route_id, |
| 157 CallbackWithReturnValue<int>::Type* next_route_id); | 139 CallbackWithReturnValue<int>::Type* next_route_id); |
| 158 | 140 |
| 159 virtual bool CanShutdown() { return instances_.empty(); } | 141 virtual bool CanShutdown() { return instances_.empty(); } |
| 160 | 142 |
| 161 // Updates the title shown in the task manager. | 143 // Updates the title shown in the task manager. |
| 162 void UpdateTitle(); | 144 void UpdateTitle(); |
| 163 | 145 |
| 164 void OnCreateWorker(const GURL& url, | 146 void OnCreateWorker(const ViewHostMsg_CreateWorker_Params& params, |
| 165 bool shared, | |
| 166 const string16& name, | |
| 167 int render_view_route_id, | |
| 168 int* route_id); | 147 int* route_id); |
| 169 void OnCancelCreateDedicatedWorker(int route_id); | 148 void OnCancelCreateDedicatedWorker(int route_id); |
| 170 void OnForwardToWorker(const IPC::Message& message); | 149 void OnForwardToWorker(const IPC::Message& message); |
| 171 | 150 |
| 172 Instances instances_; | 151 Instances instances_; |
| 173 | 152 |
| 174 // A callback to create a routing id for the associated worker process. | 153 // A callback to create a routing id for the associated worker process. |
| 175 scoped_ptr<CallbackWithReturnValue<int>::Type> next_route_id_callback_; | 154 scoped_ptr<CallbackWithReturnValue<int>::Type> next_route_id_callback_; |
| 176 | 155 |
| 177 DISALLOW_COPY_AND_ASSIGN(WorkerProcessHost); | 156 DISALLOW_COPY_AND_ASSIGN(WorkerProcessHost); |
| 178 }; | 157 }; |
| 179 | 158 |
| 180 #endif // CHROME_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_ | 159 #endif // CHROME_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_ |
| OLD | NEW |