Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 #ifndef CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_PROVIDER_H_ | |
| 5 #define CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_PROVIDER_H_ | |
| 6 | |
| 7 #include <queue> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback.h" | |
| 11 #include "base/lazy_instance.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/threading/sequenced_worker_pool.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 | |
| 16 namespace extensions { | |
| 17 | |
| 18 // A generic template for all kinds of system information provider. Each kind | |
| 19 // of SystemInfoProvider is a single shared instance. It is created if needed, | |
| 20 // and destroyed at exit time. This is done via LazyInstance and scoped_ptr. | |
| 21 // | |
| 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 | |
| 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 | |
| 26 // thread. In this way, it avoids frequent querying operation in case of lots | |
| 27 // of query requests are arrived, e.g. calling systemInfo.cpu.get repeatedly | |
| 28 // in renderer. | |
| 29 // | |
| 30 // Template parameter T is the system information type. It could be the | |
| 31 // structure type generated by IDL parser. | |
| 32 template<class T> | |
| 33 class SystemInfoProvider { | |
| 34 public: | |
| 35 // Callback type for completing to get information. The callback accepts | |
| 36 // two arguments. The first one is the information got already, the second | |
| 37 // one indicates whether its contents are valid, for example, no error | |
| 38 // occurs in querying the information. | |
| 39 typedef base::Callback<void(const T&, bool)> GetInfoCompletionCallback; | |
| 40 typedef std::queue<GetInfoCompletionCallback> CallbackQueue; | |
| 41 | |
| 42 SystemInfoProvider() | |
| 43 : is_waiting_for_completion_(false) { | |
| 44 worker_pool_token_ = | |
| 45 content::BrowserThread::GetBlockingPool()->GetSequenceToken(); | |
| 46 } | |
| 47 | |
| 48 // Static method to get the single shared instance. Should be implemented | |
| 49 // in the impl file for each kind of provider. | |
| 50 static SystemInfoProvider<T>* Get(); | |
| 51 | |
| 52 // For testing | |
| 53 static void InitializeForTesting(SystemInfoProvider<T>* provider) { | |
| 54 DCHECK(provider != NULL); | |
| 55 single_shared_provider_.Get().reset(provider); | |
| 56 } | |
| 57 | |
| 58 // Start to query the system information. Should be called on UI thread. | |
| 59 // The |callback| will get called once the query is completed. | |
| 60 virtual void StartGetInfo(const GetInfoCompletionCallback& callback) { | |
| 61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 62 DCHECK(!callback.is_null()); | |
| 63 | |
| 64 callbacks_.push(callback); | |
| 65 | |
| 66 if (is_waiting_for_completion_) | |
| 67 return; | |
| 68 | |
| 69 is_waiting_for_completion_ = true; | |
| 70 | |
| 71 base::SequencedWorkerPool* worker_pool = | |
| 72 content::BrowserThread::GetBlockingPool(); | |
| 73 // Post the query task to the worker pool with CONTINUE_ON_SHUTDOWN | |
|
Mihai Parparita -not on Chrome
2012/08/21 01:14:52
This comment just says what the code says. Either
Hongbo Min
2012/08/21 03:07:11
Done.
| |
| 74 // shutdown behavior. | |
| 75 worker_pool->PostSequencedWorkerTaskWithShutdownBehavior( | |
| 76 worker_pool_token_, | |
| 77 FROM_HERE, | |
| 78 base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool, | |
| 79 base::Unretained(this)), | |
| 80 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
| 81 } | |
| 82 | |
| 83 virtual ~SystemInfoProvider() {} | |
| 84 | |
| 85 protected: | |
| 86 // Query the information on worker pool. | |
|
Mihai Parparita -not on Chrome
2012/08/21 01:14:52
Another redundant comment.
Hongbo Min
2012/08/21 03:07:11
Done.
| |
| 87 virtual void QueryOnWorkerPool() { | |
| 88 bool success = QueryInfo(); | |
| 89 // Notify UI thread once it is completed. | |
|
Mihai Parparita -not on Chrome
2012/08/21 01:14:52
Ditto.
Hongbo Min
2012/08/21 03:07:11
Done.
| |
| 90 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 91 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, | |
| 92 base::Unretained(this), success)); | |
| 93 } | |
| 94 | |
| 95 // Platform specific implementation for how to query the information and | |
| 96 // save it into |info_|. The |info_| contents must be reset firstly in the | |
| 97 // concrete implementation. Return true if it succeeds, otherwise false is | |
| 98 // returned. | |
| 99 virtual bool QueryInfo() = 0; | |
| 100 | |
| 101 // Called on UI thread. The |success| parameter means whether it succeeds | |
| 102 // to get the information. | |
| 103 virtual void OnQueryCompleted(bool success) { | |
| 104 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 105 | |
| 106 // Traverse the queue to invoke the pending callbacks. | |
|
Mihai Parparita -not on Chrome
2012/08/21 01:14:52
Ditto.
Hongbo Min
2012/08/21 03:07:11
Done.
| |
| 107 while (!callbacks_.empty()) { | |
| 108 GetInfoCompletionCallback callback = callbacks_.front(); | |
| 109 callback.Run(info_, success); | |
| 110 callbacks_.pop(); | |
| 111 } | |
| 112 | |
| 113 // Reset waiting status. | |
|
Mihai Parparita -not on Chrome
2012/08/21 01:14:52
Ditto.
| |
| 114 is_waiting_for_completion_ = false; | |
| 115 } | |
| 116 | |
| 117 // Template function for creating the single shared provider instance. | |
| 118 // Template paramter I is the type of SystemInfoProvider implementation. | |
| 119 template<class I> | |
| 120 static SystemInfoProvider<T>* GetInstance() { | |
| 121 if (!single_shared_provider_.Get().get()) { | |
| 122 I* impl = new I(); | |
| 123 single_shared_provider_.Get().reset(impl); | |
| 124 } | |
| 125 return single_shared_provider_.Get().get(); | |
| 126 } | |
| 127 | |
| 128 // The latest information filled up by QueryInfo implementation. Here we | |
| 129 // assume the T is disallowed to copy constructor, aligns with the structure | |
| 130 // type generated by IDL parser. It leads to the |info_| can not be used as | |
| 131 // an argument for base::Bind. | |
|
Mihai Parparita -not on Chrome
2012/08/21 01:14:52
How is the base::Bind discussion relevant?
Hongbo Min
2012/08/21 03:07:11
Removed it now.
The origin implementation is to a
Hongbo Min
2012/08/21 05:16:39
Correct: It can not be used as a callback paramete
| |
| 132 T info_; | |
| 133 | |
| 134 private: | |
| 135 // The single shared provider instance. We create it only when needed. | |
| 136 static typename base::LazyInstance< | |
| 137 scoped_ptr<SystemInfoProvider<T> > > single_shared_provider_; | |
| 138 | |
| 139 // The queue of callbacks waiting for the info querying completion. It is | |
| 140 // maintained on the UI thread. | |
| 141 CallbackQueue callbacks_; | |
| 142 | |
| 143 // Indicates if it is waiting for the querying completion. | |
| 144 bool is_waiting_for_completion_; | |
| 145 | |
| 146 // Unqiue sequence token so that the operation of querying inforation can | |
| 147 // be executed in order. | |
| 148 base::SequencedWorkerPool::SequenceToken worker_pool_token_; | |
| 149 | |
| 150 DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider<T>); | |
| 151 }; | |
| 152 | |
| 153 // Static member intialization. | |
| 154 template<class T> | |
| 155 typename base::LazyInstance<scoped_ptr<SystemInfoProvider<T> > > | |
|
Mihai Parparita -not on Chrome
2012/08/21 01:14:52
Do you actually need to use scoped_ptr here? From
Hongbo
2012/08/21 01:29:37
The reason I use scoped_ptr is, the SystemInfoProv
| |
| 156 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER; | |
| 157 | |
| 158 } // namespace extensions | |
| 159 | |
| 160 #endif // CHROME_BROWSER_EXTENSIONS_SYSTEM_INFO_PROVIDER_H_ | |
| OLD | NEW |