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.
5 #include "extensions/browser/api/async_api_function.h"
8 #include "extensions/browser/extension_system.h"
10 using content::BrowserThread
;
12 namespace extensions
{
15 AsyncApiFunction::AsyncApiFunction() : work_thread_id_(BrowserThread::IO
) {}
17 AsyncApiFunction::~AsyncApiFunction() {}
19 bool AsyncApiFunction::RunAsync() {
20 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
22 if (!PrePrepare() || !Prepare()) {
25 bool rv
= BrowserThread::PostTask(
28 base::Bind(&AsyncApiFunction::WorkOnWorkThread
, this));
33 bool AsyncApiFunction::PrePrepare() { return true; }
35 void AsyncApiFunction::Work() {}
37 void AsyncApiFunction::AsyncWorkStart() {
42 void AsyncApiFunction::AsyncWorkCompleted() {
43 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
44 bool rv
= BrowserThread::PostTask(
47 base::Bind(&AsyncApiFunction::RespondOnUIThread
, this));
50 SendResponse(Respond());
54 void AsyncApiFunction::WorkOnWorkThread() {
55 DCHECK_CURRENTLY_ON(work_thread_id_
);
56 DLOG_IF(ERROR
, (work_thread_id_
== BrowserThread::UI
))
57 << "You have specified that AsyncApiFunction::Work() should happen on "
58 "the UI thread. This nullifies the point of this class. Either "
59 "specify a different thread or derive from a different class.";
63 void AsyncApiFunction::RespondOnUIThread() {
64 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
65 SendResponse(Respond());
68 } // namespace extensions