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.
5 #ifndef EXTENSIONS_BROWSER_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_
6 #define EXTENSIONS_BROWSER_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/threading/sequenced_worker_pool.h"
15 namespace extensions
{
17 // An abstract base class for all kinds of system information providers. Each
18 // kind of SystemInfoProvider is a single shared instance. It is created if
19 // needed, and destroyed at exit time. This is done via LazyInstance and
22 // The SystemInfoProvider is designed to query system information on the worker
23 // pool. It also maintains a queue of callbacks on the UI thread which are
24 // waiting for the completion of querying operation. Once the query operation
25 // is completed, all pending callbacks in the queue get called on the UI
26 // thread. In this way, it avoids frequent querying operation in case of lots
27 // of query requests, e.g. calling systemInfo.cpu.get repeatedly in an
30 // Each kind of SystemInfoProvider should satisfy an API query in a subclass on
32 class SystemInfoProvider
33 : public base::RefCountedThreadSafe
<SystemInfoProvider
> {
35 // Callback type for completing to get information. The argument indicates
36 // whether its contents are valid, for example, no error occurs in querying
38 typedef base::Callback
<void(bool)> QueryInfoCompletionCallback
;
39 typedef std::queue
<QueryInfoCompletionCallback
> CallbackQueue
;
43 // Override to do any prepare work on UI thread before |QueryInfo()| gets
45 virtual void PrepareQueryOnUIThread();
47 // The parameter |do_query_info_callback| is query info task which is posted
48 // to SystemInfoProvider sequenced worker pool.
50 // You can do any initial things of *InfoProvider before start to query info.
51 // While overriding this method, |do_query_info_callback| *must* be called
52 // directly or indirectly.
54 // Sample usage please refer to StorageInfoProvider.
55 virtual void InitializeProvider(const base::Closure
& do_query_info_callback
);
57 // Start to query the system information. Should be called on UI thread.
58 // The |callback| will get called once the query is completed.
60 // If the parameter |callback| itself calls StartQueryInfo(callback2),
61 // callback2 will be called immediately rather than triggering another call to
63 void StartQueryInfo(const QueryInfoCompletionCallback
& callback
);
66 virtual ~SystemInfoProvider();
69 friend class base::RefCountedThreadSafe
<SystemInfoProvider
>;
71 // Interface to query the system information synchronously.
72 // Return true if no error occurs.
73 // Should be called in the blocking pool.
74 virtual bool QueryInfo() = 0;
76 // Called on UI thread. The |success| parameter means whether it succeeds
77 // to get the information.
78 void OnQueryCompleted(bool success
);
80 void StartQueryInfoPostInitialization();
82 // The queue of callbacks waiting for the info querying completion. It is
83 // maintained on the UI thread.
84 CallbackQueue callbacks_
;
86 // Indicates if it is waiting for the querying completion.
87 bool is_waiting_for_completion_
;
89 // Sequenced worker pool to make the operation of querying information get
91 scoped_refptr
<base::SequencedTaskRunner
> worker_pool_
;
93 DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider
);
96 } // namespace extensions
98 #endif // EXTENSIONS_BROWSER_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_