| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/api/api_function.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/browser/extensions/api/api_resource_controller.h" | 8 #include "chrome/browser/extensions/api/api_resource_controller.h" |
| 9 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" | 9 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 | 13 |
| 14 using content::BrowserThread; | 14 using content::BrowserThread; |
| 15 | 15 |
| 16 namespace extensions { | 16 namespace extensions { |
| 17 | 17 |
| 18 bool AsyncIOAPIFunction::RunImpl() { |
| 19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 20 extension_service_ = profile()->GetExtensionService(); |
| 21 |
| 22 if (!Prepare()) { |
| 23 return false; |
| 24 } |
| 25 bool rv = BrowserThread::PostTask( |
| 26 BrowserThread::IO, FROM_HERE, |
| 27 base::Bind(&AsyncIOAPIFunction::WorkOnIOThread, this)); |
| 28 DCHECK(rv); |
| 29 return true; |
| 30 } |
| 31 |
| 32 void AsyncIOAPIFunction::Work() { |
| 33 } |
| 34 |
| 35 void AsyncIOAPIFunction::AsyncWorkStart() { |
| 36 Work(); |
| 37 AsyncWorkCompleted(); |
| 38 } |
| 39 |
| 40 void AsyncIOAPIFunction::AsyncWorkCompleted() { |
| 41 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 42 bool rv = BrowserThread::PostTask( |
| 43 BrowserThread::UI, FROM_HERE, |
| 44 base::Bind(&AsyncIOAPIFunction::RespondOnUIThread, this)); |
| 45 DCHECK(rv); |
| 46 } else { |
| 47 SendResponse(Respond()); |
| 48 } |
| 49 } |
| 50 |
| 51 void AsyncIOAPIFunction::WorkOnIOThread() { |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 53 AsyncWorkStart(); |
| 54 } |
| 55 |
| 56 void AsyncIOAPIFunction::RespondOnUIThread() { |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 58 SendResponse(Respond()); |
| 59 } |
| 60 |
| 18 int AsyncIOAPIFunction::ExtractSrcId(size_t argument_position) { | 61 int AsyncIOAPIFunction::ExtractSrcId(size_t argument_position) { |
| 19 scoped_ptr<DictionaryValue> options(new DictionaryValue()); | 62 scoped_ptr<DictionaryValue> options(new DictionaryValue()); |
| 20 if (args_->GetSize() > argument_position) { | 63 if (args_->GetSize() > argument_position) { |
| 21 DictionaryValue* temp_options = NULL; | 64 DictionaryValue* temp_options = NULL; |
| 22 if (args_->GetDictionary(argument_position, &temp_options)) | 65 if (args_->GetDictionary(argument_position, &temp_options)) |
| 23 options.reset(temp_options->DeepCopy()); | 66 options.reset(temp_options->DeepCopy()); |
| 24 } | 67 } |
| 25 | 68 |
| 26 // If we tacked on a srcId to the options object, pull it out here to provide | 69 // If we tacked on a srcId to the options object, pull it out here to provide |
| 27 // to the Socket. | 70 // to the Socket. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 40 src_id, source_url()); | 83 src_id, source_url()); |
| 41 } | 84 } |
| 42 | 85 |
| 43 APIResourceController* AsyncIOAPIFunction::controller() { | 86 APIResourceController* AsyncIOAPIFunction::controller() { |
| 44 // ExtensionService's APIResourceController is set exactly once, long before | 87 // ExtensionService's APIResourceController is set exactly once, long before |
| 45 // this code is reached, so it's safe to access it on either the IO or UI | 88 // this code is reached, so it's safe to access it on either the IO or UI |
| 46 // thread. | 89 // thread. |
| 47 return extension_service_->api_resource_controller(); | 90 return extension_service_->api_resource_controller(); |
| 48 } | 91 } |
| 49 | 92 |
| 50 bool AsyncIOAPIFunction::RunImpl() { | |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 52 extension_service_ = profile()->GetExtensionService(); | |
| 53 | |
| 54 if (!Prepare()) { | |
| 55 return false; | |
| 56 } | |
| 57 bool rv = BrowserThread::PostTask( | |
| 58 BrowserThread::IO, FROM_HERE, | |
| 59 base::Bind(&AsyncIOAPIFunction::WorkOnIOThread, this)); | |
| 60 DCHECK(rv); | |
| 61 return true; | |
| 62 } | |
| 63 | |
| 64 void AsyncIOAPIFunction::WorkOnIOThread() { | |
| 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 66 Work(); | |
| 67 bool rv = BrowserThread::PostTask( | |
| 68 BrowserThread::UI, FROM_HERE, | |
| 69 base::Bind(&AsyncIOAPIFunction::RespondOnUIThread, this)); | |
| 70 DCHECK(rv); | |
| 71 } | |
| 72 | |
| 73 void AsyncIOAPIFunction::RespondOnUIThread() { | |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 75 SendResponse(Respond()); | |
| 76 } | |
| 77 | |
| 78 } // namespace extensions | 93 } // namespace extensions |
| OLD | NEW |