| 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 16 matching lines...) Expand all Loading... |
| 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 // | 32 // |
| 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 sequenced worker pool while |is_waiting_for_completion_| is |
| 38 // |is_waiting_for_completion_| is true. | 38 // 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 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); |
| 53 content::BrowserThread::GetBlockingPool()->GetSequenceToken(); | 52 worker_pool_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 53 pool->GetSequenceToken(), |
| 54 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 54 } | 55 } |
| 55 | 56 |
| 56 virtual ~SystemInfoProvider() {} | 57 virtual ~SystemInfoProvider() {} |
| 57 | 58 |
| 59 // Override to do any prepare work on UI thread before |QueryInfo()| gets |
| 60 // called. |
| 61 virtual void PrepareQueryOnUIThread() {} |
| 62 |
| 63 // The parameter |do_query_info_callback| is query info task which is posts to |
| 64 // SystemInfoProvider sequenced worker pool. |
| 65 // |
| 66 // You can do any initial things of *InfoProvider before start to query info. |
| 67 // While overriding this method, |do_query_info_callback| *must* be called |
| 68 // directly or indirectly. |
| 69 // |
| 70 // Sample usage please refer to StorageInfoProvider. |
| 71 virtual void InitializeProvider(const base::Closure& do_query_info_callback) { |
| 72 do_query_info_callback.Run(); |
| 73 } |
| 74 |
| 58 // For testing | 75 // For testing |
| 59 static void InitializeForTesting( | 76 static void InitializeForTesting( |
| 60 scoped_refptr<SystemInfoProvider<T> > provider) { | 77 scoped_refptr<SystemInfoProvider<T> > provider) { |
| 61 DCHECK(provider.get() != NULL); | 78 DCHECK(provider.get() != NULL); |
| 62 single_shared_provider_.Get() = provider; | 79 provider_.Get() = provider; |
| 63 } | 80 } |
| 64 | 81 |
| 65 // Start to query the system information. Should be called on UI thread. | 82 // Start to query the system information. Should be called on UI thread. |
| 66 // The |callback| will get called once the query is completed. | 83 // The |callback| will get called once the query is completed. |
| 84 // |
| 85 // If the parameter |callback| itself calls StartQueryInfo(callback2), |
| 86 // callback2 will be called immediately rather than triggering another call to |
| 87 // the system. |
| 67 void StartQueryInfo(const QueryInfoCompletionCallback& callback) { | 88 void StartQueryInfo(const QueryInfoCompletionCallback& callback) { |
| 68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 89 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 69 DCHECK(!callback.is_null()); | 90 DCHECK(!callback.is_null()); |
| 70 | 91 |
| 71 callbacks_.push(callback); | 92 callbacks_.push(callback); |
| 72 | 93 |
| 73 if (is_waiting_for_completion_) | 94 if (is_waiting_for_completion_) |
| 74 return; | 95 return; |
| 75 | 96 |
| 76 is_waiting_for_completion_ = true; | 97 is_waiting_for_completion_ = true; |
| 77 | 98 |
| 78 StartQueryInfoImpl(); | 99 InitializeProvider(base::Bind( |
| 100 &SystemInfoProvider<T>::StartQueryInfoPostInitialization, this)); |
| 79 } | 101 } |
| 80 | 102 |
| 81 protected: | 103 protected: |
| 82 // Default implementation of querying system information. | 104 // Query the system information synchronously and put the result into |info_|. |
| 83 // | 105 // Return true if no error occurs. |
| 84 // While overriding, there are two things need to do: | 106 // Should be called in the blocking pool. |
| 85 // 1). Bind custom callback function for query system information. | 107 virtual bool QueryInfo() = 0; |
| 86 // 2). Post the custom task to blocking pool. | 108 |
| 87 virtual void StartQueryInfoImpl() { | 109 // Template function for creating the single shared provider instance. |
| 88 base::Closure callback = | 110 // Template paramter I is the type of SystemInfoProvider implementation. |
| 89 base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool, this); | 111 template<class I> |
| 90 PostQueryTaskToBlockingPool(FROM_HERE, callback); | 112 static I* GetInstance() { |
| 113 if (!provider_.Get().get()) |
| 114 provider_.Get() = new I(); |
| 115 return static_cast<I*>(provider_.Get().get()); |
| 91 } | 116 } |
| 92 | 117 |
| 93 // Post a task to blocking pool for information querying. | 118 // The latest information filled up by QueryInfo implementation. Here we |
| 94 // | 119 // assume the T is disallowed to copy constructor, aligns with the structure |
| 95 // The parameter query_callback should invoke QueryInfo directly or indirectly | 120 // type generated by IDL parser. |
| 96 // to query the system information and return to UI thread when the query is | 121 T info_; |
| 97 // completed. | |
| 98 void PostQueryTaskToBlockingPool(const tracked_objects::Location& from_here, | |
| 99 const base::Closure& query_callback) { | |
| 100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 101 base::SequencedWorkerPool* worker_pool = | |
| 102 content::BrowserThread::GetBlockingPool(); | |
| 103 // The query task posted to the worker pool won't block shutdown, and any | |
| 104 // running query task at shutdown time will be ignored. | |
| 105 worker_pool->PostSequencedWorkerTaskWithShutdownBehavior( | |
| 106 worker_pool_token_, from_here, query_callback, | |
| 107 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
| 108 } | |
| 109 | 122 |
| 110 // Query the system information synchronously and output the result to the | 123 private: |
| 111 // |info| parameter. The |info| contents MUST be reset firstly in its | |
| 112 // platform specific implementation. Return true if it succeeds, otherwise | |
| 113 // false is returned. | |
| 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 | 124 // Called on UI thread. The |success| parameter means whether it succeeds |
| 119 // to get the information. | 125 // to get the information. |
| 120 void OnQueryCompleted(bool success) { | 126 void OnQueryCompleted(bool success) { |
| 121 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 127 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 122 | 128 |
| 123 while (!callbacks_.empty()) { | 129 while (!callbacks_.empty()) { |
| 124 QueryInfoCompletionCallback callback = callbacks_.front(); | 130 QueryInfoCompletionCallback callback = callbacks_.front(); |
| 125 callback.Run(info_, success); | 131 callback.Run(success); |
| 126 callbacks_.pop(); | 132 callbacks_.pop(); |
| 127 } | 133 } |
| 128 | 134 |
| 129 is_waiting_for_completion_ = false; | 135 is_waiting_for_completion_ = false; |
| 130 } | 136 } |
| 131 | 137 |
| 132 // Template function for creating the single shared provider instance. | 138 void StartQueryInfoPostInitialization() { |
| 133 // Template paramter I is the type of SystemInfoProvider implementation. | 139 PrepareQueryOnUIThread(); |
| 134 template<class I> | 140 // Post the custom query info task to blocking pool for information querying |
| 135 static I* GetInstance() { | 141 // and reply with OnQueryCompleted. |
| 136 if (!single_shared_provider_.Get().get()) | 142 base::PostTaskAndReplyWithResult( |
| 137 single_shared_provider_.Get() = new I(); | 143 worker_pool_, |
| 138 return static_cast<I*>(single_shared_provider_.Get().get()); | 144 FROM_HERE, |
| 139 } | 145 base::Bind(&SystemInfoProvider<T>::QueryInfo, this), |
| 140 | 146 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this)); |
| 141 // The latest information filled up by QueryInfo implementation. Here we | |
| 142 // assume the T is disallowed to copy constructor, aligns with the structure | |
| 143 // type generated by IDL parser. | |
| 144 T info_; | |
| 145 | |
| 146 private: | |
| 147 // TODO(Haojian): Use PostBlockingPoolTaskAndReply to avoid unnecessary | |
| 148 // trampolines trip. | |
| 149 void QueryOnWorkerPool() { | |
| 150 bool success = QueryInfo(&info_); | |
| 151 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 152 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this, success)); | |
| 153 } | 147 } |
| 154 | 148 |
| 155 // The single shared provider instance. We create it only when needed. | 149 // The single shared provider instance. We create it only when needed. |
| 156 static typename base::LazyInstance< | 150 static typename base::LazyInstance< |
| 157 scoped_refptr<SystemInfoProvider<T> > > single_shared_provider_; | 151 scoped_refptr<SystemInfoProvider<T> > > provider_; |
| 158 | 152 |
| 159 // The queue of callbacks waiting for the info querying completion. It is | 153 // The queue of callbacks waiting for the info querying completion. It is |
| 160 // maintained on the UI thread. | 154 // maintained on the UI thread. |
| 161 CallbackQueue callbacks_; | 155 CallbackQueue callbacks_; |
| 162 | 156 |
| 163 // Indicates if it is waiting for the querying completion. | 157 // Indicates if it is waiting for the querying completion. |
| 164 bool is_waiting_for_completion_; | 158 bool is_waiting_for_completion_; |
| 165 | 159 |
| 166 // Unqiue sequence token so that the operation of querying inforation can | 160 // Sequenced worker pool to make the operation of querying information get |
| 167 // be executed in order. | 161 // executed in order. |
| 168 base::SequencedWorkerPool::SequenceToken worker_pool_token_; | 162 scoped_refptr<base::SequencedTaskRunner> worker_pool_; |
| 169 | 163 |
| 170 DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider<T>); | 164 DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider<T>); |
| 171 }; | 165 }; |
| 172 | 166 |
| 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 | 167 } // namespace extensions |
| 179 | 168 |
| 180 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ | 169 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ |
| OLD | NEW |