Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(459)

Side by Side Diff: chrome/browser/extensions/system_info_provider.h

Issue 10831353: Add a generic template for system info provider (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Expose QueryInfo interface publicly Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_PROVIDER_H_
5 #define CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_PROVIDER_H_
6
7 #include <queue>
8
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/lazy_instance.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/sequenced_worker_pool.h"
14 #include "content/public/browser/browser_thread.h"
15
16 namespace extensions {
17
18 // A generic template for all kinds of system information provider. Each kind
Mihai Parparita -not on Chrome 2012/08/23 00:24:52 "providers" is more grammatically correct.
19 // of SystemInfoProvider is a single shared instance. It is created if needed,
20 // and destroyed at exit time. This is done via LazyInstance and scoped_ptr.
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 are arrived, e.g. calling systemInfo.cpu.get repeatedly
Mihai Parparita -not on Chrome 2012/08/23 00:24:52 "are arrived" can be removed.
28 // in renderer.
Mihai Parparita -not on Chrome 2012/08/23 00:24:52 instead of a "in renderer", "in an extension proce
29 //
30 // Template parameter T is the system information type. It could be the
31 // structure type generated by IDL parser.
32 template<class T>
33 class SystemInfoProvider {
34 public:
35 // Callback type for completing to get information. The callback accepts
36 // two arguments. The first one is the information got already, the second
37 // one indicates whether its contents are valid, for example, no error
38 // occurs in querying the information.
39 typedef base::Callback<void(const T&, bool)> QueryInfoCompletionCallback;
40 typedef std::queue<QueryInfoCompletionCallback> CallbackQueue;
41
42 SystemInfoProvider()
43 : is_waiting_for_completion_(false) {
44 worker_pool_token_ =
45 content::BrowserThread::GetBlockingPool()->GetSequenceToken();
46 }
47
48 virtual ~SystemInfoProvider() {}
49
50 // Static method to get the single shared instance. Should be implemented
51 // in the impl file for each kind of provider.
52 static SystemInfoProvider<T>* Get();
53
54 // For testing
55 static void InitializeForTesting(SystemInfoProvider<T>* provider) {
56 DCHECK(provider != NULL);
57 single_shared_provider_.Get().reset(provider);
58 }
59
60 // Start to query the system information. Should be called on UI thread.
61 // The |callback| will get called once the query is completed.
62 virtual void StartQueryInfo(const QueryInfoCompletionCallback& callback) {
Hongbo Min 2012/08/22 08:05:59 StartGetInfo is renamed to StartQueryInfo for cons
63 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
64 DCHECK(!callback.is_null());
65
66 callbacks_.push(callback);
67
68 if (is_waiting_for_completion_)
69 return;
70
71 is_waiting_for_completion_ = true;
72
73 base::SequencedWorkerPool* worker_pool =
74 content::BrowserThread::GetBlockingPool();
75 // The query task posted to the worker pool won't block shutdown, and any
76 // running query task at shutdown time will be ignored.
77 worker_pool->PostSequencedWorkerTaskWithShutdownBehavior(
78 worker_pool_token_,
79 FROM_HERE,
80 base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool,
81 base::Unretained(this)),
82 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
83 }
84
85 // Query the system information synchronously and output the result to the
86 // |info| parameter. The |info| contents MUST be reset firstly in its
87 // platform specific implementation. Return true if it succeeds, otherwise
88 // false is returned.
89 virtual bool QueryInfo(T* info) = 0;
Hongbo Min 2012/08/22 08:05:59 When I am writing the code for watching storage fr
90
91 protected:
92 virtual void QueryOnWorkerPool() {
Mihai Parparita -not on Chrome 2012/08/23 00:24:52 Does this need to be virtual?
Hongbo Min 2012/08/23 00:35:40 Platform specific implementation may have differen
93 bool success = QueryInfo(&info_);
94 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
95 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted,
96 base::Unretained(this), success));
97 }
98
99 // Called on UI thread. The |success| parameter means whether it succeeds
100 // to get the information.
101 virtual void OnQueryCompleted(bool success) {
Mihai Parparita -not on Chrome 2012/08/23 00:24:52 Ditto.
Hongbo Min 2012/08/23 00:35:40 Same as the above.
102 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
103
104 while (!callbacks_.empty()) {
105 QueryInfoCompletionCallback callback = callbacks_.front();
106 callback.Run(info_, success);
107 callbacks_.pop();
108 }
109
110 is_waiting_for_completion_ = false;
111 }
112
113 // Template function for creating the single shared provider instance.
114 // Template paramter I is the type of SystemInfoProvider implementation.
115 template<class I>
116 static SystemInfoProvider<T>* GetInstance() {
117 if (!single_shared_provider_.Get().get()) {
118 I* impl = new I();
119 single_shared_provider_.Get().reset(impl);
120 }
121 return single_shared_provider_.Get().get();
122 }
123
124 // The latest information filled up by QueryInfo implementation. Here we
125 // assume the T is disallowed to copy constructor, aligns with the structure
126 // type generated by IDL parser.
Mihai Parparita -not on Chrome 2012/08/23 00:24:52 Ah, you mean that because T doesn't have a copy co
Hongbo Min 2012/08/23 00:35:40 Correct. It is protected due to the same reason as
127 T info_;
128
129 private:
130 // The single shared provider instance. We create it only when needed.
131 static typename base::LazyInstance<
132 scoped_ptr<SystemInfoProvider<T> > > single_shared_provider_;
133
134 // The queue of callbacks waiting for the info querying completion. It is
135 // maintained on the UI thread.
136 CallbackQueue callbacks_;
137
138 // Indicates if it is waiting for the querying completion.
139 bool is_waiting_for_completion_;
140
141 // Unqiue sequence token so that the operation of querying inforation can
142 // be executed in order.
143 base::SequencedWorkerPool::SequenceToken worker_pool_token_;
144
145 DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider<T>);
146 };
147
148 // Static member intialization.
149 template<class T>
150 typename base::LazyInstance<scoped_ptr<SystemInfoProvider<T> > >
151 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER;
152
153 } // namespace extensions
154
155 #endif // CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_PROVIDER_H_
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/system_info_cpu/system_info_cpu_apitest.cc ('k') | chrome/chrome_browser_extensions.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698