Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 1 | // Copyright 2013 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/system_info/system_info_provider.h" |
| 6 | |
| 7 | namespace extensions { |
| 8 | |
| 9 | SystemInfoProvider::SystemInfoProvider() |
| 10 | : is_waiting_for_completion_(false) { |
| 11 | base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); |
| 12 | worker_pool_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 13 | pool->GetSequenceToken(), |
| 14 | base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 15 | } |
| 16 | |
| 17 | SystemInfoProvider::~SystemInfoProvider() {} |
| 18 | |
| 19 | void SystemInfoProvider::PrepareQueryOnUIThread() {} |
| 20 | |
| 21 | void SystemInfoProvider::InitializeProvider(const base::Closure& |
| 22 | do_query_info_callback) { |
| 23 | do_query_info_callback.Run(); |
| 24 | } |
| 25 | |
| 26 | void SystemInfoProvider::StartQueryInfo( |
| 27 | const QueryInfoCompletionCallback& callback) { |
| 28 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 29 | DCHECK(!callback.is_null()); |
| 30 | |
| 31 | callbacks_.push(callback); |
| 32 | |
| 33 | if (is_waiting_for_completion_) |
| 34 | return; |
| 35 | |
| 36 | is_waiting_for_completion_ = true; |
| 37 | |
| 38 | InitializeProvider(base::Bind( |
| 39 | &SystemInfoProvider::StartQueryInfoPostInitialization, this)); |
| 40 | } |
| 41 | |
| 42 | void SystemInfoProvider::OnQueryCompleted(bool success) { |
| 43 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 44 | |
| 45 | while (!callbacks_.empty()) { |
| 46 | QueryInfoCompletionCallback callback = callbacks_.front(); |
| 47 | callback.Run(success); |
| 48 | callbacks_.pop(); |
| 49 | } |
| 50 | |
| 51 | is_waiting_for_completion_ = false; |
| 52 | } |
| 53 | |
| 54 | void SystemInfoProvider::StartQueryInfoPostInitialization() { |
| 55 | PrepareQueryOnUIThread(); |
| 56 | // Post the custom query info task to blocking pool for information querying |
| 57 | // and reply with OnQueryCompleted. |
| 58 | base::PostTaskAndReplyWithResult( |
| 59 | worker_pool_, |
| 60 | FROM_HERE, |
| 61 | base::Bind(&SystemInfoProvider::QueryInfo, this), |
| 62 | base::Bind(&SystemInfoProvider::OnQueryCompleted, this)); |
| 63 | } |
| 64 | |
| 65 | } // namespace extensions |