Home | History | Annotate | Download | only in shared_impl
      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
      3 // found in the LICENSE file.
      4 
      5 #include "ppapi/shared_impl/ppb_tcp_socket_shared.h"
      6 
      7 #include "base/logging.h"
      8 
      9 namespace ppapi {
     10 
     11 TCPSocketState::TCPSocketState(StateType state)
     12     : state_(state),
     13       pending_transition_(NONE) {
     14   DCHECK(state_ == INITIAL || state_ == CONNECTED);
     15 }
     16 
     17 TCPSocketState::~TCPSocketState() {
     18 }
     19 
     20 void TCPSocketState::SetPendingTransition(TransitionType pending_transition) {
     21   DCHECK(IsValidTransition(pending_transition));
     22   pending_transition_ = pending_transition;
     23 }
     24 
     25 void TCPSocketState::CompletePendingTransition(bool success) {
     26   switch (pending_transition_) {
     27     case NONE:
     28       NOTREACHED();
     29       break;
     30     case BIND:
     31       if (success)
     32         state_ = BOUND;
     33       break;
     34     case CONNECT:
     35       state_ = success ? CONNECTED : CLOSED;
     36       break;
     37     case SSL_CONNECT:
     38       state_ = success ? SSL_CONNECTED : CLOSED;
     39       break;
     40     case LISTEN:
     41       if (success)
     42         state_ = LISTENING;
     43       break;
     44     case CLOSE:
     45       state_ = CLOSED;
     46       break;
     47   }
     48   pending_transition_ = NONE;
     49 }
     50 
     51 void TCPSocketState::DoTransition(TransitionType transition, bool success) {
     52   SetPendingTransition(transition);
     53   CompletePendingTransition(success);
     54 }
     55 
     56 bool TCPSocketState::IsValidTransition(TransitionType transition) const {
     57   if (pending_transition_ != NONE && transition != CLOSE)
     58     return false;
     59 
     60   switch (transition) {
     61     case NONE:
     62       return false;
     63     case BIND:
     64       return state_ == INITIAL;
     65     case CONNECT:
     66       return state_ == INITIAL || state_ == BOUND;
     67     case SSL_CONNECT:
     68       return state_ == CONNECTED;
     69     case LISTEN:
     70       return state_ == BOUND;
     71     case CLOSE:
     72       return true;
     73   }
     74   NOTREACHED();
     75   return false;
     76 }
     77 
     78 bool TCPSocketState::IsPending(TransitionType transition) const {
     79   return pending_transition_ == transition;
     80 }
     81 
     82 bool TCPSocketState::IsConnected() const {
     83   return state_ == CONNECTED || state_ == SSL_CONNECTED;
     84 }
     85 
     86 bool TCPSocketState::IsBound() const {
     87   return state_ != INITIAL && state_ != CLOSED;
     88 }
     89 
     90 }  // namespace ppapi
     91