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 // | |
| 33 // The class member info_ is accessed on multiple threads, but that the whole | |
| 34 // class is being guarded by SystemInfoProvider. | |
| 35 // | |
| 36 // SystemInfoProvider maintains a variable |is_waiting_for_completion_| on UI | |
|
Jeffrey Yasskin
2013/07/03 21:44:49
To summarize this long paragraph, would it be corr
Haojian Wu
2013/07/04 00:41:17
I think so. Done.
| |
| 37 // thread. The |is_waiting_for_completion_| is used to ensure that only one | |
| 38 // thread is accessing the |info_| member at the same time. | |
| 39 // In case of |is_waiting_for_completion_| is true, the callback will be queued | |
| 40 // and so no entry will be access |info_| until SystemInfoProvider:: | |
| 41 // OnQueryCompleted is called on UI thread. | |
| 32 template<class T> | 42 template<class T> |
| 33 class SystemInfoProvider | 43 class SystemInfoProvider |
| 34 : public base::RefCountedThreadSafe<SystemInfoProvider<T> > { | 44 : public base::RefCountedThreadSafe<SystemInfoProvider<T> > { |
| 35 public: | 45 public: |
| 36 // Callback type for completing to get information. The callback accepts | 46 // Callback type for completing to get information. The callback accepts |
| 37 // two arguments. The first one is the information got already, the second | 47 // two arguments. The first one is the information got already, the second |
| 38 // one indicates whether its contents are valid, for example, no error | 48 // one indicates whether its contents are valid, for example, no error |
| 39 // occurs in querying the information. | 49 // occurs in querying the information. |
| 40 typedef base::Callback<void(const T&, bool)> QueryInfoCompletionCallback; | 50 typedef base::Callback<void(const T&, bool)> QueryInfoCompletionCallback; |
| 41 typedef std::queue<QueryInfoCompletionCallback> CallbackQueue; | 51 typedef std::queue<QueryInfoCompletionCallback> CallbackQueue; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 71 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 62 DCHECK(!callback.is_null()); | 72 DCHECK(!callback.is_null()); |
| 63 | 73 |
| 64 callbacks_.push(callback); | 74 callbacks_.push(callback); |
| 65 | 75 |
| 66 if (is_waiting_for_completion_) | 76 if (is_waiting_for_completion_) |
| 67 return; | 77 return; |
| 68 | 78 |
| 69 is_waiting_for_completion_ = true; | 79 is_waiting_for_completion_ = true; |
| 70 | 80 |
| 81 StartQueryInfoImpl(); | |
| 82 } | |
| 83 | |
| 84 protected: | |
| 85 // Default implementation of querying system information. | |
| 86 // | |
| 87 // Before overriding this function, you should know what you intend to do | |
|
Jeffrey Yasskin
2013/07/03 21:44:49
This sentence doesn't convey any information, so p
Haojian Wu
2013/07/04 00:41:17
Done.
| |
| 88 // exactly. | |
| 89 // While overriding, there are two things need to do: | |
| 90 // 1). Bind custom callback function for query system information. | |
| 91 // 2). Post the custom task to blocking pool. | |
| 92 virtual void StartQueryInfoImpl() { | |
| 93 base::Closure callback = | |
| 94 base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool, this); | |
| 95 PostQueryTaskToBlockingPool(FROM_HERE, callback); | |
| 96 } | |
| 97 | |
| 98 // Post a task to blocking pool for information querying. | |
| 99 // | |
| 100 // The parameter query_callback should invoke QueryInfo directly or indirectly | |
| 101 // to query the system information and return to UI thread when the query is | |
| 102 // completed. | |
| 103 void PostQueryTaskToBlockingPool(const tracked_objects::Location& from_here, | |
| 104 const base::Closure& query_callback) { | |
| 105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 71 base::SequencedWorkerPool* worker_pool = | 106 base::SequencedWorkerPool* worker_pool = |
| 72 content::BrowserThread::GetBlockingPool(); | 107 content::BrowserThread::GetBlockingPool(); |
| 73 // The query task posted to the worker pool won't block shutdown, and any | 108 // The query task posted to the worker pool won't block shutdown, and any |
| 74 // running query task at shutdown time will be ignored. | 109 // running query task at shutdown time will be ignored. |
| 75 worker_pool->PostSequencedWorkerTaskWithShutdownBehavior( | 110 worker_pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 76 worker_pool_token_, | 111 worker_pool_token_, from_here, query_callback, |
| 77 FROM_HERE, | |
| 78 base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool, this), | |
| 79 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | 112 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 80 } | 113 } |
| 81 | 114 |
| 82 protected: | |
| 83 // Query the system information synchronously and output the result to the | 115 // Query the system information synchronously and output the result to the |
| 84 // |info| parameter. The |info| contents MUST be reset firstly in its | 116 // |info| parameter. The |info| contents MUST be reset firstly in its |
| 85 // platform specific implementation. Return true if it succeeds, otherwise | 117 // platform specific implementation. Return true if it succeeds, otherwise |
| 86 // false is returned. | 118 // false is returned. |
| 119 // TODO(Haojian): Remove the parameter T-typed pointer, replacing with void | |
| 120 // QueryInfo(). | |
| 87 virtual bool QueryInfo(T* info) = 0; | 121 virtual bool QueryInfo(T* info) = 0; |
| 88 | 122 |
| 89 virtual void QueryOnWorkerPool() { | 123 // TODO(Haojian): Use PostBlockingPoolTaskAndReply to avoid unnecessary |
| 124 // trampolines trip. | |
| 125 void QueryOnWorkerPool() { | |
| 90 bool success = QueryInfo(&info_); | 126 bool success = QueryInfo(&info_); |
| 91 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 127 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 92 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this, success)); | 128 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this, success)); |
| 93 } | 129 } |
| 94 | 130 |
| 95 // Called on UI thread. The |success| parameter means whether it succeeds | 131 // Called on UI thread. The |success| parameter means whether it succeeds |
| 96 // to get the information. | 132 // to get the information. |
| 97 virtual void OnQueryCompleted(bool success) { | 133 virtual void OnQueryCompleted(bool success) { |
| 98 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 134 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 99 | 135 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 }; | 176 }; |
| 141 | 177 |
| 142 // Static member intialization. | 178 // Static member intialization. |
| 143 template<class T> | 179 template<class T> |
| 144 typename base::LazyInstance<scoped_refptr<SystemInfoProvider<T> > > | 180 typename base::LazyInstance<scoped_refptr<SystemInfoProvider<T> > > |
| 145 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER; | 181 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER; |
| 146 | 182 |
| 147 } // namespace extensions | 183 } // namespace extensions |
| 148 | 184 |
| 149 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ | 185 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ |
| OLD | NEW |