Home | History | Annotate | Download | only in devtools
      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 "chrome/browser/devtools/refcounted_adb_thread.h"
      6 
      7 #include "content/public/browser/browser_thread.h"
      8 
      9 using content::BrowserThread;
     10 
     11 const char kDevToolsAdbBridgeThreadName[] = "Chrome_DevToolsADBThread";
     12 
     13 RefCountedAdbThread* RefCountedAdbThread::instance_ = NULL;
     14 
     15 // static
     16 scoped_refptr<RefCountedAdbThread> RefCountedAdbThread::GetInstance() {
     17   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     18   if (!instance_)
     19     new RefCountedAdbThread();
     20   return instance_;
     21 }
     22 
     23 RefCountedAdbThread::RefCountedAdbThread() {
     24   instance_ = this;
     25   thread_ = new base::Thread(kDevToolsAdbBridgeThreadName);
     26   base::Thread::Options options;
     27   options.message_loop_type = base::MessageLoop::TYPE_IO;
     28   if (!thread_->StartWithOptions(options)) {
     29     delete thread_;
     30     thread_ = NULL;
     31   }
     32 }
     33 
     34 base::MessageLoop* RefCountedAdbThread::message_loop() {
     35   return thread_ ? thread_->message_loop() : NULL;
     36 }
     37 
     38 // static
     39 void RefCountedAdbThread::StopThread(base::Thread* thread) {
     40   thread->Stop();
     41 }
     42 
     43 RefCountedAdbThread::~RefCountedAdbThread() {
     44   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     45   instance_ = NULL;
     46   if (!thread_)
     47     return;
     48   // Shut down thread on FILE thread to join into IO.
     49   BrowserThread::PostTask(
     50       BrowserThread::FILE, FROM_HERE,
     51       base::Bind(&RefCountedAdbThread::StopThread, thread_));
     52 }
     53