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 #include "content/browser/shared_worker/worker_document_set.h"
6
7 #include "base/logging.h"
8
9 namespace content {
10
WorkerDocumentSet()11 WorkerDocumentSet::WorkerDocumentSet() {
12 }
13
Add(BrowserMessageFilter * parent,unsigned long long document_id,int render_process_id,int render_frame_id)14 void WorkerDocumentSet::Add(BrowserMessageFilter* parent,
15 unsigned long long document_id,
16 int render_process_id,
17 int render_frame_id) {
18 DocumentInfo info(parent, document_id, render_process_id, render_frame_id);
19 document_set_.insert(info);
20 }
21
Contains(BrowserMessageFilter * parent,unsigned long long document_id) const22 bool WorkerDocumentSet::Contains(BrowserMessageFilter* parent,
23 unsigned long long document_id) const {
24 for (DocumentInfoSet::const_iterator i = document_set_.begin();
25 i != document_set_.end(); ++i) {
26 if (i->filter() == parent && i->document_id() == document_id)
27 return true;
28 }
29 return false;
30 }
31
ContainsExternalRenderer(int worker_process_id) const32 bool WorkerDocumentSet::ContainsExternalRenderer(
33 int worker_process_id) const {
34 for (DocumentInfoSet::const_iterator i = document_set_.begin();
35 i != document_set_.end(); ++i) {
36 if (i->render_process_id() != worker_process_id)
37 return true;
38 }
39 return false;
40 }
41
Remove(BrowserMessageFilter * parent,unsigned long long document_id)42 void WorkerDocumentSet::Remove(BrowserMessageFilter* parent,
43 unsigned long long document_id) {
44 for (DocumentInfoSet::iterator i = document_set_.begin();
45 i != document_set_.end(); i++) {
46 if (i->filter() == parent && i->document_id() == document_id) {
47 document_set_.erase(i);
48 break;
49 }
50 }
51 // Should not be duplicate copies in the document set.
52 DCHECK(!Contains(parent, document_id));
53 }
54
RemoveAll(BrowserMessageFilter * parent)55 void WorkerDocumentSet::RemoveAll(BrowserMessageFilter* parent) {
56 for (DocumentInfoSet::iterator i = document_set_.begin();
57 i != document_set_.end();) {
58
59 // Note this idiom is somewhat tricky - calling document_set_.erase(iter)
60 // invalidates any iterators that point to the element being removed, so
61 // bump the iterator beyond the item being removed before calling erase.
62 if (i->filter() == parent) {
63 DocumentInfoSet::iterator item_to_delete = i++;
64 document_set_.erase(item_to_delete);
65 } else {
66 ++i;
67 }
68 }
69 }
70
DocumentInfo(BrowserMessageFilter * filter,unsigned long long document_id,int render_process_id,int render_frame_id)71 WorkerDocumentSet::DocumentInfo::DocumentInfo(
72 BrowserMessageFilter* filter, unsigned long long document_id,
73 int render_process_id, int render_frame_id)
74 : filter_(filter),
75 document_id_(document_id),
76 render_process_id_(render_process_id),
77 render_frame_id_(render_frame_id) {
78 }
79
~WorkerDocumentSet()80 WorkerDocumentSet::~WorkerDocumentSet() {
81 }
82
83 } // namespace content
84