Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 #ifndef CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ | 4 #ifndef CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ |
| 5 #define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ | 5 #define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ |
| 6 | 6 |
| 7 #include <queue> | 7 #include <queue> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 class SystemInfoProvider | 33 class SystemInfoProvider |
| 34 : public base::RefCountedThreadSafe<SystemInfoProvider<T> > { | 34 : public base::RefCountedThreadSafe<SystemInfoProvider<T> > { |
| 35 public: | 35 public: |
| 36 // Callback type for completing to get information. The callback accepts | 36 // Callback type for completing to get information. The callback accepts |
| 37 // two arguments. The first one is the information got already, the second | 37 // two arguments. The first one is the information got already, the second |
| 38 // one indicates whether its contents are valid, for example, no error | 38 // one indicates whether its contents are valid, for example, no error |
| 39 // occurs in querying the information. | 39 // occurs in querying the information. |
| 40 typedef base::Callback<void(const T&, bool)> QueryInfoCompletionCallback; | 40 typedef base::Callback<void(const T&, bool)> QueryInfoCompletionCallback; |
| 41 typedef std::queue<QueryInfoCompletionCallback> CallbackQueue; | 41 typedef std::queue<QueryInfoCompletionCallback> CallbackQueue; |
| 42 | 42 |
| 43 SystemInfoProvider() | 43 SystemInfoProvider() : is_waiting_for_completion_(false) { |
|
Lei Zhang
2013/05/28 08:08:07
(Slightly off-topic) Why are all the implementatio
Hongbo Min
2013/05/28 14:24:01
Because the SystemInfoProvider class is just a tem
Lei Zhang
2013/05/28 21:16:44
It doesn't have to be in the .h file. Maybe after
| |
| 44 : is_waiting_for_completion_(false) { | |
| 45 worker_pool_token_ = | 44 worker_pool_token_ = |
| 46 content::BrowserThread::GetBlockingPool()->GetSequenceToken(); | 45 content::BrowserThread::GetBlockingPool()->GetSequenceToken(); |
| 47 } | 46 } |
| 48 | 47 |
| 49 virtual ~SystemInfoProvider() {} | 48 virtual ~SystemInfoProvider() {} |
| 50 | 49 |
| 51 // For testing | 50 // For testing |
| 52 static void InitializeForTesting( | 51 static void InitializeForTesting( |
| 53 scoped_refptr<SystemInfoProvider<T> > provider) { | 52 scoped_refptr<SystemInfoProvider<T> > provider) { |
| 54 DCHECK(provider != NULL); | 53 DCHECK(provider != NULL); |
| 55 single_shared_provider_.Get() = provider; | 54 single_shared_provider_.Get() = provider; |
| 56 } | 55 } |
| 57 | 56 |
| 58 // Start to query the system information. Should be called on UI thread. | 57 // Start to query the system information. Should be called on UI thread. |
| 59 // The |callback| will get called once the query is completed. | 58 // The |callback| will get called once the query is completed. |
| 60 void StartQueryInfo(const QueryInfoCompletionCallback& callback) { | 59 void StartQueryInfo(const QueryInfoCompletionCallback& callback) { |
| 61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 60 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 62 DCHECK(!callback.is_null()); | 61 DCHECK(!callback.is_null()); |
| 63 | 62 |
| 64 callbacks_.push(callback); | 63 callbacks_.push(callback); |
| 65 | |
| 66 if (is_waiting_for_completion_) | 64 if (is_waiting_for_completion_) |
| 67 return; | 65 return; |
| 68 | |
| 69 is_waiting_for_completion_ = true; | 66 is_waiting_for_completion_ = true; |
| 70 | 67 |
| 68 StartQueryInfoImpl(); | |
| 69 } | |
| 70 | |
| 71 protected: | |
| 72 // Default implementation of querying system information. | |
| 73 virtual void StartQueryInfoImpl() { | |
| 74 base::Closure callback = | |
| 75 base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool, this); | |
| 76 PostQueryTaskToBlockingPool(FROM_HERE, callback); | |
| 77 } | |
| 78 | |
| 79 // Post a task to blocking pool for information querying. | |
| 80 void PostQueryTaskToBlockingPool(const tracked_objects::Location& from_here, | |
| 81 const base::Closure& query_callback) { | |
| 82 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 71 base::SequencedWorkerPool* worker_pool = | 83 base::SequencedWorkerPool* worker_pool = |
| 72 content::BrowserThread::GetBlockingPool(); | 84 content::BrowserThread::GetBlockingPool(); |
| 73 // The query task posted to the worker pool won't block shutdown, and any | 85 // The query task posted to the worker pool won't block shutdown, and any |
| 74 // running query task at shutdown time will be ignored. | 86 // running query task at shutdown time will be ignored. |
| 75 worker_pool->PostSequencedWorkerTaskWithShutdownBehavior( | 87 worker_pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 76 worker_pool_token_, | 88 worker_pool_token_, from_here, query_callback, |
| 77 FROM_HERE, | |
| 78 base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool, this), | |
| 79 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | 89 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 80 } | 90 } |
| 81 | 91 |
| 82 protected: | |
| 83 // Query the system information synchronously and output the result to the | 92 // Query the system information synchronously and output the result to the |
| 84 // |info| parameter. The |info| contents MUST be reset firstly in its | 93 // |info| parameter. The |info| contents MUST be reset firstly in its |
| 85 // platform specific implementation. Return true if it succeeds, otherwise | 94 // platform specific implementation. Return true if it succeeds, otherwise |
| 86 // false is returned. | 95 // false is returned. |
| 87 virtual bool QueryInfo(T* info) = 0; | 96 virtual bool QueryInfo(T* info) = 0; |
| 88 | 97 |
| 89 virtual void QueryOnWorkerPool() { | 98 virtual void QueryOnWorkerPool() { |
| 90 bool success = QueryInfo(&info_); | 99 bool success = QueryInfo(&info_); |
| 91 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 100 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 92 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this, success)); | 101 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this, success)); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 }; | 149 }; |
| 141 | 150 |
| 142 // Static member intialization. | 151 // Static member intialization. |
| 143 template<class T> | 152 template<class T> |
| 144 typename base::LazyInstance<scoped_refptr<SystemInfoProvider<T> > > | 153 typename base::LazyInstance<scoped_refptr<SystemInfoProvider<T> > > |
| 145 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER; | 154 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER; |
| 146 | 155 |
| 147 } // namespace extensions | 156 } // namespace extensions |
| 148 | 157 |
| 149 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ | 158 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ |
| OLD | NEW |