15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file defines the type used to provide sources for NotificationService
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notifications.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CONTENT_PUBLIC_BROWSER_NOTIFICATION_SOURCE_H_
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_PUBLIC_BROWSER_NOTIFICATION_SOURCE_H_
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/content_export.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Do not declare a NotificationSource directly--use either
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "Source<sourceclassname>(sourceclasspointer)" or
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NotificationService::AllSources().
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CONTENT_EXPORT NotificationSource {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   NotificationSource(const NotificationSource& other) : ptr_(other.ptr_) {}
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~NotificationSource() {}
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NotificationSource can be used as the index for a map; this method
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returns the pointer to the current source as an identifier, for use as a
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // map index.
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uintptr_t map_key() const { return reinterpret_cast<uintptr_t>(ptr_); }
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator!=(const NotificationSource& other) const {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ptr_ != other.ptr_;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator==(const NotificationSource& other) const {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ptr_ == other.ptr_;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit NotificationSource(const void* ptr) : ptr_(ptr) {}
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Declaring this const allows Source<T> to be used with both T = Foo and
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // T = const Foo.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const void* ptr_;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <class T>
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Source : public NotificationSource {
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(erg): Our code hard relies on implicit conversion
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Source(const T* ptr) : NotificationSource(ptr) {}  // NOLINT
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Source(const NotificationSource& other)      // NOLINT
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : NotificationSource(other) {}
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  T* operator->() const { return ptr(); }
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The casts here allow this to compile with both T = Foo and T = const Foo.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  T* ptr() const { return static_cast<T*>(const_cast<void*>(ptr_)); }
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CONTENT_PUBLIC_BROWSER_NOTIFICATION_SOURCE_H_
60