Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 4 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/threading/sequenced_worker_pool.h" |
| 14 | #include "content/public/browser/browser_thread.h" |
| 15 | |
| 16 | namespace extensions { |
| 17 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 18 | // 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 22 | // |
| 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 Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 31 | // Each kind of SystemInfoProvider should satisfy an API query in a subclass on |
| 32 | // the blocking pool. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 33 | class SystemInfoProvider |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 34 | : public base::RefCountedThreadSafe<SystemInfoProvider> { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 35 | public: |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 36 | // 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 40 | typedef std::queue<QueryInfoCompletionCallback> CallbackQueue; |
| 41 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 42 | SystemInfoProvider(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 43 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 44 | // Override to do any prepare work on UI thread before |QueryInfo()| gets |
| 45 | // called. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 46 | virtual void PrepareQueryOnUIThread(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 47 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 48 | // The parameter |do_query_info_callback| is query info task which is posted |
| 49 | // to SystemInfoProvider sequenced worker pool. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 50 | // |
| 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 Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 56 | virtual void InitializeProvider(const base::Closure& do_query_info_callback); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 57 | |
| 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 Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 60 | // |
| 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 Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 64 | void StartQueryInfo(const QueryInfoCompletionCallback& callback); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 65 | |
| 66 | protected: |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 67 | virtual ~SystemInfoProvider(); |
| 68 | |
| 69 | private: |
| 70 | friend class base::RefCountedThreadSafe<SystemInfoProvider>; |
| 71 | |
| 72 | // Interface to query the system information synchronously. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 73 | // Return true if no error occurs. |
| 74 | // Should be called in the blocking pool. |
| 75 | virtual bool QueryInfo() = 0; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 76 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 77 | // Called on UI thread. The |success| parameter means whether it succeeds |
| 78 | // to get the information. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 79 | void OnQueryCompleted(bool success); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 80 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 81 | void StartQueryInfoPostInitialization(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 82 | |
| 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 Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 90 | // 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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 93 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 94 | DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 97 | } // namespace extensions |
| 98 | |
| 99 | #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ |