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