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 // The class member |info_| is accessed on multiple threads, but that the whole | 33 // The class member |info_| is accessed on multiple threads, but that the whole |
| 34 // class is being guarded by SystemInfoProvider. | 34 // class is being guarded by SystemInfoProvider. |
| 35 // | 35 // |
| 36 // |info_| is accessed on the UI thread while |is_waiting_for_completion_| is | 36 // |info_| is accessed on the UI thread while |is_waiting_for_completion_| is |
| 37 // false and on the BlockingPool under |worker_pool_token_| while | 37 // false and on the BlockingPool under |worker_pool_token_| while |
| 38 // |is_waiting_for_completion_| is true. | 38 // |is_waiting_for_completion_| is true. |
| 39 template<class T> | 39 template<class T> |
| 40 class SystemInfoProvider | 40 class SystemInfoProvider |
| 41 : public base::RefCountedThreadSafe<SystemInfoProvider<T> > { | 41 : public base::RefCountedThreadSafe<SystemInfoProvider<T> > { |
| 42 public: | 42 public: |
| 43 // Callback type for completing to get information. The callback accepts | 43 // Callback type for completing to get information. The argument indicates |
| 44 // two arguments. The first one is the information got already, the second | 44 // whether its contents are valid, for example, no error occurs in querying |
| 45 // one indicates whether its contents are valid, for example, no error | 45 // the information. |
| 46 // occurs in querying the information. | 46 typedef base::Callback<void(bool)> QueryInfoCompletionCallback; |
| 47 typedef base::Callback<void(const T&, bool)> QueryInfoCompletionCallback; | |
| 48 typedef std::queue<QueryInfoCompletionCallback> CallbackQueue; | 47 typedef std::queue<QueryInfoCompletionCallback> CallbackQueue; |
| 49 | 48 |
| 50 SystemInfoProvider() | 49 SystemInfoProvider() |
| 51 : is_waiting_for_completion_(false) { | 50 : is_waiting_for_completion_(false) { |
| 52 worker_pool_token_ = | 51 worker_pool_token_ = |
| 53 content::BrowserThread::GetBlockingPool()->GetSequenceToken(); | 52 content::BrowserThread::GetBlockingPool()->GetSequenceToken(); |
|
Jeffrey Yasskin
2013/07/09 18:19:59
Change worker_pool_token_ to be declared as "scope
Haojian Wu
2013/07/10 03:11:37
Done.
| |
| 54 } | 53 } |
| 55 | 54 |
| 56 virtual ~SystemInfoProvider() {} | 55 virtual ~SystemInfoProvider() {} |
| 57 | 56 |
| 58 // For testing | 57 // For testing |
| 59 static void InitializeForTesting( | 58 static void InitializeForTesting( |
| 60 scoped_refptr<SystemInfoProvider<T> > provider) { | 59 scoped_refptr<SystemInfoProvider<T> > provider) { |
| 61 DCHECK(provider.get() != NULL); | 60 DCHECK(provider.get() != NULL); |
| 62 single_shared_provider_.Get() = provider; | 61 provider_.Get() = provider; |
| 63 } | 62 } |
| 64 | 63 |
| 65 // Start to query the system information. Should be called on UI thread. | 64 // Start to query the system information. Should be called on UI thread. |
| 66 // The |callback| will get called once the query is completed. | 65 // The |callback| will get called once the query is completed. |
| 66 // | |
| 67 // If the parameter |callback| itself calls StartQueryInfo(callback2), | |
| 68 // callback2 will be called immediately rather than triggering another call to | |
| 69 // the system. | |
| 67 void StartQueryInfo(const QueryInfoCompletionCallback& callback) { | 70 void StartQueryInfo(const QueryInfoCompletionCallback& callback) { |
| 68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 71 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 69 DCHECK(!callback.is_null()); | 72 DCHECK(!callback.is_null()); |
| 70 | 73 |
| 71 callbacks_.push(callback); | 74 callbacks_.push(callback); |
| 72 | 75 |
| 73 if (is_waiting_for_completion_) | 76 if (is_waiting_for_completion_) |
| 74 return; | 77 return; |
| 75 | 78 |
| 76 is_waiting_for_completion_ = true; | 79 is_waiting_for_completion_ = true; |
| 77 | 80 |
| 78 StartQueryInfoImpl(); | 81 StartQueryInfoImpl(); |
| 79 } | 82 } |
| 80 | 83 |
| 81 protected: | 84 protected: |
| 82 // Default implementation of querying system information. | 85 // Post the custom query info task to the sequenced blocking pool. |
| 83 // | 86 // SystemInfo API uses QueryInfo to retrieve system information in blocking |
| 84 // While overriding, there are two things need to do: | 87 // pool defaultly. |
| 85 // 1). Bind custom callback function for query system information. | 88 // PostQueryTaskToBlockingPool must be called directly or indirectly to make |
| 86 // 2). Post the custom task to blocking pool. | 89 // the custom query info task run in sequence while overriding this method. |
| 87 virtual void StartQueryInfoImpl() { | 90 virtual void StartQueryInfoImpl() { |
| 88 base::Closure callback = | 91 PostQueryTaskToBlockingPool(base::Bind(&SystemInfoProvider<T>::QueryInfo, |
| 89 base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool, this); | 92 this)); |
| 90 PostQueryTaskToBlockingPool(FROM_HERE, callback); | |
| 91 } | 93 } |
| 92 | 94 |
| 93 // Post a task to blocking pool for information querying. | 95 // Post the query info task to blocking pool and will call OnQueryCompleted |
| 94 // | 96 // after the query task is completed. |
| 95 // The parameter query_callback should invoke QueryInfo directly or indirectly | 97 void PostQueryTaskToBlockingPool( |
| 96 // to query the system information and return to UI thread when the query is | 98 const base::Callback<bool()> & query_info_callback) { |
| 97 // completed. | 99 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); |
| 98 void PostQueryTaskToBlockingPool(const tracked_objects::Location& from_here, | 100 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_ = |
| 99 const base::Closure& query_callback) { | 101 pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 102 worker_pool_token_, |
| 101 base::SequencedWorkerPool* worker_pool = | 103 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 102 content::BrowserThread::GetBlockingPool(); | 104 // Post the custom query info task to blocking pool for information querying |
| 103 // The query task posted to the worker pool won't block shutdown, and any | 105 // and reply with OnQueryCompleted. |
| 104 // running query task at shutdown time will be ignored. | 106 base::PostTaskAndReplyWithResult( |
| 105 worker_pool->PostSequencedWorkerTaskWithShutdownBehavior( | 107 blocking_task_runner_, |
| 106 worker_pool_token_, from_here, query_callback, | 108 FROM_HERE, |
| 107 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | 109 query_info_callback, |
| 110 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this)); | |
| 108 } | 111 } |
| 109 | 112 |
| 110 // Query the system information synchronously and output the result to the | 113 // Query the system information synchronously and put the result into |info_|. |
| 111 // |info| parameter. The |info| contents MUST be reset firstly in its | 114 // Return true if no error occurs. |
| 112 // platform specific implementation. Return true if it succeeds, otherwise | 115 // Should be called in the blocking pool. |
| 113 // false is returned. | 116 virtual bool QueryInfo() = 0; |
| 114 // TODO(Haojian): Remove the parameter T-typed pointer, replacing with void | |
| 115 // QueryInfo(). | |
| 116 virtual bool QueryInfo(T* info) = 0; | |
| 117 | |
| 118 // Called on UI thread. The |success| parameter means whether it succeeds | |
| 119 // to get the information. | |
| 120 void OnQueryCompleted(bool success) { | |
| 121 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 122 | |
| 123 while (!callbacks_.empty()) { | |
| 124 QueryInfoCompletionCallback callback = callbacks_.front(); | |
| 125 callback.Run(info_, success); | |
| 126 callbacks_.pop(); | |
| 127 } | |
| 128 | |
| 129 is_waiting_for_completion_ = false; | |
| 130 } | |
| 131 | 117 |
| 132 // Template function for creating the single shared provider instance. | 118 // Template function for creating the single shared provider instance. |
| 133 // Template paramter I is the type of SystemInfoProvider implementation. | 119 // Template paramter I is the type of SystemInfoProvider implementation. |
| 134 template<class I> | 120 template<class I> |
| 135 static I* GetInstance() { | 121 static I* GetInstance() { |
| 136 if (!single_shared_provider_.Get().get()) | 122 if (!provider_.Get().get()) |
| 137 single_shared_provider_.Get() = new I(); | 123 provider_.Get() = new I(); |
| 138 return static_cast<I*>(single_shared_provider_.Get().get()); | 124 return static_cast<I*>(provider_.Get().get()); |
| 139 } | 125 } |
| 140 | 126 |
| 141 // The latest information filled up by QueryInfo implementation. Here we | 127 // The latest information filled up by QueryInfo implementation. Here we |
| 142 // assume the T is disallowed to copy constructor, aligns with the structure | 128 // assume the T is disallowed to copy constructor, aligns with the structure |
| 143 // type generated by IDL parser. | 129 // type generated by IDL parser. |
| 144 T info_; | 130 T info_; |
| 145 | 131 |
| 146 private: | 132 private: |
| 147 // TODO(Haojian): Use PostBlockingPoolTaskAndReply to avoid unnecessary | 133 // Called on UI thread. The |success| parameter means whether it succeeds |
| 148 // trampolines trip. | 134 // to get the information. |
| 149 void QueryOnWorkerPool() { | 135 void OnQueryCompleted(bool success) { |
| 150 bool success = QueryInfo(&info_); | 136 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 151 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 137 |
| 152 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this, success)); | 138 while (!callbacks_.empty()) { |
| 139 QueryInfoCompletionCallback callback = callbacks_.front(); | |
| 140 callback.Run(success); | |
| 141 callbacks_.pop(); | |
| 142 } | |
| 143 | |
| 144 is_waiting_for_completion_ = false; | |
| 153 } | 145 } |
| 154 | 146 |
| 155 // The single shared provider instance. We create it only when needed. | 147 // The single shared provider instance. We create it only when needed. |
| 156 static typename base::LazyInstance< | 148 static typename base::LazyInstance< |
| 157 scoped_refptr<SystemInfoProvider<T> > > single_shared_provider_; | 149 scoped_refptr<SystemInfoProvider<T> > > provider_; |
| 158 | 150 |
| 159 // The queue of callbacks waiting for the info querying completion. It is | 151 // The queue of callbacks waiting for the info querying completion. It is |
| 160 // maintained on the UI thread. | 152 // maintained on the UI thread. |
| 161 CallbackQueue callbacks_; | 153 CallbackQueue callbacks_; |
| 162 | 154 |
| 163 // Indicates if it is waiting for the querying completion. | 155 // Indicates if it is waiting for the querying completion. |
| 164 bool is_waiting_for_completion_; | 156 bool is_waiting_for_completion_; |
| 165 | 157 |
| 166 // Unqiue sequence token so that the operation of querying inforation can | 158 // Unqiue sequence token so that the operation of querying inforation can |
| 167 // be executed in order. | 159 // be executed in order. |
| 168 base::SequencedWorkerPool::SequenceToken worker_pool_token_; | 160 base::SequencedWorkerPool::SequenceToken worker_pool_token_; |
| 169 | 161 |
| 170 DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider<T>); | 162 DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider<T>); |
| 171 }; | 163 }; |
| 172 | 164 |
| 173 // Static member intialization. | |
| 174 template<class T> | |
| 175 typename base::LazyInstance<scoped_refptr<SystemInfoProvider<T> > > | |
| 176 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER; | |
| 177 | |
| 178 } // namespace extensions | 165 } // namespace extensions |
| 179 | 166 |
| 180 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ | 167 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ |
| OLD | NEW |