Reland the ULONG -> SIZE_T change from 317177
[chromium-blink-merge.git] / extensions / browser / api / system_info / system_info_provider.h
blobb8674f959c143519fb5a746879d2c2ca4ea31f49
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_
8 #include <queue>
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
20 // scoped_refptr.
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
28 // extension process.
30 // Each kind of SystemInfoProvider should satisfy an API query in a subclass on
31 // the blocking pool.
32 class SystemInfoProvider
33 : public base::RefCountedThreadSafe<SystemInfoProvider> {
34 public:
35 // Callback type for completing to get information. The argument indicates
36 // whether its contents are valid, for example, no error occurs in querying
37 // the information.
38 typedef base::Callback<void(bool)> QueryInfoCompletionCallback;
39 typedef std::queue<QueryInfoCompletionCallback> CallbackQueue;
41 SystemInfoProvider();
43 // Override to do any prepare work on UI thread before |QueryInfo()| gets
44 // called.
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
62 // the system.
63 void StartQueryInfo(const QueryInfoCompletionCallback& callback);
65 protected:
66 virtual ~SystemInfoProvider();
68 private:
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
90 // executed in order.
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_