1// Copyright 2014 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_BROWSER_SHARED_WORKER_WORKER_DOCUMENT_SET_H_
6#define CONTENT_BROWSER_SHARED_WORKER_WORKER_DOCUMENT_SET_H_
7
8#include <set>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12
13namespace content {
14class BrowserMessageFilter;
15
16// The WorkerDocumentSet tracks all of the DOM documents associated with a
17// set of workers. With nested workers, multiple workers can share the same
18// WorkerDocumentSet (meaning that they all share the same lifetime, and will
19// all exit when the last document in that set exits, per the WebWorkers spec).
20class WorkerDocumentSet : public base::RefCounted<WorkerDocumentSet> {
21 public:
22  WorkerDocumentSet();
23
24  // The information we track for each document
25  class DocumentInfo {
26   public:
27    DocumentInfo(BrowserMessageFilter* filter, unsigned long long document_id,
28                 int renderer_process_id, int render_frame_id);
29    BrowserMessageFilter* filter() const { return filter_; }
30    unsigned long long document_id() const { return document_id_; }
31    int render_process_id() const { return render_process_id_; }
32    int render_frame_id() const { return render_frame_id_; }
33
34    // Define operator "<", which is used to determine uniqueness within
35    // the set.
36    bool operator <(const DocumentInfo& other) const {
37      // Items are identical if the sender and document_id are identical,
38      // otherwise create an arbitrary stable ordering based on the document
39      // id/filter.
40      if (filter() == other.filter()) {
41        return document_id() < other.document_id();
42      } else {
43        return reinterpret_cast<unsigned long long>(filter()) <
44            reinterpret_cast<unsigned long long>(other.filter());
45      }
46    }
47
48   private:
49    BrowserMessageFilter* filter_;
50    unsigned long long document_id_;
51    int render_process_id_;
52    int render_frame_id_;
53  };
54
55  // Adds a document to a shared worker's document set. Also includes the
56  // associated render_process_id the document is associated with, to enable
57  // communication with the parent tab for things like http auth dialogs.
58  void Add(BrowserMessageFilter* parent,
59           unsigned long long document_id,
60           int render_process_id,
61           int render_frame_id);
62
63  // Checks to see if a document is in a shared worker's document set.
64  bool Contains(BrowserMessageFilter* parent,
65                unsigned long long document_id) const;
66
67  // Checks to see if the document set contains any documents which is
68  // associated with other renderer process than worker_process_id.
69  bool ContainsExternalRenderer(int worker_process_id) const;
70
71  // Removes a specific document from a worker's document set when that document
72  // is detached.
73  void Remove(BrowserMessageFilter* parent, unsigned long long document_id);
74
75  // Invoked when a render process exits, to remove all associated documents
76  // from a worker's document set.
77  void RemoveAll(BrowserMessageFilter* parent);
78
79  bool IsEmpty() const { return document_set_.empty(); }
80
81  // Define a typedef for convenience here when declaring iterators, etc.
82  typedef std::set<DocumentInfo> DocumentInfoSet;
83
84  // Returns the set of documents associated with this worker.
85  const DocumentInfoSet& documents() { return document_set_; }
86
87 private:
88  friend class base::RefCounted<WorkerDocumentSet>;
89  virtual ~WorkerDocumentSet();
90
91  DocumentInfoSet document_set_;
92};
93
94}  // namespace content
95
96#endif  // CONTENT_BROWSER_SHARED_WORKER_WORKER_DOCUMENT_SET_H_
97