summaryrefslogtreecommitdiffstats
path: root/extensions/browser/api/system_info/system_info_provider.cc
blob: 3c2cf5fd38c91064d041fe67544ee3bff02624b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/api/system_info/system_info_provider.h"

#include "base/bind.h"
#include "content/public/browser/browser_thread.h"

namespace extensions {

SystemInfoProvider::SystemInfoProvider() : is_waiting_for_completion_(false) {
  base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
  worker_pool_ = pool->GetSequencedTaskRunnerWithShutdownBehavior(
      pool->GetSequenceToken(),
      base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
}

SystemInfoProvider::~SystemInfoProvider() {
}

void SystemInfoProvider::PrepareQueryOnUIThread() {
}

void SystemInfoProvider::InitializeProvider(
    const base::Closure& do_query_info_callback) {
  do_query_info_callback.Run();
}

void SystemInfoProvider::StartQueryInfo(
    const QueryInfoCompletionCallback& callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(!callback.is_null());

  callbacks_.push(callback);

  if (is_waiting_for_completion_)
    return;

  is_waiting_for_completion_ = true;

  InitializeProvider(
      base::Bind(&SystemInfoProvider::StartQueryInfoPostInitialization, this));
}

void SystemInfoProvider::OnQueryCompleted(bool success) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  while (!callbacks_.empty()) {
    QueryInfoCompletionCallback callback = callbacks_.front();
    callback.Run(success);
    callbacks_.pop();
  }

  is_waiting_for_completion_ = false;
}

void SystemInfoProvider::StartQueryInfoPostInitialization() {
  PrepareQueryOnUIThread();
  // Post the custom query info task to blocking pool for information querying
  // and reply with OnQueryCompleted.
  base::PostTaskAndReplyWithResult(
      worker_pool_.get(),
      FROM_HERE,
      base::Bind(&SystemInfoProvider::QueryInfo, this),
      base::Bind(&SystemInfoProvider::OnQueryCompleted, this));
}

}  // namespace extensions