blob: 84dbb804c2a30a27095cebddeeccc7f43fec2a2b [file] [log] [blame]
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001// 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.
Ben Murdochbb1529c2013-08-08 10:24:53 +01004
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00005#ifndef CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_
6#define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_
7
8#include <queue>
9
10#include "base/bind.h"
11#include "base/callback.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000012#include "base/memory/ref_counted.h"
13#include "base/threading/sequenced_worker_pool.h"
14#include "content/public/browser/browser_thread.h"
15
16namespace extensions {
17
Ben Murdochbb1529c2013-08-08 10:24:53 +010018// An abstract base class for all kinds of system information providers. Each
19// kind of SystemInfoProvider is a single shared instance. It is created if
20// needed, and destroyed at exit time. This is done via LazyInstance and
21// scoped_refptr.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000022//
23// The SystemInfoProvider is designed to query system information on the worker
24// pool. It also maintains a queue of callbacks on the UI thread which are
25// waiting for the completion of querying operation. Once the query operation
26// is completed, all pending callbacks in the queue get called on the UI
27// thread. In this way, it avoids frequent querying operation in case of lots
28// of query requests, e.g. calling systemInfo.cpu.get repeatedly in an
29// extension process.
30//
Ben Murdochbb1529c2013-08-08 10:24:53 +010031// Each kind of SystemInfoProvider should satisfy an API query in a subclass on
32// the blocking pool.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000033class SystemInfoProvider
Ben Murdochbb1529c2013-08-08 10:24:53 +010034 : public base::RefCountedThreadSafe<SystemInfoProvider> {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000035 public:
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010036 // Callback type for completing to get information. The argument indicates
37 // whether its contents are valid, for example, no error occurs in querying
38 // the information.
39 typedef base::Callback<void(bool)> QueryInfoCompletionCallback;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000040 typedef std::queue<QueryInfoCompletionCallback> CallbackQueue;
41
Ben Murdochbb1529c2013-08-08 10:24:53 +010042 SystemInfoProvider();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000043
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010044 // Override to do any prepare work on UI thread before |QueryInfo()| gets
45 // called.
Ben Murdochbb1529c2013-08-08 10:24:53 +010046 virtual void PrepareQueryOnUIThread();
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010047
Ben Murdochbb1529c2013-08-08 10:24:53 +010048 // The parameter |do_query_info_callback| is query info task which is posted
49 // to SystemInfoProvider sequenced worker pool.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010050 //
51 // You can do any initial things of *InfoProvider before start to query info.
52 // While overriding this method, |do_query_info_callback| *must* be called
53 // directly or indirectly.
54 //
55 // Sample usage please refer to StorageInfoProvider.
Ben Murdochbb1529c2013-08-08 10:24:53 +010056 virtual void InitializeProvider(const base::Closure& do_query_info_callback);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000057
58 // Start to query the system information. Should be called on UI thread.
59 // The |callback| will get called once the query is completed.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010060 //
61 // If the parameter |callback| itself calls StartQueryInfo(callback2),
62 // callback2 will be called immediately rather than triggering another call to
63 // the system.
Ben Murdochbb1529c2013-08-08 10:24:53 +010064 void StartQueryInfo(const QueryInfoCompletionCallback& callback);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000065
66 protected:
Ben Murdochbb1529c2013-08-08 10:24:53 +010067 virtual ~SystemInfoProvider();
68
69 private:
70 friend class base::RefCountedThreadSafe<SystemInfoProvider>;
71
72 // Interface to query the system information synchronously.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010073 // Return true if no error occurs.
74 // Should be called in the blocking pool.
75 virtual bool QueryInfo() = 0;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000076
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010077 // Called on UI thread. The |success| parameter means whether it succeeds
78 // to get the information.
Ben Murdochbb1529c2013-08-08 10:24:53 +010079 void OnQueryCompleted(bool success);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010080
Ben Murdochbb1529c2013-08-08 10:24:53 +010081 void StartQueryInfoPostInitialization();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000082
83 // The queue of callbacks waiting for the info querying completion. It is
84 // maintained on the UI thread.
85 CallbackQueue callbacks_;
86
87 // Indicates if it is waiting for the querying completion.
88 bool is_waiting_for_completion_;
89
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010090 // Sequenced worker pool to make the operation of querying information get
91 // executed in order.
92 scoped_refptr<base::SequencedTaskRunner> worker_pool_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000093
Ben Murdochbb1529c2013-08-08 10:24:53 +010094 DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000095};
96
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000097} // namespace extensions
98
99#endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_