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.
5 #ifndef CHROME_FRAME_NS_ISUPPORTS_IMPL_H_
6 #define CHROME_FRAME_NS_ISUPPORTS_IMPL_H_
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"
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
{
21 NsISupportsImplBase() : nsiimpl_ref_count_(0) {
22 nsiimpl_thread_id_
= PlatformThread::CurrentId();
25 virtual ~NsISupportsImplBase() {
26 DCHECK(nsiimpl_thread_id_
== PlatformThread::CurrentId());
29 NS_IMETHOD
QueryInterface(REFNSIID iid
, void** ptr
) {
30 DCHECK(nsiimpl_thread_id_
== PlatformThread::CurrentId());
31 nsresult res
= NS_NOINTERFACE
;
33 if (memcmp(&iid
, &__uuidof(nsISupports
), sizeof(nsIID
)) == 0) {
34 *ptr
= static_cast<nsISupports
*>(static_cast<typename Derived
*>(this));
42 NS_IMETHOD_(nsrefcnt
) AddRef() {
43 DCHECK(nsiimpl_thread_id_
== PlatformThread::CurrentId());
45 return nsiimpl_ref_count_
;
48 NS_IMETHOD_(nsrefcnt
) Release() {
49 DCHECK(nsiimpl_thread_id_
== PlatformThread::CurrentId());
52 if (!nsiimpl_ref_count_
) {
53 Derived
* me
= static_cast<Derived
*>(this);
58 return nsiimpl_ref_count_
;
62 nsrefcnt nsiimpl_ref_count_
;
63 AddRefModule nsiimpl_module_ref_
;
64 // used to DCHECK on expected single-threaded usage
65 uint64 nsiimpl_thread_id_
;
68 #endif // CHROME_FRAME_NS_ISUPPORTS_IMPL_H_