<commit_before>// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/public/cpp/bindings/lib/router.h"

namespace mojo {
namespace internal {

// ----------------------------------------------------------------------------

class ResponderThunk : public MessageReceiver {
 public:
  explicit ResponderThunk(const SharedData<Router*>& router)
      : router_(router) {
  }
  virtual ~ResponderThunk() {
  }

  // MessageReceiver implementation:
  virtual bool Accept(Message* message) MOJO_OVERRIDE {
    assert(message->has_flag(kMessageIsResponse));

    bool result = false;

    Router* router = router_.value();
    if (router)
      result = router->Accept(message);

    return result;
  }

  virtual bool AcceptWithResponder(Message* message,
                                   MessageReceiver* responder) MOJO_OVERRIDE {
    assert(false);  // not reached!
    return false;
  }

 private:
  SharedData<Router*> router_;
};

// ----------------------------------------------------------------------------

Router::HandleIncomingMessageThunk::HandleIncomingMessageThunk(Router* router)
    : router_(router) {
}

Router::HandleIncomingMessageThunk::~HandleIncomingMessageThunk() {
}

bool Router::HandleIncomingMessageThunk::Accept(Message* message) {
  return router_->HandleIncomingMessage(message);
}

bool Router::HandleIncomingMessageThunk::AcceptWithResponder(
    Message* message,
    MessageReceiver* responder) {
  assert(false);  // not reached!
  return false;
}

// ----------------------------------------------------------------------------

Router::Router(ScopedMessagePipeHandle message_pipe, MojoAsyncWaiter* waiter)
    : connector_(message_pipe.Pass(), waiter),
      weak_self_(this),
      incoming_receiver_(NULL),
      thunk_(this),
      next_request_id_(0) {
  connector_.set_incoming_receiver(&thunk_);
}

Router::~Router() {
  weak_self_.set_value(NULL);
}

bool Router::Accept(Message* message) {
  assert(!message->has_flag(kMessageExpectsResponse));
  return connector_.Accept(message);
}

bool Router::AcceptWithResponder(Message* message,
                                 MessageReceiver* responder) {
  assert(message->has_flag(kMessageExpectsResponse));
<commit_msg>Delete remaining responders when Router is destructed.<commit_after>// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/public/cpp/bindings/lib/router.h"

namespace mojo {
namespace internal {

// ----------------------------------------------------------------------------

class ResponderThunk : public MessageReceiver {
 public:
  explicit ResponderThunk(const SharedData<Router*>& router)
      : router_(router) {
  }
  virtual ~ResponderThunk() {
  }

  // MessageReceiver implementation:
  virtual bool Accept(Message* message) MOJO_OVERRIDE {
    assert(message->has_flag(kMessageIsResponse));

    bool result = false;

    Router* router = router_.value();
    if (router)
      result = router->Accept(message);

    return result;
  }

  virtual bool AcceptWithResponder(Message* message,
                                   MessageReceiver* responder) MOJO_OVERRIDE {
    assert(false);  // not reached!
    return false;
  }

 private:
  SharedData<Router*> router_;
};

// ----------------------------------------------------------------------------

Router::HandleIncomingMessageThunk::HandleIncomingMessageThunk(Router* router)
    : router_(router) {
}

Router::HandleIncomingMessageThunk::~HandleIncomingMessageThunk() {
}

bool Router::HandleIncomingMessageThunk::Accept(Message* message) {
  return router_->HandleIncomingMessage(message);
}

bool Router::HandleIncomingMessageThunk::AcceptWithResponder(
    Message* message,
    MessageReceiver* responder) {
  assert(false);  // not reached!
  return false;
}

// ----------------------------------------------------------------------------

Router::Router(ScopedMessagePipeHandle message_pipe, MojoAsyncWaiter* waiter)
    : connector_(message_pipe.Pass(), waiter),
      weak_self_(this),
      incoming_receiver_(NULL),
      thunk_(this),
      next_request_id_(0) {
  connector_.set_incoming_receiver(&thunk_);
}

Router::~Router() {
  weak_self_.set_value(NULL);

  for (ResponderMap::const_iterator i = responders_.begin();
       i != responders_.end(); ++i) {
    delete i->second;
  }
}

bool Router::Accept(Message* message) {
  assert(!message->has_flag(kMessageExpectsResponse));
  return connector_.Accept(message);
}

bool Router::AcceptWithResponder(Message* message,
                                 MessageReceiver* responder) {
  assert(message->has_flag(kMessageExpectsResponse));

{'fraction_non_alphanumeric': 0.19487497416821659, 'fraction_numerical': 0.0020665426741062203, 'mean_word_length': 2.6390977443609023, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3938324', 'n_tokens_mistral': 1343, 'n_tokens_neox': 1289, 'n_words': 267}