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

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: Update 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 22 matching lines...) Expand all
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 callback accepts
Greg Billock 2013/07/08 18:05:06 Comment is now stale.
Haojian Wu 2013/07/09 13:45:04 Done.
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 StartQueryInfoImpl();
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
Greg Billock 2013/07/08 18:05:06 I think you can just get rid of this comment.
Jeffrey Yasskin 2013/07/08 19:09:49 If this function stays virtual, it does need a com
Haojian Wu 2013/07/09 13:45:04 Done. Update the comments.
83 // 80 // function which type is bool().
84 // While overriding, there are two things need to do:
85 // 1). Bind custom callback function for query system information.
86 // 2). Post the custom task to blocking pool.
87 virtual void StartQueryInfoImpl() { 81 virtual void StartQueryInfoImpl() {
88 base::Closure callback = 82 base::Callback<bool()> query_info_callback =
Greg Billock 2013/07/08 18:05:06 You can put this in the PostQueryTaskToBlockingPoo
Haojian Wu 2013/07/09 13:45:04 Done.
89 base::Bind(&SystemInfoProvider<T>::QueryOnWorkerPool, this); 83 base::Bind(&SystemInfoProvider<T>::QueryInfo, this);
90 PostQueryTaskToBlockingPool(FROM_HERE, callback); 84 PostQueryTaskToBlockingPool(query_info_callback);
91 } 85 }
92 86
93 // Post a task to blocking pool for information querying. 87 // Post the query info task to blocking pool.
Greg Billock 2013/07/08 18:05:06 Your method name says this. Delete comment or repl
Haojian Wu 2013/07/09 13:45:04 Done.
94 // 88 void PostQueryTaskToBlockingPool(
95 // The parameter query_callback should invoke QueryInfo directly or indirectly 89 const base::Callback<bool()> & query_info_callback) {
96 // to query the system information and return to UI thread when the query is 90 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
97 // completed. 91 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_ =
98 void PostQueryTaskToBlockingPool(const tracked_objects::Location& from_here, 92 pool->GetSequencedTaskRunnerWithShutdownBehavior(
99 const base::Closure& query_callback) { 93 pool->GetSequenceToken(),
Jeffrey Yasskin 2013/07/08 19:09:49 Although this is one reading of what I suggested,
Haojian Wu 2013/07/09 13:45:04 Done.
100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 94 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
101 base::SequencedWorkerPool* worker_pool = 95 // Post the custom query info task to blocking pool for information querying
102 content::BrowserThread::GetBlockingPool(); 96 // and reply with OnQueryCompleted.
103 // The query task posted to the worker pool won't block shutdown, and any 97 base::PostTaskAndReplyWithResult(
104 // running query task at shutdown time will be ignored. 98 blocking_task_runner_,
105 worker_pool->PostSequencedWorkerTaskWithShutdownBehavior( 99 FROM_HERE,
106 worker_pool_token_, from_here, query_callback, 100 query_info_callback,
107 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); 101 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this));
108 } 102 }
109 103
110 // Query the system information synchronously and output the result to the 104 // Query the system information synchronously and put the result into |info_|.
111 // |info| parameter. The |info| contents MUST be reset firstly in its 105 // Return true if no error occurs.
112 // platform specific implementation. Return true if it succeeds, otherwise 106 virtual bool QueryInfo() = 0;
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 107
132 // Template function for creating the single shared provider instance. 108 // Template function for creating the single shared provider instance.
133 // Template paramter I is the type of SystemInfoProvider implementation. 109 // Template paramter I is the type of SystemInfoProvider implementation.
134 template<class I> 110 template<class I>
135 static I* GetInstance() { 111 static I* GetInstance() {
136 if (!single_shared_provider_.Get().get()) 112 if (!single_shared_provider_.Get().get())
137 single_shared_provider_.Get() = new I(); 113 single_shared_provider_.Get() = new I();
138 return static_cast<I*>(single_shared_provider_.Get().get()); 114 return static_cast<I*>(single_shared_provider_.Get().get());
139 } 115 }
140 116
141 // The latest information filled up by QueryInfo implementation. Here we 117 // The latest information filled up by QueryInfo implementation. Here we
142 // assume the T is disallowed to copy constructor, aligns with the structure 118 // assume the T is disallowed to copy constructor, aligns with the structure
143 // type generated by IDL parser. 119 // type generated by IDL parser.
144 T info_; 120 T info_;
Greg Billock 2013/07/08 18:05:06 This change is most of the way to de-templating th
Haojian Wu 2013/07/09 13:45:04 Yes, I think so. I will separate a new CL to do it
145 121
146 private: 122 private:
147 // TODO(Haojian): Use PostBlockingPoolTaskAndReply to avoid unnecessary 123 // Called on UI thread. The |success| parameter means whether it succeeds
148 // trampolines trip. 124 // to get the information.
149 void QueryOnWorkerPool() { 125 void OnQueryCompleted(bool success) {
150 bool success = QueryInfo(&info_); 126 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
151 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, 127
152 base::Bind(&SystemInfoProvider<T>::OnQueryCompleted, this, success)); 128 while (!callbacks_.empty()) {
129 QueryInfoCompletionCallback callback = callbacks_.front();
130 callback.Run(success);
131 callbacks_.pop();
132 }
133
134 is_waiting_for_completion_ = false;
Jeffrey Yasskin 2013/07/08 19:09:49 This is a bit subtle. Could you add a comment to S
Haojian Wu 2013/07/09 13:45:04 Done.
153 } 135 }
154 136
155 // The single shared provider instance. We create it only when needed. 137 // The single shared provider instance. We create it only when needed.
156 static typename base::LazyInstance< 138 static typename base::LazyInstance<
Jeffrey Yasskin 2013/07/08 19:09:49 You can't use static members of templates because
Haojian Wu 2013/07/09 13:45:04 Done.
157 scoped_refptr<SystemInfoProvider<T> > > single_shared_provider_; 139 scoped_refptr<SystemInfoProvider<T> > > single_shared_provider_;
158 140
159 // The queue of callbacks waiting for the info querying completion. It is 141 // The queue of callbacks waiting for the info querying completion. It is
160 // maintained on the UI thread. 142 // maintained on the UI thread.
161 CallbackQueue callbacks_; 143 CallbackQueue callbacks_;
162 144
163 // Indicates if it is waiting for the querying completion. 145 // Indicates if it is waiting for the querying completion.
164 bool is_waiting_for_completion_; 146 bool is_waiting_for_completion_;
165 147
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>); 148 DISALLOW_COPY_AND_ASSIGN(SystemInfoProvider<T>);
171 }; 149 };
172 150
173 // Static member intialization. 151 // Static member intialization.
174 template<class T> 152 template<class T>
175 typename base::LazyInstance<scoped_refptr<SystemInfoProvider<T> > > 153 typename base::LazyInstance<scoped_refptr<SystemInfoProvider<T> > >
176 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER; 154 SystemInfoProvider<T>::single_shared_provider_ = LAZY_INSTANCE_INITIALIZER;
Greg Billock 2013/07/08 18:05:06 This name makes it sound like there's only one pro
Haojian Wu 2013/07/09 13:45:04 Done.
177 155
178 } // namespace extensions 156 } // namespace extensions
179 157
180 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_ 158 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_SYSTEM_INFO_PROVIDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698