| OLD | NEW |
| 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 // CancelableRequestProviders and Consumers work together to make requests that | 5 // CancelableRequestProviders and Consumers work together to make requests that |
| 6 // execute on a background thread in the provider and return data to the | 6 // execute on a background thread in the provider and return data to the |
| 7 // consumer. These class collaborate to keep a list of open requests and to | 7 // consumer. These class collaborate to keep a list of open requests and to |
| 8 // make sure that requests to not outlive either of the objects involved in the | 8 // make sure that requests to not outlive either of the objects involved in the |
| 9 // transaction. | 9 // transaction. |
| 10 // | 10 // |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 #include "base/basictypes.h" | 93 #include "base/basictypes.h" |
| 94 #include "base/callback.h" | 94 #include "base/callback.h" |
| 95 #include "base/logging.h" | 95 #include "base/logging.h" |
| 96 #include "base/memory/ref_counted.h" | 96 #include "base/memory/ref_counted.h" |
| 97 #include "base/memory/scoped_ptr.h" | 97 #include "base/memory/scoped_ptr.h" |
| 98 #include "base/message_loop.h" | 98 #include "base/message_loop.h" |
| 99 #include "base/synchronization/cancellation_flag.h" | 99 #include "base/synchronization/cancellation_flag.h" |
| 100 #include "base/synchronization/lock.h" | 100 #include "base/synchronization/lock.h" |
| 101 #include "base/task.h" | 101 #include "base/task.h" |
| 102 #include "build/build_config.h" | 102 #include "build/build_config.h" |
| 103 #include "content/common/content_export.h" |
| 103 | 104 |
| 104 class CancelableRequestBase; | 105 class CancelableRequestBase; |
| 105 class CancelableRequestConsumerBase; | 106 class CancelableRequestConsumerBase; |
| 106 | 107 |
| 107 // CancelableRequestProvider -------------------------------------------------- | 108 // CancelableRequestProvider -------------------------------------------------- |
| 108 // | 109 // |
| 109 // This class is threadsafe. Requests may be added or canceled from any thread, | 110 // This class is threadsafe. Requests may be added or canceled from any thread, |
| 110 // but a task must only be canceled from the same thread it was initially run | 111 // but a task must only be canceled from the same thread it was initially run |
| 111 // on. | 112 // on. |
| 112 // | 113 // |
| 113 // It is intended that providers inherit from this class to provide the | 114 // It is intended that providers inherit from this class to provide the |
| 114 // necessary functionality. | 115 // necessary functionality. |
| 115 | 116 |
| 116 class CancelableRequestProvider { | 117 class CONTENT_EXPORT CancelableRequestProvider { |
| 117 public: | 118 public: |
| 118 // Identifies a specific request from this provider. | 119 // Identifies a specific request from this provider. |
| 119 typedef int Handle; | 120 typedef int Handle; |
| 120 | 121 |
| 121 CancelableRequestProvider(); | 122 CancelableRequestProvider(); |
| 122 virtual ~CancelableRequestProvider(); | 123 virtual ~CancelableRequestProvider(); |
| 123 | 124 |
| 124 // Called by the enduser of the request to cancel it. This MUST be called on | 125 // Called by the enduser of the request to cancel it. This MUST be called on |
| 125 // the same thread that originally issued the request (which is also the same | 126 // the same thread that originally issued the request (which is also the same |
| 126 // thread that would have received the callback if it was not canceled). | 127 // thread that would have received the callback if it was not canceled). |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 // | 508 // |
| 508 // Callback parameters are passed by value. In some cases, the request will | 509 // Callback parameters are passed by value. In some cases, the request will |
| 509 // want to return a large amount of data (for example, an image). One good | 510 // want to return a large amount of data (for example, an image). One good |
| 510 // approach is to derive from the CancelableRequest and make the data object | 511 // approach is to derive from the CancelableRequest and make the data object |
| 511 // (for example, a std::vector) owned by the CancelableRequest. The pointer | 512 // (for example, a std::vector) owned by the CancelableRequest. The pointer |
| 512 // to this data would be passed for the callback parameter. Since the | 513 // to this data would be passed for the callback parameter. Since the |
| 513 // CancelableRequest outlives the callback call, the data will be valid on the | 514 // CancelableRequest outlives the callback call, the data will be valid on the |
| 514 // other thread for the callback, but will still be destroyed properly. | 515 // other thread for the callback, but will still be destroyed properly. |
| 515 | 516 |
| 516 // Non-templatized base class that provides cancellation | 517 // Non-templatized base class that provides cancellation |
| 517 class CancelableRequestBase | 518 class CONTENT_EXPORT CancelableRequestBase |
| 518 : public base::RefCountedThreadSafe<CancelableRequestBase> { | 519 : public base::RefCountedThreadSafe<CancelableRequestBase> { |
| 519 public: | 520 public: |
| 520 friend class CancelableRequestProvider; | 521 friend class CancelableRequestProvider; |
| 521 | 522 |
| 522 // Initializes most things to empty, Init() must be called to complete | 523 // Initializes most things to empty, Init() must be called to complete |
| 523 // initialization of the object. This will be done by the provider when | 524 // initialization of the object. This will be done by the provider when |
| 524 // the request is dispatched. | 525 // the request is dispatched. |
| 525 // | 526 // |
| 526 // This must be called on the same thread the callback will be executed on, | 527 // This must be called on the same thread the callback will be executed on, |
| 527 // it will save that thread for later. | 528 // it will save that thread for later. |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 723 } | 724 } |
| 724 | 725 |
| 725 // The value. | 726 // The value. |
| 726 Type value; | 727 Type value; |
| 727 | 728 |
| 728 protected: | 729 protected: |
| 729 virtual ~CancelableRequest1() {} | 730 virtual ~CancelableRequest1() {} |
| 730 }; | 731 }; |
| 731 | 732 |
| 732 #endif // CONTENT_BROWSER_CANCELABLE_REQUEST_H_ | 733 #endif // CONTENT_BROWSER_CANCELABLE_REQUEST_H_ |
| OLD | NEW |