Access ChromotingHost::clients_ only on network thread.

Previously ChromotingHost was doing some work on the main thread and
some on the network thread. |clients_| and some other members were
accessed without lock on both of these threads. Moved most of the
ChromotingHost activity to the network thread to avoid possible
race conditions.

BUG=96325
TEST=Chromoting works

Review URL: http://codereview.chromium.org/8495024

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@109556 0039d316-1c4b-4281-b951-d872f2087c98
 void ChromotingHost::Shutdown(const base::Closure& shutdown_task) {
  if (!context_->network_message_loop()->BelongsToCurrentThread()) {
    context_->network_message_loop()->PostTask(
        FROM_HERE, base::Bind(&ChromotingHost::Shutdown, this, shutdown_task));
     return;
   }
 
  if (state_ == kInitial || state_ == kStopped) {
    state_ = kStopped;
    context_->network_message_loop()->PostTask(FROM_HERE, shutdown_task);
    return;
   }
  if (!shutdown_task.is_null())
    shutdown_tasks_.push_back(shutdown_task);
  if (state_ == kStopping)
    return;
  state_ = kStopping;
 
   while (!clients_.empty()) {
    clients_.front()->Disconnect();
  }

  // Stop session manager.
  if (session_manager_.get()) {
    session_manager_->Close();
    // It may not be safe to delete |session_manager_| here becase
    // this method may be invoked in response to a libjingle event and
    // libjingle's sigslot doesn't handle it properly, so postpone the
    // deletion.
    context_->network_message_loop()->DeleteSoon(
        FROM_HERE, session_manager_.release());
   }
 
  // Stop XMPP connection synchronously.
  if (signal_strategy_.get()) {
    signal_strategy_->Close();
    signal_strategy_.reset();

    for (StatusObserverList::iterator it = status_observers_.begin();
         it != status_observers_.end(); ++it) {
      (*it)->OnSignallingDisconnected();
    }
  }

  if (recorder_.get()) {
    StopScreenRecorder();
  } else {
    ShutdownFinish();
  }
 }

 void ChromotingHost::Shutdown(const base::Closure& shutdown_task) {
  if (MessageLoop::current() != context_->main_message_loop()) {
    context_->main_message_loop()->PostTask(
        FROM_HERE,
        base::Bind(&ChromotingHost::Shutdown, this, shutdown_task));
     return;
   }
 
  {
    base::AutoLock auto_lock(lock_);
    if (state_ == kInitial || state_ == kStopped) {
      state_ = kStopped;
      context_->main_message_loop()->PostTask(FROM_HERE, shutdown_task);
      return;
    }
    if (!shutdown_task.is_null())
      shutdown_tasks_.push_back(shutdown_task);
    if (state_ == kStopping)
      return;
    state_ = kStopping;
   }
 
   while (!clients_.empty()) {
    scoped_refptr<ClientSession> client = clients_.front();
    client->Disconnect();
    OnClientDisconnected(client);
   }
 
  ShutdownNetwork();
 }
