| 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" | |
| 9 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" | 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 12 | 10 |
| 13 using content::BrowserThread; | 11 using content::BrowserThread; |
| 14 | 12 |
| 15 namespace extensions { | 13 namespace extensions { |
| 16 | 14 |
| 17 AsyncAPIFunction::AsyncAPIFunction() | 15 AsyncApiFunction::AsyncApiFunction() |
| 18 : work_thread_id_(BrowserThread::IO) { | 16 : work_thread_id_(BrowserThread::IO) { |
| 19 } | 17 } |
| 20 | 18 |
| 21 bool AsyncAPIFunction::RunImpl() { | 19 bool AsyncApiFunction::RunImpl() { |
| 22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 23 extension_service_ = profile()->GetExtensionService(); | |
| 24 | 21 |
| 25 if (!Prepare()) { | 22 if (!PrePrepare() || !Prepare()) { |
| 26 return false; | 23 return false; |
| 27 } | 24 } |
| 28 bool rv = BrowserThread::PostTask( | 25 bool rv = BrowserThread::PostTask( |
| 29 work_thread_id_, FROM_HERE, | 26 work_thread_id_, FROM_HERE, |
| 30 base::Bind(&AsyncAPIFunction::WorkOnWorkThread, this)); | 27 base::Bind(&AsyncApiFunction::WorkOnWorkThread, this)); |
| 31 DCHECK(rv); | 28 DCHECK(rv); |
| 32 return true; | 29 return true; |
| 33 } | 30 } |
| 34 | 31 |
| 35 void AsyncAPIFunction::Work() { | 32 bool AsyncApiFunction::PrePrepare() { |
| 33 return true; |
| 36 } | 34 } |
| 37 | 35 |
| 38 void AsyncAPIFunction::AsyncWorkStart() { | 36 void AsyncApiFunction::Work() { |
| 37 } |
| 38 |
| 39 void AsyncApiFunction::AsyncWorkStart() { |
| 39 Work(); | 40 Work(); |
| 40 AsyncWorkCompleted(); | 41 AsyncWorkCompleted(); |
| 41 } | 42 } |
| 42 | 43 |
| 43 void AsyncAPIFunction::AsyncWorkCompleted() { | 44 void AsyncApiFunction::AsyncWorkCompleted() { |
| 44 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 45 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 45 bool rv = BrowserThread::PostTask( | 46 bool rv = BrowserThread::PostTask( |
| 46 BrowserThread::UI, FROM_HERE, | 47 BrowserThread::UI, FROM_HERE, |
| 47 base::Bind(&AsyncAPIFunction::RespondOnUIThread, this)); | 48 base::Bind(&AsyncApiFunction::RespondOnUIThread, this)); |
| 48 DCHECK(rv); | 49 DCHECK(rv); |
| 49 } else { | 50 } else { |
| 50 SendResponse(Respond()); | 51 SendResponse(Respond()); |
| 51 } | 52 } |
| 52 } | 53 } |
| 53 | 54 |
| 54 void AsyncAPIFunction::WorkOnWorkThread() { | 55 void AsyncApiFunction::WorkOnWorkThread() { |
| 55 DCHECK(BrowserThread::CurrentlyOn(work_thread_id_)); | 56 DCHECK(BrowserThread::CurrentlyOn(work_thread_id_)); |
| 56 DCHECK(work_thread_id_ != BrowserThread::UI) << | 57 DCHECK(work_thread_id_ != BrowserThread::UI) << |
| 57 "You have specified that AsyncAPIFunction::Work() should happen on " | 58 "You have specified that AsyncApiFunction::Work() should happen on " |
| 58 "the UI thread. This nullifies the point of this class. Either " | 59 "the UI thread. This nullifies the point of this class. Either " |
| 59 "specify a different thread or derive from a different class."; | 60 "specify a different thread or derive from a different class."; |
| 60 AsyncWorkStart(); | 61 AsyncWorkStart(); |
| 61 } | 62 } |
| 62 | 63 |
| 63 void AsyncAPIFunction::RespondOnUIThread() { | 64 void AsyncApiFunction::RespondOnUIThread() { |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 65 SendResponse(Respond()); | 66 SendResponse(Respond()); |
| 66 } | 67 } |
| 67 | 68 |
| 68 int AsyncAPIFunction::DeprecatedExtractSrcId(size_t argument_position) { | 69 int AsyncApiFunction::DeprecatedExtractSrcId(size_t argument_position) { |
| 69 scoped_ptr<DictionaryValue> options(new DictionaryValue()); | 70 scoped_ptr<DictionaryValue> options(new DictionaryValue()); |
| 70 if (args_->GetSize() > argument_position) { | 71 if (args_->GetSize() > argument_position) { |
| 71 DictionaryValue* temp_options = NULL; | 72 DictionaryValue* temp_options = NULL; |
| 72 if (args_->GetDictionary(argument_position, &temp_options)) | 73 if (args_->GetDictionary(argument_position, &temp_options)) |
| 73 options.reset(temp_options->DeepCopy()); | 74 options.reset(temp_options->DeepCopy()); |
| 74 } | 75 } |
| 75 | 76 |
| 76 // If we tacked on a srcId to the options object, pull it out here to provide | 77 // If we tacked on a srcId to the options object, pull it out here to provide |
| 77 // to the caller. | 78 // to the caller. |
| 78 int src_id = -1; | 79 int src_id = -1; |
| 79 if (options->HasKey(kSrcIdKey)) { | 80 if (options->HasKey(kSrcIdKey)) { |
| 80 EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kSrcIdKey, &src_id)); | 81 EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kSrcIdKey, &src_id)); |
| 81 } | 82 } |
| 82 | 83 |
| 83 return src_id; | 84 return src_id; |
| 84 } | 85 } |
| 85 | 86 |
| 86 int AsyncAPIFunction::ExtractSrcId(const DictionaryValue* options) { | 87 int AsyncApiFunction::ExtractSrcId(const DictionaryValue* options) { |
| 87 int src_id = -1; | 88 int src_id = -1; |
| 88 if (options) { | 89 if (options) { |
| 89 if (options->HasKey(kSrcIdKey)) | 90 if (options->HasKey(kSrcIdKey)) |
| 90 EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kSrcIdKey, &src_id)); | 91 EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kSrcIdKey, &src_id)); |
| 91 } | 92 } |
| 92 return src_id; | 93 return src_id; |
| 93 } | 94 } |
| 94 | 95 |
| 95 APIResourceEventNotifier* AsyncAPIFunction::CreateEventNotifier(int src_id) { | 96 ApiResourceEventNotifier* AsyncApiFunction::CreateEventNotifier(int src_id) { |
| 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 97 return new APIResourceEventNotifier( | 98 return new ApiResourceEventNotifier( |
| 98 profile()->GetExtensionEventRouter(), profile(), extension_id(), | 99 profile()->GetExtensionEventRouter(), profile(), extension_id(), |
| 99 src_id, source_url()); | 100 src_id, source_url()); |
| 100 } | 101 } |
| 101 | 102 |
| 102 APIResourceController* AsyncAPIFunction::controller() { | |
| 103 // ExtensionService's APIResourceController is set exactly once, long before | |
| 104 // this code is reached, so it's safe to access it on any thread. | |
| 105 return extension_service_->api_resource_controller(); | |
| 106 } | |
| 107 | |
| 108 } // namespace extensions | 103 } // namespace extensions |
| OLD | NEW |