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

Unified Diff: chrome/browser/extensions/api/system_info/system_info_provider.h

Issue 18290002: [SystemInfo API] Finish TODOs in SystemInfoProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@dev_rewrite_storage_info_api
Patch Set: Update according to Greg and Jeffrey's comments Created 7 years, 5 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/api/system_info/system_info_provider.h
diff --git a/chrome/browser/extensions/api/system_info/system_info_provider.h b/chrome/browser/extensions/api/system_info/system_info_provider.h
index 1fc5b3532d1007dec84a94267c0b181234f36388..915b52885a1f91b5bdfd1206df1dd0864c44b6e2 100644
--- a/chrome/browser/extensions/api/system_info/system_info_provider.h
+++ b/chrome/browser/extensions/api/system_info/system_info_provider.h
@@ -40,11 +40,10 @@ template<class T>
class SystemInfoProvider
: public base::RefCountedThreadSafe<SystemInfoProvider<T> > {
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;
+ // Callback type for completing to get information. The argument indicates
+ // whether its contents are valid, for example, no error occurs in querying
+ // the information.
+ typedef base::Callback<void(bool)> QueryInfoCompletionCallback;
typedef std::queue<QueryInfoCompletionCallback> CallbackQueue;
SystemInfoProvider()
@@ -59,11 +58,15 @@ class SystemInfoProvider
static void InitializeForTesting(
scoped_refptr<SystemInfoProvider<T> > provider) {
DCHECK(provider.get() != NULL);
- single_shared_provider_.Get() = provider;
+ provider_.Get() = provider;
}
// Start to query the system information. Should be called on UI thread.
// The |callback| will get called once the query is completed.
+ //
+ // If the parameter |callback| itself calls StartQueryInfo(callback2),
+ // callback2 will be called immediately rather than triggering another call to
+ // the system.
void StartQueryInfo(const QueryInfoCompletionCallback& callback) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(!callback.is_null());
@@ -79,63 +82,46 @@ class SystemInfoProvider
}
protected:
- // Default implementation of querying system information.
- //
- // While overriding, there are two things need to do:
- // 1). Bind custom callback function for query system information.
- // 2). Post the custom task to blocking pool.
+ // Post the custom query info task to the sequenced blocking pool.
+ // SystemInfo API uses QueryInfo to retrieve system information in blocking
+ // pool defaultly.
+ // PostQueryTaskToBlockingPool must be called directly or indirectly to make
+ // the custom query info task run in sequence while overriding this method.
virtual void StartQueryInfoImpl() {
- base::Closure callback =
- base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool, this);
- PostQueryTaskToBlockingPool(FROM_HERE, callback);
+ PostQueryTaskToBlockingPool(base::Bind(&SystemInfoProvider<T>::QueryInfo,
+ this));
}
- // Post a task to blocking pool for information querying.
- //
- // The parameter query_callback should invoke QueryInfo directly or indirectly
- // to query the system information and return to UI thread when the query is
- // completed.
- void PostQueryTaskToBlockingPool(const tracked_objects::Location& from_here,
- const base::Closure& query_callback) {
- DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
- 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, query_callback,
- base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
+ // Post the query info task to blocking pool and will call OnQueryCompleted
+ // after the query task is completed.
+ void PostQueryTaskToBlockingPool(
+ const base::Callback<bool()> & query_info_callback) {
+ base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
+ scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_ =
+ pool->GetSequencedTaskRunnerWithShutdownBehavior(
+ worker_pool_token_,
+ base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
+ // Post the custom query info task to blocking pool for information querying
+ // and reply with OnQueryCompleted.
+ base::PostTaskAndReplyWithResult(
+ blocking_task_runner_,
+ FROM_HERE,
+ query_info_callback,
+ base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this));
}
- // 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.
- // TODO(Haojian): Remove the parameter T-typed pointer, replacing with void
- // QueryInfo().
- virtual bool QueryInfo(T* info) = 0;
-
- // Called on UI thread. The |success| parameter means whether it succeeds
- // to get the information.
- void OnQueryCompleted(bool success) {
- 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;
- }
+ // Query the system information synchronously and put the result into |info_|.
+ // Return true if no error occurs.
+ // Should be called in the blocking pool.
+ virtual bool QueryInfo() = 0;
// Template function for creating the single shared provider instance.
// Template paramter I is the type of SystemInfoProvider implementation.
template<class I>
static I* GetInstance() {
- if (!single_shared_provider_.Get().get())
- single_shared_provider_.Get() = new I();
- return static_cast<I*>(single_shared_provider_.Get().get());
+ if (!provider_.Get().get())
+ provider_.Get() = new I();
+ return static_cast<I*>(provider_.Get().get());
}
// The latest information filled up by QueryInfo implementation. Here we
@@ -144,17 +130,23 @@ class SystemInfoProvider
T info_;
private:
- // TODO(Haojian): Use PostBlockingPoolTaskAndReply to avoid unnecessary
- // trampolines trip.
- void QueryOnWorkerPool() {
- bool success = QueryInfo(&info_);
- content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
- base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this, success));
+ // Called on UI thread. The |success| parameter means whether it succeeds
+ // to get the information.
+ void OnQueryCompleted(bool success) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+
+ while (!callbacks_.empty()) {
+ QueryInfoCompletionCallback callback = callbacks_.front();
+ callback.Run(success);
+ callbacks_.pop();
+ }
+
+ is_waiting_for_completion_ = false;
}
// The single shared provider instance. We create it only when needed.
static typename base::LazyInstance<
- scoped_refptr<SystemInfoProvider<T> > > single_shared_provider_;
+ scoped_refptr<SystemInfoProvider<T> > > provider_;
// The queue of callbacks waiting for the info querying completion. It is
// maintained on the UI thread.
@@ -170,11 +162,6 @@ class SystemInfoProvider
DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider<T>);
};
-// Static member intialization.
-template<class T>
-typename base::LazyInstance<scoped_refptr<SystemInfoProvider<T> > >
- SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER;
-
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_

Powered by Google App Engine
This is Rietveld 408576698