1// Copyright (c) 2012 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 CONTENT_PUBLIC_BROWSER_BROWSER_CHILD_PROCESS_HOST_ITERATOR_H_
6#define CONTENT_PUBLIC_BROWSER_BROWSER_CHILD_PROCESS_HOST_ITERATOR_H_
7
8#include <list>
9
10#include "content/common/content_export.h"
11
12namespace IPC {
13class Message;
14}
15
16namespace content {
17class BrowserChildProcessHostDelegate;
18class BrowserChildProcessHostImpl;
19struct ChildProcessData;
20
21// This class allows iteration through either all child processes, or ones of a
22// specific type, depending on which constructor is used.  Note that this should
23// be done from the IO thread and that the iterator should not be kept around as
24// it may be invalidated on subsequent event processing in the event loop.
25class CONTENT_EXPORT BrowserChildProcessHostIterator {
26 public:
27  BrowserChildProcessHostIterator();
28  explicit BrowserChildProcessHostIterator(int type);
29
30  // These methods work on the current iterator object. Only call them if
31  // Done() returns false.
32  bool operator++();
33  bool Done();
34  const ChildProcessData& GetData();
35  bool Send(IPC::Message* message);
36  BrowserChildProcessHostDelegate* GetDelegate();
37
38 private:
39  bool all_;
40  int process_type_;
41  std::list<BrowserChildProcessHostImpl*>::iterator iterator_;
42};
43
44// Helper class so that subclasses of BrowserChildProcessHostDelegate can be
45// iterated with no casting needing. Note that because of the components build,
46// this class can only be used by BCPHD implementations that live in content,
47// otherwise link errors will result.
48template <class T>
49class CONTENT_EXPORT BrowserChildProcessHostTypeIterator
50    : public BrowserChildProcessHostIterator {
51 public:
52  explicit BrowserChildProcessHostTypeIterator(int process_type)
53      : BrowserChildProcessHostIterator(process_type) {}
54  T* operator->() {
55    return static_cast<T*>(GetDelegate());
56  }
57  T* operator*() {
58    return static_cast<T*>(GetDelegate());
59  }
60};
61
62};  // namespace content
63
64#endif  // CONTENT_PUBLIC_BROWSER_BROWSER_CHILD_PROCESS_HOST_ITERATOR_H_
65