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