Home | History | Annotate | Download | only in ipc
      1 // Copyright (c) 2011 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 #include "ipc/ipc_test_sink.h"
      6 
      7 #include "ipc/ipc_listener.h"
      8 #include "ipc/ipc_message.h"
      9 
     10 namespace IPC {
     11 
     12 TestSink::TestSink() {
     13 }
     14 
     15 TestSink::~TestSink() {
     16 }
     17 
     18 bool TestSink::Send(Message* message) {
     19   OnMessageReceived(*message);
     20   delete message;
     21   return true;
     22 }
     23 
     24 bool TestSink::Connect() {
     25   NOTIMPLEMENTED();
     26   return false;
     27 }
     28 
     29 void TestSink::Close() {
     30   NOTIMPLEMENTED();
     31 }
     32 
     33 base::ProcessId TestSink::GetPeerPID() const {
     34   NOTIMPLEMENTED();
     35   return base::ProcessId();
     36 }
     37 
     38 base::ProcessId TestSink::GetSelfPID() const {
     39   NOTIMPLEMENTED();
     40   return base::ProcessId();
     41 }
     42 
     43 bool TestSink::OnMessageReceived(const Message& msg) {
     44   ObserverListBase<Listener>::Iterator it(filter_list_);
     45   Listener* observer;
     46   while ((observer = it.GetNext()) != NULL) {
     47     if (observer->OnMessageReceived(msg))
     48       return true;
     49   }
     50 
     51   // No filter handled the message, so store it.
     52   messages_.push_back(Message(msg));
     53   return true;
     54 }
     55 
     56 void TestSink::ClearMessages() {
     57   messages_.clear();
     58 }
     59 
     60 const Message* TestSink::GetMessageAt(size_t index) const {
     61   if (index >= messages_.size())
     62     return NULL;
     63   return &messages_[index];
     64 }
     65 
     66 const Message* TestSink::GetFirstMessageMatching(uint32 id) const {
     67   for (size_t i = 0; i < messages_.size(); i++) {
     68     if (messages_[i].type() == id)
     69       return &messages_[i];
     70   }
     71   return NULL;
     72 }
     73 
     74 const Message* TestSink::GetUniqueMessageMatching(uint32 id) const {
     75   size_t found_index = 0;
     76   size_t found_count = 0;
     77   for (size_t i = 0; i < messages_.size(); i++) {
     78     if (messages_[i].type() == id) {
     79       found_count++;
     80       found_index = i;
     81     }
     82   }
     83   if (found_count != 1)
     84     return NULL;  // Didn't find a unique one.
     85   return &messages_[found_index];
     86 }
     87 
     88 void TestSink::AddFilter(Listener* filter) {
     89   filter_list_.AddObserver(filter);
     90 }
     91 
     92 void TestSink::RemoveFilter(Listener* filter) {
     93   filter_list_.RemoveObserver(filter);
     94 }
     95 
     96 #if defined(OS_POSIX) && !defined(OS_NACL)
     97 
     98 int TestSink::GetClientFileDescriptor() const {
     99   NOTREACHED();
    100   return -1;
    101 }
    102 
    103 int TestSink::TakeClientFileDescriptor() {
    104   NOTREACHED();
    105   return -1;
    106 }
    107 
    108 #endif  // defined(OS_POSIX) && !defined(OS_NACL)
    109 
    110 }  // namespace IPC
    111