Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: chrome/browser/extensions/api/system_info/system_info_provider.h

Issue 18290002: [SystemInfo API] Finish TODOs in SystemInfoProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@dev_rewrite_storage_info_api
Patch Set: Fix hongbo's comments Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 26 matching lines...) Expand all
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 callback accepts
44 // two arguments. The first one is the information got already, the second 44 // two arguments. The first one is the information got already, the second
45 // one indicates whether its contents are valid, for example, no error 45 // one indicates whether its contents are valid, for example, no error
46 // occurs in querying the information. 46 // occurs in querying the information.
47 typedef base::Callback<void(const T&, bool)> QueryInfoCompletionCallback; 47 typedef base::Callback<void(bool)> QueryInfoCompletionCallback;
48 typedef std::queue<QueryInfoCompletionCallback> CallbackQueue; 48 typedef std::queue<QueryInfoCompletionCallback> CallbackQueue;
49 49
50 SystemInfoProvider() 50 SystemInfoProvider()
51 : is_waiting_for_completion_(false) { 51 : is_waiting_for_completion_(false) {}
52 worker_pool_token_ =
53 content::BrowserThread::GetBlockingPool()->GetSequenceToken();
54 }
55 52
56 virtual ~SystemInfoProvider() {} 53 virtual ~SystemInfoProvider() {}
57 54
58 // For testing 55 // For testing
59 static void InitializeForTesting( 56 static void InitializeForTesting(
60 scoped_refptr<SystemInfoProvider<T> > provider) { 57 scoped_refptr<SystemInfoProvider<T> > provider) {
61 DCHECK(provider.get() != NULL); 58 DCHECK(provider.get() != NULL);
62 single_shared_provider_.Get() = provider; 59 single_shared_provider_.Get() = provider;
63 } 60 }
64 61
65 // Start to query the system information. Should be called on UI thread. 62 // Start to query the system information. Should be called on UI thread.
66 // The |callback| will get called once the query is completed. 63 // The |callback| will get called once the query is completed.
67 void StartQueryInfo(const QueryInfoCompletionCallback& callback) { 64 void StartQueryInfo(const QueryInfoCompletionCallback& callback) {
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 65 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
69 DCHECK(!callback.is_null()); 66 DCHECK(!callback.is_null());
70 67
71 callbacks_.push(callback); 68 callbacks_.push(callback);
72 69
73 if (is_waiting_for_completion_) 70 if (is_waiting_for_completion_)
74 return; 71 return;
75 72
76 is_waiting_for_completion_ = true; 73 is_waiting_for_completion_ = true;
77 74
78 StartQueryInfoImpl(); 75 StartQueryInfoPostInitialization();
Hongbo Min 2013/07/05 05:01:34 What is the major change except for just renaming
Haojian Wu 2013/07/06 05:31:37 In this way we can make OnQueryCompleted method pr
Hongbo Min 2013/07/06 14:26:35 OK. That's an improvement. But I still feel not co
Haojian Wu 2013/07/08 09:26:35 Done.
79 } 76 }
80 77
81 protected: 78 protected:
82 // Default implementation of querying system information. 79 // We can override this method with our own custom query info Callback
83 // 80 // function which type is bool().
84 // While overriding, there are two things need to do: 81 virtual void StartQueryInfoPostInitialization() {
85 // 1). Bind custom callback function for query system information. 82 base::Callback<bool()> query_info_callback =
86 // 2). Post the custom task to blocking pool. 83 base::Bind(&SystemInfoProvider<T>::QueryInfo, this);
87 virtual void StartQueryInfoImpl() { 84 StartQueryInfoImpl(query_info_callback);
88 base::Closure callback =
89 base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool, this);
90 PostQueryTaskToBlockingPool(FROM_HERE, callback);
91 } 85 }
92 86
93 // Post a task to blocking pool for information querying. 87 // Post the query info task to blocking pool.
94 // 88 void StartQueryInfoImpl(const base::Callback<bool()> & query_info_callback) {
95 // The parameter query_callback should invoke QueryInfo directly or indirectly 89 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
96 // to query the system information and return to UI thread when the query is 90 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_ =
97 // completed. 91 pool->GetSequencedTaskRunnerWithShutdownBehavior(
98 void PostQueryTaskToBlockingPool(const tracked_objects::Location& from_here, 92 pool->GetSequenceToken(),
99 const base::Closure& query_callback) { 93 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 94 // Post the custom query info task to blocking pool for information querying
101 base::SequencedWorkerPool* worker_pool = 95 // and reply with OnQueryCompleted.
102 content::BrowserThread::GetBlockingPool(); 96 base::PostTaskAndReplyWithResult(
103 // The query task posted to the worker pool won't block shutdown, and any 97 blocking_task_runner_,
104 // running query task at shutdown time will be ignored. 98 FROM_HERE,
105 worker_pool->PostSequencedWorkerTaskWithShutdownBehavior( 99 query_info_callback,
106 worker_pool_token_, from_here, query_callback, 100 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this));
107 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
108 } 101 }
109 102
110 // Query the system information synchronously and output the result to the 103 // Query the system information synchronously and the result is info_.
Hongbo Min 2013/07/05 05:01:34 "Query the system information and put the result i
Haojian Wu 2013/07/06 05:31:37 Done.
111 // |info| parameter. The |info| contents MUST be reset firstly in its 104 virtual bool QueryInfo() = 0;
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
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 105
132 // Template function for creating the single shared provider instance. 106 // Template function for creating the single shared provider instance.
133 // Template paramter I is the type of SystemInfoProvider implementation. 107 // Template paramter I is the type of SystemInfoProvider implementation.
134 template<class I> 108 template<class I>
135 static I* GetInstance() { 109 static I* GetInstance() {
136 if (!single_shared_provider_.Get().get()) 110 if (!single_shared_provider_.Get().get())
137 single_shared_provider_.Get() = new I(); 111 single_shared_provider_.Get() = new I();
138 return static_cast<I*>(single_shared_provider_.Get().get()); 112 return static_cast<I*>(single_shared_provider_.Get().get());
139 } 113 }
140 114
141 // The latest information filled up by QueryInfo implementation. Here we 115 // The latest information filled up by QueryInfo implementation. Here we
142 // assume the T is disallowed to copy constructor, aligns with the structure 116 // assume the T is disallowed to copy constructor, aligns with the structure
143 // type generated by IDL parser. 117 // type generated by IDL parser.
144 T info_; 118 T info_;
145 119
146 private: 120 private:
147 // TODO(Haojian): Use PostBlockingPoolTaskAndReply to avoid unnecessary 121 // Called on UI thread. The |success| parameter means whether it succeeds
148 // trampolines trip. 122 // to get the information.
149 void QueryOnWorkerPool() { 123 void OnQueryCompleted(bool success) {
150 bool success = QueryInfo(&info_); 124 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
151 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, 125
152 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this, success)); 126 while (!callbacks_.empty()) {
127 QueryInfoCompletionCallback callback = callbacks_.front();
128 callback.Run(success);
129 callbacks_.pop();
130 }
131
132 is_waiting_for_completion_ = false;
153 } 133 }
154 134
135
Hongbo Min 2013/07/05 05:01:34 nits: no need to extra blank line.
Haojian Wu 2013/07/06 05:31:37 Done.
155 // The single shared provider instance. We create it only when needed. 136 // The single shared provider instance. We create it only when needed.
156 static typename base::LazyInstance< 137 static typename base::LazyInstance<
157 scoped_refptr<SystemInfoProvider<T> > > single_shared_provider_; 138 scoped_refptr<SystemInfoProvider<T> > > single_shared_provider_;
158 139
159 // The queue of callbacks waiting for the info querying completion. It is 140 // The queue of callbacks waiting for the info querying completion. It is
160 // maintained on the UI thread. 141 // maintained on the UI thread.
161 CallbackQueue callbacks_; 142 CallbackQueue callbacks_;
162 143
163 // Indicates if it is waiting for the querying completion. 144 // Indicates if it is waiting for the querying completion.
164 bool is_waiting_for_completion_; 145 bool is_waiting_for_completion_;
165 146
166 // Unqiue sequence token so that the operation of querying inforation can
167 // be executed in order.
168 base::SequencedWorkerPool::SequenceToken worker_pool_token_;
169
170 DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider<T>); 147 DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider<T>);
171 }; 148 };
172 149
173 // Static member intialization. 150 // Static member intialization.
174 template<class T> 151 template<class T>
175 typename base::LazyInstance<scoped_refptr<SystemInfoProvider<T> > > 152 typename base::LazyInstance<scoped_refptr<SystemInfoProvider<T> > >
176 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER; 153 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER;
177 154
178 } // namespace extensions 155 } // namespace extensions
179 156
180 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ 157 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698