Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / embedded_worker_registry.cc
index d939cfb..98fe608 100644 (file)
@@ -8,6 +8,7 @@
 #include "content/browser/service_worker/embedded_worker_instance.h"
 #include "content/browser/service_worker/service_worker_context_core.h"
 #include "content/common/service_worker/embedded_worker_messages.h"
+#include "content/common/service_worker/service_worker_messages.h"
 #include "ipc/ipc_message.h"
 #include "ipc/ipc_sender.h"
 
@@ -25,7 +26,7 @@ scoped_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() {
   return worker.Pass();
 }
 
-bool EmbeddedWorkerRegistry::StartWorker(
+ServiceWorkerStatusCode EmbeddedWorkerRegistry::StartWorker(
     int process_id,
     int embedded_worker_id,
     int64 service_worker_version_id,
@@ -36,21 +37,22 @@ bool EmbeddedWorkerRegistry::StartWorker(
                                                script_url));
 }
 
-bool EmbeddedWorkerRegistry::StopWorker(int process_id,
-                                        int embedded_worker_id) {
+ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker(
+    int process_id, int embedded_worker_id) {
   return Send(process_id,
               new EmbeddedWorkerMsg_StopWorker(embedded_worker_id));
 }
 
 void EmbeddedWorkerRegistry::OnWorkerStarted(
     int process_id, int thread_id, int embedded_worker_id) {
-  DCHECK(!ContainsKey(worker_process_map_, process_id));
+  DCHECK(!ContainsKey(worker_process_map_, process_id) ||
+         worker_process_map_[process_id].count(embedded_worker_id) == 0);
   WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
   if (found == worker_map_.end()) {
     LOG(ERROR) << "Worker " << embedded_worker_id << " not registered";
     return;
   }
-  worker_process_map_[process_id] = embedded_worker_id;
+  worker_process_map_[process_id].insert(embedded_worker_id);
   DCHECK_EQ(found->second->process_id(), process_id);
   found->second->OnStarted(thread_id);
 }
@@ -63,21 +65,25 @@ void EmbeddedWorkerRegistry::OnWorkerStopped(
     return;
   }
   DCHECK_EQ(found->second->process_id(), process_id);
-  worker_process_map_.erase(process_id);
+  worker_process_map_[process_id].erase(embedded_worker_id);
   found->second->OnStopped();
 }
 
 void EmbeddedWorkerRegistry::OnSendMessageToBrowser(
-    int embedded_worker_id, const IPC::Message& message) {
+    int embedded_worker_id, int request_id, const IPC::Message& message) {
   WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
   if (found == worker_map_.end()) {
     LOG(ERROR) << "Worker " << embedded_worker_id << " not registered";
     return;
   }
-  // TODO(kinuko): Filter out unexpected messages here and uncomment below
-  // when we actually define messages that are to be sent from child process
-  // to the browser via this channel. (We don't have any yet)
-  // found->second->OnMessageReceived(message);
+  // Perform security check to filter out any unexpected (and non-test)
+  // messages. This must list up all message types that can go through here.
+  if (message.type() == ServiceWorkerHostMsg_InstallEventFinished::ID ||
+      IPC_MESSAGE_CLASS(message) == TestMsgStart) {
+    found->second->OnMessageReceived(request_id, message);
+    return;
+  }
+  NOTREACHED() << "Got unexpected message: " << message.type();
 }
 
 void EmbeddedWorkerRegistry::AddChildProcessSender(
@@ -88,11 +94,17 @@ void EmbeddedWorkerRegistry::AddChildProcessSender(
 
 void EmbeddedWorkerRegistry::RemoveChildProcessSender(int process_id) {
   process_sender_map_.erase(process_id);
-  std::map<int, int>::iterator found = worker_process_map_.find(process_id);
+  std::map<int, std::set<int> >::iterator found =
+      worker_process_map_.find(process_id);
   if (found != worker_process_map_.end()) {
-    int embedded_worker_id = found->second;
-    DCHECK(ContainsKey(worker_map_, embedded_worker_id));
-    worker_map_[embedded_worker_id]->OnStopped();
+    const std::set<int>& worker_set = worker_process_map_[process_id];
+    for (std::set<int>::const_iterator it = worker_set.begin();
+         it != worker_set.end();
+         ++it) {
+      int embedded_worker_id = *it;
+      DCHECK(ContainsKey(worker_map_, embedded_worker_id));
+      worker_map_[embedded_worker_id]->OnStopped();
+    }
     worker_process_map_.erase(found);
   }
 }
@@ -107,13 +119,16 @@ EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker(
 
 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {}
 
-bool EmbeddedWorkerRegistry::Send(int process_id, IPC::Message* message) {
+ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send(
+    int process_id, IPC::Message* message) {
   if (!context_)
-    return false;
+    return SERVICE_WORKER_ERROR_ABORT;
   ProcessToSenderMap::iterator found = process_sender_map_.find(process_id);
   if (found == process_sender_map_.end())
-    return false;
-  return found->second->Send(message);
+    return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND;
+  if (!found->second->Send(message))
+    return SERVICE_WORKER_ERROR_IPC_FAILED;
+  return SERVICE_WORKER_OK;
 }
 
 void EmbeddedWorkerRegistry::RemoveWorker(int process_id,