summaryrefslogtreecommitdiffstats
path: root/chrome_frame/ns_isupports_impl.h
blob: 4873163ab5516ed8a56ff9fa449a62bf36944d82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_FRAME_NS_ISUPPORTS_IMPL_H_
#define CHROME_FRAME_NS_ISUPPORTS_IMPL_H_

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/platform_thread.h"
#include "chrome_frame/utils.h"
#include "third_party/xulrunner-sdk/win/include/xpcom/nscore.h"
#include "third_party/xulrunner-sdk/win/include/xpcom/nsid.h"
#include "third_party/xulrunner-sdk/win/include/xpcom/nsISupportsBase.h"

// A simple single-threaded implementation of methods needed to support
// nsISupports. Must be inherited by classes that also inherit from nsISupports.
template<class Derived>
class NsISupportsImplBase {
 public:
  NsISupportsImplBase() : nsiimpl_ref_count_(0) {
    nsiimpl_thread_id_ = PlatformThread::CurrentId();
  }

  virtual ~NsISupportsImplBase() {
    DCHECK(nsiimpl_thread_id_ == PlatformThread::CurrentId());
  }

  NS_IMETHOD QueryInterface(REFNSIID iid, void** ptr) {
    DCHECK(nsiimpl_thread_id_ == PlatformThread::CurrentId());
    nsresult res = NS_NOINTERFACE;

    if (memcmp(&iid, &__uuidof(nsISupports), sizeof(nsIID)) == 0) {
      *ptr = static_cast<nsISupports*>(static_cast<typename Derived*>(this));
      AddRef();
      res = NS_OK;
    }

    return res;
  }

  NS_IMETHOD_(nsrefcnt) AddRef() {
    DCHECK(nsiimpl_thread_id_ == PlatformThread::CurrentId());
    nsiimpl_ref_count_++;
    return nsiimpl_ref_count_;
  }

  NS_IMETHOD_(nsrefcnt) Release() {
    DCHECK(nsiimpl_thread_id_ == PlatformThread::CurrentId());
    nsiimpl_ref_count_--;

    if (!nsiimpl_ref_count_) {
      Derived* me = static_cast<Derived*>(this);
      delete me;
      return 0;
    }

    return nsiimpl_ref_count_;
  }

 protected:
  nsrefcnt nsiimpl_ref_count_;
  AddRefModule nsiimpl_module_ref_;
  // used to DCHECK on expected single-threaded usage
  uint64 nsiimpl_thread_id_;
};

#endif  // CHROME_FRAME_NS_ISUPPORTS_IMPL_H_