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