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

Side by Side Diff: chrome/browser/extensions/extension_function.h

Issue 8588067: Refactor to allow same code to test both sync and async functions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix dependencies. Created 9 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_function.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 4
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_
7 #pragma once 7 #pragma once
8 8
9 #include <list> 9 #include <list>
10 #include <string> 10 #include <string>
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 // returning. The calling renderer process will be killed. 201 // returning. The calling renderer process will be killed.
202 bool bad_message_; 202 bool bad_message_;
203 203
204 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); 204 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction);
205 }; 205 };
206 206
207 // Extension functions that run on the UI thread. Most functions fall into 207 // Extension functions that run on the UI thread. Most functions fall into
208 // this category. 208 // this category.
209 class UIThreadExtensionFunction : public ExtensionFunction { 209 class UIThreadExtensionFunction : public ExtensionFunction {
210 public: 210 public:
211 // A delegate for use in testing, to intercept the call to SendResponse.
212 class DelegateForTests {
213 public:
214 virtual void OnSendResponse(UIThreadExtensionFunction* function,
215 bool success) = 0;
216 };
217
211 UIThreadExtensionFunction(); 218 UIThreadExtensionFunction();
212 219
213 virtual UIThreadExtensionFunction* AsUIThreadExtensionFunction() OVERRIDE; 220 virtual UIThreadExtensionFunction* AsUIThreadExtensionFunction() OVERRIDE;
214 221
222 void set_test_delegate(DelegateForTests* delegate) {
223 delegate_ = delegate;
224 }
215 // Set the profile which contains the extension that has originated this 225 // Set the profile which contains the extension that has originated this
216 // function call. 226 // function call.
217 void set_profile(Profile* profile) { profile_ = profile; } 227 void set_profile(Profile* profile) { profile_ = profile; }
218 Profile* profile() const { return profile_; } 228 Profile* profile() const { return profile_; }
219 229
220 void SetRenderViewHost(RenderViewHost* render_view_host); 230 void SetRenderViewHost(RenderViewHost* render_view_host);
221 RenderViewHost* render_view_host() const { return render_view_host_; } 231 RenderViewHost* render_view_host() const { return render_view_host_; }
222 232
223 void set_dispatcher( 233 void set_dispatcher(
224 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher) { 234 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 virtual void Observe(int type, 288 virtual void Observe(int type,
279 const content::NotificationSource& source, 289 const content::NotificationSource& source,
280 const content::NotificationDetails& details) OVERRIDE; 290 const content::NotificationDetails& details) OVERRIDE;
281 UIThreadExtensionFunction* function_; 291 UIThreadExtensionFunction* function_;
282 content::NotificationRegistrar registrar_; 292 content::NotificationRegistrar registrar_;
283 }; 293 };
284 294
285 virtual void Destruct() const OVERRIDE; 295 virtual void Destruct() const OVERRIDE;
286 296
287 scoped_ptr<RenderViewHostTracker> tracker_; 297 scoped_ptr<RenderViewHostTracker> tracker_;
298
299 DelegateForTests* delegate_;
288 }; 300 };
289 301
290 // Extension functions that run on the IO thread. This type of function avoids 302 // Extension functions that run on the IO thread. This type of function avoids
291 // a roundtrip to and from the UI thread (because communication with the 303 // a roundtrip to and from the UI thread (because communication with the
292 // extension process happens on the IO thread). It's intended to be used when 304 // extension process happens on the IO thread). It's intended to be used when
293 // performance is critical (e.g. the webRequest API which can block network 305 // performance is critical (e.g. the webRequest API which can block network
294 // requests). Generally, UIThreadExtensionFunction is more appropriate and will 306 // requests). Generally, UIThreadExtensionFunction is more appropriate and will
295 // be easier to use and interface with the rest of the browser. 307 // be easier to use and interface with the rest of the browser.
296 class IOThreadExtensionFunction : public ExtensionFunction { 308 class IOThreadExtensionFunction : public ExtensionFunction {
297 public: 309 public:
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender_; 345 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender_;
334 int routing_id_; 346 int routing_id_;
335 347
336 scoped_refptr<const ExtensionInfoMap> extension_info_map_; 348 scoped_refptr<const ExtensionInfoMap> extension_info_map_;
337 }; 349 };
338 350
339 // Base class for an extension function that runs asynchronously *relative to 351 // Base class for an extension function that runs asynchronously *relative to
340 // the browser's UI thread*. 352 // the browser's UI thread*.
341 class AsyncExtensionFunction : public UIThreadExtensionFunction { 353 class AsyncExtensionFunction : public UIThreadExtensionFunction {
342 public: 354 public:
343 // A delegate for use in testing, to intercept the call to SendResponse.
344 class DelegateForTests {
345 public:
346 virtual void OnSendResponse(AsyncExtensionFunction* function,
347 bool success) = 0;
348 };
349
350 AsyncExtensionFunction(); 355 AsyncExtensionFunction();
351 virtual void SendResponse(bool success) OVERRIDE;
352
353 void set_test_delegate(DelegateForTests* delegate) {
354 delegate_ = delegate;
355 }
356 356
357 protected: 357 protected:
358 virtual ~AsyncExtensionFunction(); 358 virtual ~AsyncExtensionFunction();
359
360 DelegateForTests* delegate_;
361 }; 359 };
362 360
363 // A SyncExtensionFunction is an ExtensionFunction that runs synchronously 361 // A SyncExtensionFunction is an ExtensionFunction that runs synchronously
364 // *relative to the browser's UI thread*. Note that this has nothing to do with 362 // *relative to the browser's UI thread*. Note that this has nothing to do with
365 // running synchronously relative to the extension process. From the extension 363 // running synchronously relative to the extension process. From the extension
366 // process's point of view, the function is still asynchronous. 364 // process's point of view, the function is still asynchronous.
367 // 365 //
368 // This kind of function is convenient for implementing simple APIs that just 366 // This kind of function is convenient for implementing simple APIs that just
369 // need to interact with things on the browser UI thread. 367 // need to interact with things on the browser UI thread.
370 class SyncExtensionFunction : public UIThreadExtensionFunction { 368 class SyncExtensionFunction : public UIThreadExtensionFunction {
(...skipping 10 matching lines...) Expand all
381 public: 379 public:
382 SyncIOThreadExtensionFunction(); 380 SyncIOThreadExtensionFunction();
383 381
384 virtual void Run() OVERRIDE; 382 virtual void Run() OVERRIDE;
385 383
386 protected: 384 protected:
387 virtual ~SyncIOThreadExtensionFunction(); 385 virtual ~SyncIOThreadExtensionFunction();
388 }; 386 };
389 387
390 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ 388 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_function.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698