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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/system_info_provider.h
diff --git a/chrome/browser/extensions/system_info_provider.h b/chrome/browser/extensions/system_info_provider.h
new file mode 100644
index 0000000000000000000000000000000000000000..9eba7b51057c02800069d85e21dfdbe9e0a0c850
--- /dev/null
+++ b/chrome/browser/extensions/system_info_provider.h
@@ -0,0 +1,155 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+#ifndef CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_PROVIDER_H_
+#define CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_PROVIDER_H_
+
+#include <queue>
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/lazy_instance.h"
+#include "base/memory/weak_ptr.h"
+#include "base/threading/sequenced_worker_pool.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace extensions {
+
+// 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.
+// of SystemInfoProvider is a single shared instance. It is created if needed,
+// and destroyed at exit time. This is done via LazyInstance and scoped_ptr.
+//
+// The SystemInfoProvider is designed to query system information on the worker
+// pool. It also maintains a queue of callbacks on the UI thread which are
+// waiting for the completion of querying operation. Once the query operation
+// is completed, all pending callbacks in the queue get called on the UI
+// thread. In this way, it avoids frequent querying operation in case of lots
+// 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.
+// in renderer.
Mihai Parparita -not on Chrome 2012/08/23 00:24:52 instead of a "in renderer", "in an extension proce
+//
+// Template parameter T is the system information type. It could be the
+// structure type generated by IDL parser.
+template<class T>
+class SystemInfoProvider {
+ public:
+ // Callback type for completing to get information. The callback accepts
+ // two arguments. The first one is the information got already, the second
+ // one indicates whether its contents are valid, for example, no error
+ // occurs in querying the information.
+ typedef base::Callback<void(const T&, bool)> QueryInfoCompletionCallback;
+ typedef std::queue<QueryInfoCompletionCallback> CallbackQueue;
+
+ SystemInfoProvider()
+ : is_waiting_for_completion_(false) {
+ worker_pool_token_ =
+ content::BrowserThread::GetBlockingPool()->GetSequenceToken();
+ }
+
+ virtual ~SystemInfoProvider() {}
+
+ // Static method to get the single shared instance. Should be implemented
+ // in the impl file for each kind of provider.
+ static SystemInfoProvider<T>* Get();
+
+ // For testing
+ static void InitializeForTesting(SystemInfoProvider<T>* provider) {
+ DCHECK(provider != NULL);
+ single_shared_provider_.Get().reset(provider);
+ }
+
+ // Start to query the system information. Should be called on UI thread.
+ // The |callback| will get called once the query is completed.
+ virtual void StartQueryInfo(const QueryInfoCompletionCallback& callback) {
Hongbo Min 2012/08/22 08:05:59 StartGetInfo is renamed to StartQueryInfo for cons
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+ DCHECK(!callback.is_null());
+
+ callbacks_.push(callback);
+
+ if (is_waiting_for_completion_)
+ return;
+
+ is_waiting_for_completion_ = true;
+
+ base::SequencedWorkerPool* worker_pool =
+ content::BrowserThread::GetBlockingPool();
+ // The query task posted to the worker pool won't block shutdown, and any
+ // running query task at shutdown time will be ignored.
+ worker_pool->PostSequencedWorkerTaskWithShutdownBehavior(
+ worker_pool_token_,
+ FROM_HERE,
+ base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool,
+ base::Unretained(this)),
+ base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
+ }
+
+ // Query the system information synchronously and output the result to the
+ // |info| parameter. The |info| contents MUST be reset firstly in its
+ // platform specific implementation. Return true if it succeeds, otherwise
+ // false is returned.
+ virtual bool QueryInfo(T* info) = 0;
Hongbo Min 2012/08/22 08:05:59 When I am writing the code for watching storage fr
+
+ protected:
+ 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
+ bool success = QueryInfo(&info_);
+ content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&SystemInfoProvider<T>::OnQueryCompleted,
+ base::Unretained(this), success));
+ }
+
+ // Called on UI thread. The |success| parameter means whether it succeeds
+ // to get the information.
+ 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.
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+
+ while (!callbacks_.empty()) {
+ QueryInfoCompletionCallback callback = callbacks_.front();
+ callback.Run(info_, success);
+ callbacks_.pop();
+ }
+
+ is_waiting_for_completion_ = false;
+ }
+
+ // Template function for creating the single shared provider instance.
+ // Template paramter I is the type of SystemInfoProvider implementation.
+ template<class I>
+ static SystemInfoProvider<T>* GetInstance() {
+ if (!single_shared_provider_.Get().get()) {
+ I* impl = new I();
+ single_shared_provider_.Get().reset(impl);
+ }
+ return single_shared_provider_.Get().get();
+ }
+
+ // The latest information filled up by QueryInfo implementation. Here we
+ // assume the T is disallowed to copy constructor, aligns with the structure
+ // 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
+ T info_;
+
+ private:
+ // The single shared provider instance. We create it only when needed.
+ static typename base::LazyInstance<
+ scoped_ptr<SystemInfoProvider<T> > > single_shared_provider_;
+
+ // The queue of callbacks waiting for the info querying completion. It is
+ // maintained on the UI thread.
+ CallbackQueue callbacks_;
+
+ // Indicates if it is waiting for the querying completion.
+ bool is_waiting_for_completion_;
+
+ // Unqiue sequence token so that the operation of querying inforation can
+ // be executed in order.
+ base::SequencedWorkerPool::SequenceToken worker_pool_token_;
+
+ DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider<T>);
+};
+
+// Static member intialization.
+template<class T>
+typename base::LazyInstance<scoped_ptr<SystemInfoProvider<T> > >
+ SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER;
+
+} // namespace extensions
+
+#endif // CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_PROVIDER_H_
« 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