OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 |
| 5 #ifndef CHROME_FRAME_NS_ISUPPORTS_IMPL_H_ |
| 6 #define CHROME_FRAME_NS_ISUPPORTS_IMPL_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/platform_thread.h" |
| 11 #include "chrome_frame/utils.h" |
| 12 #include "third_party/xulrunner-sdk/win/include/xpcom/nscore.h" |
| 13 #include "third_party/xulrunner-sdk/win/include/xpcom/nsid.h" |
| 14 #include "third_party/xulrunner-sdk/win/include/xpcom/nsISupportsBase.h" |
| 15 |
| 16 // A simple single-threaded implementation of methods needed to support |
| 17 // nsISupports. Must be inherited by classes that also inherit from nsISupports. |
| 18 template<class Derived> |
| 19 class NsISupportsImplBase { |
| 20 public: |
| 21 NsISupportsImplBase() : nsiimpl_ref_count_(0) { |
| 22 nsiimpl_thread_id_ = PlatformThread::CurrentId(); |
| 23 } |
| 24 |
| 25 virtual ~NsISupportsImplBase() { |
| 26 DCHECK(nsiimpl_thread_id_ == PlatformThread::CurrentId()); |
| 27 } |
| 28 |
| 29 NS_IMETHOD QueryInterface(REFNSIID iid, void** ptr) { |
| 30 DCHECK(nsiimpl_thread_id_ == PlatformThread::CurrentId()); |
| 31 nsresult res = NS_NOINTERFACE; |
| 32 |
| 33 if (memcmp(&iid, &__uuidof(nsISupports), sizeof(nsIID)) == 0) { |
| 34 *ptr = static_cast<nsISupports*>(static_cast<typename Derived*>(this)); |
| 35 AddRef(); |
| 36 res = NS_OK; |
| 37 } |
| 38 |
| 39 return res; |
| 40 } |
| 41 |
| 42 NS_IMETHOD_(nsrefcnt) AddRef() { |
| 43 DCHECK(nsiimpl_thread_id_ == PlatformThread::CurrentId()); |
| 44 nsiimpl_ref_count_++; |
| 45 return nsiimpl_ref_count_; |
| 46 } |
| 47 |
| 48 NS_IMETHOD_(nsrefcnt) Release() { |
| 49 DCHECK(nsiimpl_thread_id_ == PlatformThread::CurrentId()); |
| 50 nsiimpl_ref_count_--; |
| 51 |
| 52 if (!nsiimpl_ref_count_) { |
| 53 Derived* me = static_cast<Derived*>(this); |
| 54 delete me; |
| 55 return 0; |
| 56 } |
| 57 |
| 58 return nsiimpl_ref_count_; |
| 59 } |
| 60 |
| 61 protected: |
| 62 nsrefcnt nsiimpl_ref_count_; |
| 63 AddRefModule nsiimpl_module_ref_; |
| 64 // used to DCHECK on expected single-threaded usage |
| 65 uint64 nsiimpl_thread_id_; |
| 66 }; |
| 67 |
| 68 #endif // CHROME_FRAME_NS_ISUPPORTS_IMPL_H_ |
OLD | NEW |