blob: 5e9c5cf9f54e464af0fc88edc7e8143a916e1bd8 (
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
// 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.
#ifndef CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_
#define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_
#include <queue>
#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/threading/sequenced_worker_pool.h"
namespace extensions {
// An abstract base class for all kinds of system information providers. Each
// kind of SystemInfoProvider is a single shared instance. It is created if
// needed, and destroyed at exit time. This is done via LazyInstance and
// scoped_refptr.
//
// The SystemInfoProvider is designed to query system information on the worker
// pool. It also maintains a queue of callbacks on the UI thread which are
// waiting for the completion of querying operation. Once the query operation
// is completed, all pending callbacks in the queue get called on the UI
// thread. In this way, it avoids frequent querying operation in case of lots
// of query requests, e.g. calling systemInfo.cpu.get repeatedly in an
// extension process.
//
// Each kind of SystemInfoProvider should satisfy an API query in a subclass on
// the blocking pool.
class SystemInfoProvider
: public base::RefCountedThreadSafe<SystemInfoProvider> {
public:
// Callback type for completing to get information. The argument indicates
// whether its contents are valid, for example, no error occurs in querying
// the information.
typedef base::Callback<void(bool)> QueryInfoCompletionCallback;
typedef std::queue<QueryInfoCompletionCallback> CallbackQueue;
SystemInfoProvider();
// Override to do any prepare work on UI thread before |QueryInfo()| gets
// called.
virtual void PrepareQueryOnUIThread();
// The parameter |do_query_info_callback| is query info task which is posted
// to SystemInfoProvider sequenced worker pool.
//
// You can do any initial things of *InfoProvider before start to query info.
// While overriding this method, |do_query_info_callback| *must* be called
// directly or indirectly.
//
// Sample usage please refer to StorageInfoProvider.
virtual void InitializeProvider(const base::Closure& do_query_info_callback);
// Start to query the system information. Should be called on UI thread.
// The |callback| will get called once the query is completed.
//
// If the parameter |callback| itself calls StartQueryInfo(callback2),
// callback2 will be called immediately rather than triggering another call to
// the system.
void StartQueryInfo(const QueryInfoCompletionCallback& callback);
protected:
virtual ~SystemInfoProvider();
private:
friend class base::RefCountedThreadSafe<SystemInfoProvider>;
// Interface to query the system information synchronously.
// Return true if no error occurs.
// Should be called in the blocking pool.
virtual bool QueryInfo() = 0;
// Called on UI thread. The |success| parameter means whether it succeeds
// to get the information.
void OnQueryCompleted(bool success);
void StartQueryInfoPostInitialization();
// The queue of callbacks waiting for the info querying completion. It is
// maintained on the UI thread.
CallbackQueue callbacks_;
// Indicates if it is waiting for the querying completion.
bool is_waiting_for_completion_;
// Sequenced worker pool to make the operation of querying information get
// executed in order.
scoped_refptr<base::SequencedTaskRunner> worker_pool_;
DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider);
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_
|