Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / system_info / system_info_provider.h
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.
4
5 #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"
12 #include "base/memory/ref_counted.h"
13 #include "base/threading/sequenced_worker_pool.h"
14
15 namespace extensions {
16
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.
21 //
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.
29 //
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;
40
41   SystemInfoProvider();
42
43   // Override to do any prepare work on UI thread before |QueryInfo()| gets
44   // called.
45   virtual void PrepareQueryOnUIThread();
46
47   // The parameter |do_query_info_callback| is query info task which is posted
48   // to SystemInfoProvider sequenced worker pool.
49   //
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.
53   //
54   // Sample usage please refer to StorageInfoProvider.
55   virtual void InitializeProvider(const base::Closure& do_query_info_callback);
56
57   // Start to query the system information. Should be called on UI thread.
58   // The |callback| will get called once the query is completed.
59   //
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);
64
65  protected:
66   virtual ~SystemInfoProvider();
67
68  private:
69   friend class base::RefCountedThreadSafe<SystemInfoProvider>;
70
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;
75
76   // Called on UI thread. The |success| parameter means whether it succeeds
77   // to get the information.
78   void OnQueryCompleted(bool success);
79
80   void StartQueryInfoPostInitialization();
81
82   // The queue of callbacks waiting for the info querying completion. It is
83   // maintained on the UI thread.
84   CallbackQueue callbacks_;
85
86   // Indicates if it is waiting for the querying completion.
87   bool is_waiting_for_completion_;
88
89   // Sequenced worker pool to make the operation of querying information get
90   // executed in order.
91   scoped_refptr<base::SequencedTaskRunner> worker_pool_;
92
93   DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider);
94 };
95
96 }  // namespace extensions
97
98 #endif  // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_