| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback_forward.h" | |
| 14 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" | 14 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 18 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
| 19 #include "content/common/content_export.h" | 18 #include "content/common/content_export.h" |
| 20 #include "content/common/service_worker/service_worker_status_code.h" | 19 #include "content/common/service_worker/service_worker_status_code.h" |
| 21 | 20 |
| 22 class GURL; | 21 class GURL; |
| 23 | 22 |
| 24 namespace IPC { | 23 namespace IPC { |
| 25 class Message; | 24 class Message; |
| 26 class Sender; | |
| 27 } | 25 } |
| 28 | 26 |
| 29 namespace content { | 27 namespace content { |
| 30 | 28 |
| 31 class EmbeddedWorkerInstance; | 29 class EmbeddedWorkerInstance; |
| 32 class ServiceWorkerContextCore; | 30 class ServiceWorkerContextCore; |
| 33 | 31 |
| 34 // Acts as a thin stub between MessageFilter and each EmbeddedWorkerInstance, | 32 // Acts as a thin stub between MessageFilter and each EmbeddedWorkerInstance, |
| 35 // which sends/receives messages to/from each EmbeddedWorker in child process. | 33 // which sends/receives messages to/from each EmbeddedWorker in child process. |
| 36 // | 34 // |
| 37 // Hangs off ServiceWorkerContextCore (its reference is also held by each | 35 // Hangs off ServiceWorkerContextCore (its reference is also held by each |
| 38 // EmbeddedWorkerInstance). Operated only on IO thread. | 36 // EmbeddedWorkerInstance). Operated only on IO thread. |
| 39 class CONTENT_EXPORT EmbeddedWorkerRegistry | 37 class CONTENT_EXPORT EmbeddedWorkerRegistry |
| 40 : public NON_EXPORTED_BASE(base::RefCounted<EmbeddedWorkerRegistry>) { | 38 : public NON_EXPORTED_BASE(base::RefCounted<EmbeddedWorkerRegistry>) { |
| 41 public: | 39 public: |
| 42 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; | |
| 43 | |
| 44 static scoped_refptr<EmbeddedWorkerRegistry> Create( | 40 static scoped_refptr<EmbeddedWorkerRegistry> Create( |
| 45 const base::WeakPtr<ServiceWorkerContextCore>& contxet); | 41 const base::WeakPtr<ServiceWorkerContextCore>& contxet); |
| 46 | 42 |
| 47 // Used for DeleteAndStartOver. Creates a new registry which takes over | 43 // Used for DeleteAndStartOver. Creates a new registry which takes over |
| 48 // |next_embedded_worker_id_| and |process_sender_map_| from |old_registry|. | 44 // |next_embedded_worker_id_| and |process_sender_map_| from |old_registry|. |
| 49 static scoped_refptr<EmbeddedWorkerRegistry> Create( | 45 static scoped_refptr<EmbeddedWorkerRegistry> Create( |
| 50 const base::WeakPtr<ServiceWorkerContextCore>& context, | 46 const base::WeakPtr<ServiceWorkerContextCore>& context, |
| 51 EmbeddedWorkerRegistry* old_registry); | 47 EmbeddedWorkerRegistry* old_registry); |
| 52 | 48 |
| 53 bool OnMessageReceived(const IPC::Message& message, int process_id); | 49 bool OnMessageReceived(const IPC::Message& message, int process_id); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 81 int line_number, | 77 int line_number, |
| 82 int column_number, | 78 int column_number, |
| 83 const GURL& source_url); | 79 const GURL& source_url); |
| 84 void OnReportConsoleMessage(int embedded_worker_id, | 80 void OnReportConsoleMessage(int embedded_worker_id, |
| 85 int source_identifier, | 81 int source_identifier, |
| 86 int message_level, | 82 int message_level, |
| 87 const base::string16& message, | 83 const base::string16& message, |
| 88 int line_number, | 84 int line_number, |
| 89 const GURL& source_url); | 85 const GURL& source_url); |
| 90 | 86 |
| 91 // Keeps a map from process_id to sender information. | 87 // Removes information about the service workers running on the process and |
| 92 void AddChildProcessSender(int process_id, IPC::Sender* sender); | 88 // calls ServiceWorkerVersion::OnDetached() on each. Called when the process |
| 93 void RemoveChildProcessSender(int process_id); | 89 // is terminated. Under normal operation, the workers should already have |
| 90 // been stopped before the process is terminated, in which case this function |
| 91 // does nothing. But in some cases the process can be terminated unexpectedly |
| 92 // or the workers can fail to stop cleanly. |
| 93 void RemoveProcess(int process_id); |
| 94 | 94 |
| 95 // Returns an embedded worker instance for given |embedded_worker_id|. | 95 // Returns an embedded worker instance for given |embedded_worker_id|. |
| 96 EmbeddedWorkerInstance* GetWorker(int embedded_worker_id); | 96 EmbeddedWorkerInstance* GetWorker(int embedded_worker_id); |
| 97 | 97 |
| 98 // Returns true if |embedded_worker_id| is managed by this registry. | 98 // Returns true if |embedded_worker_id| is managed by this registry. |
| 99 bool CanHandle(int embedded_worker_id) const; | 99 bool CanHandle(int embedded_worker_id) const; |
| 100 | 100 |
| 101 private: | 101 private: |
| 102 friend class base::RefCounted<EmbeddedWorkerRegistry>; | 102 friend class base::RefCounted<EmbeddedWorkerRegistry>; |
| 103 friend class MojoEmbeddedWorkerInstanceTest; | 103 friend class MojoEmbeddedWorkerInstanceTest; |
| 104 friend class EmbeddedWorkerInstance; | 104 friend class EmbeddedWorkerInstance; |
| 105 friend class EmbeddedWorkerInstanceTest; | 105 friend class EmbeddedWorkerInstanceTest; |
| 106 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, | 106 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, |
| 107 RemoveWorkerInSharedProcess); | 107 RemoveWorkerInSharedProcess); |
| 108 | 108 |
| 109 using WorkerInstanceMap = std::map<int, EmbeddedWorkerInstance*>; | 109 using WorkerInstanceMap = std::map<int, EmbeddedWorkerInstance*>; |
| 110 using ProcessToSenderMap = std::map<int, IPC::Sender*>; | |
| 111 | 110 |
| 112 EmbeddedWorkerRegistry( | 111 EmbeddedWorkerRegistry( |
| 113 const base::WeakPtr<ServiceWorkerContextCore>& context, | 112 const base::WeakPtr<ServiceWorkerContextCore>& context, |
| 114 int initial_embedded_worker_id); | 113 int initial_embedded_worker_id); |
| 115 ~EmbeddedWorkerRegistry(); | 114 ~EmbeddedWorkerRegistry(); |
| 116 | 115 |
| 117 ServiceWorkerStatusCode Send(int process_id, IPC::Message* message); | 116 ServiceWorkerStatusCode Send(int process_id, IPC::Message* message); |
| 118 | 117 |
| 119 // Called when EmbeddedWorkerInstance is ready for IPC. This function | 118 // Called when EmbeddedWorkerInstance is ready for IPC. This function |
| 120 // prepares a route to the child worker thread. | 119 // prepares a route to the child worker thread. |
| 121 // TODO(shimazu): Remove this function once mojofication is completed. | 120 // TODO(shimazu): Remove this function once mojofication is completed. |
| 122 void BindWorkerToProcess(int process_id, int embedded_worker_id); | 121 void BindWorkerToProcess(int process_id, int embedded_worker_id); |
| 123 | 122 |
| 124 // RemoveWorker is called when EmbeddedWorkerInstance is destructed. | 123 // RemoveWorker is called when EmbeddedWorkerInstance is destructed. |
| 125 // |process_id| could be invalid (i.e. ChildProcessHost::kInvalidUniqueID) | 124 // |process_id| could be invalid (i.e. ChildProcessHost::kInvalidUniqueID) |
| 126 // if it's not running. | 125 // if it's not running. |
| 127 void RemoveWorker(int process_id, int embedded_worker_id); | 126 void RemoveWorker(int process_id, int embedded_worker_id); |
| 128 // DetachWorker is called when EmbeddedWorkerInstance releases a process. | 127 // DetachWorker is called when EmbeddedWorkerInstance releases a process. |
| 129 // |process_id| could be invalid (i.e. ChildProcessHost::kInvalidUniqueID) | 128 // |process_id| could be invalid (i.e. ChildProcessHost::kInvalidUniqueID) |
| 130 // if it's not running. | 129 // if it's not running. |
| 131 void DetachWorker(int process_id, int embedded_worker_id); | 130 void DetachWorker(int process_id, int embedded_worker_id); |
| 132 | 131 |
| 133 EmbeddedWorkerInstance* GetWorkerForMessage(int process_id, | 132 EmbeddedWorkerInstance* GetWorkerForMessage(int process_id, |
| 134 int embedded_worker_id); | 133 int embedded_worker_id); |
| 135 | 134 |
| 136 base::WeakPtr<ServiceWorkerContextCore> context_; | 135 base::WeakPtr<ServiceWorkerContextCore> context_; |
| 137 | 136 |
| 138 WorkerInstanceMap worker_map_; | 137 WorkerInstanceMap worker_map_; |
| 139 ProcessToSenderMap process_sender_map_; | |
| 140 | 138 |
| 141 // Map from process_id to embedded_worker_id. | 139 // Map from process_id to embedded_worker_id. |
| 142 // This map only contains starting and running workers. | 140 // This map only contains starting and running workers. |
| 143 std::map<int, std::set<int> > worker_process_map_; | 141 std::map<int, std::set<int> > worker_process_map_; |
| 144 | 142 |
| 145 int next_embedded_worker_id_; | 143 int next_embedded_worker_id_; |
| 146 const int initial_embedded_worker_id_; | 144 const int initial_embedded_worker_id_; |
| 147 | 145 |
| 148 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerRegistry); | 146 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerRegistry); |
| 149 }; | 147 }; |
| 150 | 148 |
| 151 } // namespace content | 149 } // namespace content |
| 152 | 150 |
| 153 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_ | 151 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_ |
| OLD | NEW |