Home | History | Annotate | Download | only in api
      1 // Copyright 2014 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 "extensions/browser/api/async_api_function.h"
      6 
      7 #include "base/bind.h"
      8 #include "extensions/browser/extension_system.h"
      9 
     10 using content::BrowserThread;
     11 
     12 namespace extensions {
     13 
     14 // AsyncApiFunction
     15 AsyncApiFunction::AsyncApiFunction() : work_thread_id_(BrowserThread::IO) {}
     16 
     17 AsyncApiFunction::~AsyncApiFunction() {}
     18 
     19 bool AsyncApiFunction::RunAsync() {
     20   DCHECK_CURRENTLY_ON(BrowserThread::UI);
     21 
     22   if (!PrePrepare() || !Prepare()) {
     23     return false;
     24   }
     25   bool rv = BrowserThread::PostTask(
     26       work_thread_id_,
     27       FROM_HERE,
     28       base::Bind(&AsyncApiFunction::WorkOnWorkThread, this));
     29   DCHECK(rv);
     30   return true;
     31 }
     32 
     33 bool AsyncApiFunction::PrePrepare() { return true; }
     34 
     35 void AsyncApiFunction::Work() {}
     36 
     37 void AsyncApiFunction::AsyncWorkStart() {
     38   Work();
     39   AsyncWorkCompleted();
     40 }
     41 
     42 void AsyncApiFunction::AsyncWorkCompleted() {
     43   if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
     44     bool rv = BrowserThread::PostTask(
     45         BrowserThread::UI,
     46         FROM_HERE,
     47         base::Bind(&AsyncApiFunction::RespondOnUIThread, this));
     48     DCHECK(rv);
     49   } else {
     50     SendResponse(Respond());
     51   }
     52 }
     53 
     54 void AsyncApiFunction::WorkOnWorkThread() {
     55   DCHECK_CURRENTLY_ON(work_thread_id_);
     56   // TODO(reillyg): Commented out to aid in the transition of the HID API from
     57   // the FILE thread to UI thread. Re-enable when it won't spam the logs.
     58   //
     59   // DLOG_IF(ERROR, (work_thread_id_ == BrowserThread::UI))
     60   //    << "You have specified that AsyncApiFunction::Work() should happen on "
     61   //       "the UI thread. This nullifies the point of this class. Either "
     62   //       "specify a different thread or derive from a different class.";
     63   AsyncWorkStart();
     64 }
     65 
     66 void AsyncApiFunction::RespondOnUIThread() {
     67   DCHECK_CURRENTLY_ON(BrowserThread::UI);
     68   SendResponse(Respond());
     69 }
     70 
     71 }  // namespace extensions
     72