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 11 matching lines...) Expand all Loading... | |
| 22 // The SystemInfoProvider is designed to query system information on the worker | 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 | 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 | 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 | 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 | 26 // thread. In this way, it avoids frequent querying operation in case of lots |
| 27 // of query requests, e.g. calling systemInfo.cpu.get repeatedly in an | 27 // of query requests, e.g. calling systemInfo.cpu.get repeatedly in an |
| 28 // extension process. | 28 // extension process. |
| 29 // | 29 // |
| 30 // Template parameter T is the system information type. It could be the | 30 // Template parameter T is the system information type. It could be the |
| 31 // structure type generated by IDL parser. | 31 // structure type generated by IDL parser. |
| 32 template<class T> | 32 template<class T> |
|
Greg Billock
2013/06/18 18:24:52
I'm not a fan of templates here. Given that we nee
Haojian Wu
2013/06/21 05:49:04
hongbo@, how about your idea? Should we refractor
Hongbo Min
2013/06/22 09:52:28
Original thought on type T is, it is a type genera
Greg Billock
2013/06/24 16:55:38
I see. Do you foresee enough specializations to ma
| |
| 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() |
| 44 : is_waiting_for_completion_(false) { | 44 : is_waiting_for_completion_(false) { |
| 45 worker_pool_token_ = | 45 worker_pool_token_ = |
| 46 content::BrowserThread::GetBlockingPool()->GetSequenceToken(); | 46 content::BrowserThread::GetBlockingPool()->GetSequenceToken(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 virtual ~SystemInfoProvider() {} | 49 virtual ~SystemInfoProvider() {} |
| 50 | 50 |
| 51 // For testing | 51 // For testing |
| 52 static void InitializeForTesting( | 52 static void InitializeForTesting( |
| 53 scoped_refptr<SystemInfoProvider<T> > provider) { | 53 scoped_refptr<SystemInfoProvider<T> > provider) { |
| 54 DCHECK(provider.get() != NULL); | 54 DCHECK(provider.get() != NULL); |
| 55 single_shared_provider_.Get() = provider; | 55 single_shared_provider_.Get() = provider; |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Start to query the system information. Should be called on UI thread. | 58 // Start to query the system information. Should be called on UI thread. |
| 59 // The |callback| will get called once the query is completed. | 59 // The |callback| will get called once the query is completed. |
| 60 void StartQueryInfo(const QueryInfoCompletionCallback& callback) { | 60 void StartQueryInfo(const QueryInfoCompletionCallback& callback) { |
|
Greg Billock
2013/06/18 18:24:52
This can go in a .cc file, along with non-template
Haojian Wu
2013/06/21 05:49:04
I will create a new CL to separate the declaration
Greg Billock
2013/06/24 16:55:38
Sounds good.
| |
| 61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 62 DCHECK(!callback.is_null()); | 62 DCHECK(!callback.is_null()); |
| 63 | 63 |
| 64 callbacks_.push(callback); | 64 callbacks_.push(callback); |
| 65 | 65 |
| 66 if (is_waiting_for_completion_) | 66 if (is_waiting_for_completion_) |
| 67 return; | 67 return; |
| 68 | 68 |
| 69 is_waiting_for_completion_ = true; | 69 is_waiting_for_completion_ = true; |
| 70 | 70 |
| 71 StartQueryInfoImpl(); | |
| 72 } | |
| 73 | |
| 74 protected: | |
| 75 // Default implementation of querying system information. | |
| 76 virtual void StartQueryInfoImpl() { | |
| 77 base::Closure callback = | |
| 78 base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool, this); | |
| 79 PostQueryTaskToBlockingPool(FROM_HERE, callback); | |
| 80 } | |
| 81 | |
| 82 // Post a task to blocking pool for information querying. | |
| 83 void PostQueryTaskToBlockingPool(const tracked_objects::Location& from_here, | |
| 84 const base::Closure& query_callback) { | |
| 85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 71 base::SequencedWorkerPool* worker_pool = | 86 base::SequencedWorkerPool* worker_pool = |
| 72 content::BrowserThread::GetBlockingPool(); | 87 content::BrowserThread::GetBlockingPool(); |
| 73 // The query task posted to the worker pool won't block shutdown, and any | 88 // The query task posted to the worker pool won't block shutdown, and any |
| 74 // running query task at shutdown time will be ignored. | 89 // running query task at shutdown time will be ignored. |
| 75 worker_pool->PostSequencedWorkerTaskWithShutdownBehavior( | 90 worker_pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 76 worker_pool_token_, | 91 worker_pool_token_, from_here, query_callback, |
| 77 FROM_HERE, | |
| 78 base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool, this), | |
| 79 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | 92 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 80 } | 93 } |
| 81 | 94 |
| 82 protected: | |
| 83 // Query the system information synchronously and output the result to the | 95 // Query the system information synchronously and output the result to the |
| 84 // |info| parameter. The |info| contents MUST be reset firstly in its | 96 // |info| parameter. The |info| contents MUST be reset firstly in its |
| 85 // platform specific implementation. Return true if it succeeds, otherwise | 97 // platform specific implementation. Return true if it succeeds, otherwise |
| 86 // false is returned. | 98 // false is returned. |
| 87 virtual bool QueryInfo(T* info) = 0; | 99 virtual bool QueryInfo(T* info) = 0; |
| 88 | 100 |
| 89 virtual void QueryOnWorkerPool() { | 101 virtual void QueryOnWorkerPool() { |
|
Greg Billock
2013/06/18 18:24:52
Instead of trampolines on both sides, could we use
Haojian Wu
2013/06/21 05:49:04
hongbo@, your idea?
Hongbo Min
2013/06/22 09:52:28
Thanks for Billock to raise these improvements.
I
Haojian Wu
2013/06/23 07:59:16
Done. Add TODO comments.
| |
| 90 bool success = QueryInfo(&info_); | 102 bool success = QueryInfo(&info_); |
| 91 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 103 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 92 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this, success)); | 104 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this, success)); |
| 93 } | 105 } |
| 94 | 106 |
| 95 // Called on UI thread. The |success| parameter means whether it succeeds | 107 // Called on UI thread. The |success| parameter means whether it succeeds |
| 96 // to get the information. | 108 // to get the information. |
| 97 virtual void OnQueryCompleted(bool success) { | 109 virtual void OnQueryCompleted(bool success) { |
| 98 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 99 | 111 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 }; | 152 }; |
| 141 | 153 |
| 142 // Static member intialization. | 154 // Static member intialization. |
| 143 template<class T> | 155 template<class T> |
| 144 typename base::LazyInstance<scoped_refptr<SystemInfoProvider<T> > > | 156 typename base::LazyInstance<scoped_refptr<SystemInfoProvider<T> > > |
| 145 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER; | 157 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER; |
| 146 | 158 |
| 147 } // namespace extensions | 159 } // namespace extensions |
| 148 | 160 |
| 149 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ | 161 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ |
| OLD | NEW |