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