1 // Copyright 2013 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 "extensions/browser/process_map.h"
6
7 #include "extensions/browser/process_map_factory.h"
8
9 namespace extensions {
10
11 // Item
12 struct ProcessMap::Item {
Itemextensions::ProcessMap::Item13 Item() : process_id(0), site_instance_id(0) {
14 }
15
16 // Purposely implicit constructor needed on older gcc's. See:
17 // http://codereview.chromium.org/8769022/
Itemextensions::ProcessMap::Item18 explicit Item(const ProcessMap::Item& other)
19 : extension_id(other.extension_id),
20 process_id(other.process_id),
21 site_instance_id(other.site_instance_id) {
22 }
23
Itemextensions::ProcessMap::Item24 Item(const std::string& extension_id, int process_id,
25 int site_instance_id)
26 : extension_id(extension_id),
27 process_id(process_id),
28 site_instance_id(site_instance_id) {
29 }
30
~Itemextensions::ProcessMap::Item31 ~Item() {
32 }
33
operator <extensions::ProcessMap::Item34 bool operator<(const ProcessMap::Item& other) const {
35 if (extension_id < other.extension_id)
36 return true;
37
38 if (extension_id == other.extension_id &&
39 process_id < other.process_id) {
40 return true;
41 }
42
43 if (extension_id == other.extension_id &&
44 process_id == other.process_id &&
45 site_instance_id < other.site_instance_id) {
46 return true;
47 }
48
49 return false;
50 }
51
52 std::string extension_id;
53 int process_id;
54 int site_instance_id;
55 };
56
57
58 // ProcessMap
ProcessMap()59 ProcessMap::ProcessMap() {
60 }
61
~ProcessMap()62 ProcessMap::~ProcessMap() {
63 }
64
65 // static
Get(content::BrowserContext * browser_context)66 ProcessMap* ProcessMap::Get(content::BrowserContext* browser_context) {
67 return ProcessMapFactory::GetForBrowserContext(browser_context);
68 }
69
Insert(const std::string & extension_id,int process_id,int site_instance_id)70 bool ProcessMap::Insert(const std::string& extension_id, int process_id,
71 int site_instance_id) {
72 return items_.insert(Item(extension_id, process_id, site_instance_id)).second;
73 }
74
Remove(const std::string & extension_id,int process_id,int site_instance_id)75 bool ProcessMap::Remove(const std::string& extension_id, int process_id,
76 int site_instance_id) {
77 return items_.erase(Item(extension_id, process_id, site_instance_id)) > 0;
78 }
79
RemoveAllFromProcess(int process_id)80 int ProcessMap::RemoveAllFromProcess(int process_id) {
81 int result = 0;
82 for (ItemSet::iterator iter = items_.begin(); iter != items_.end(); ) {
83 if (iter->process_id == process_id) {
84 items_.erase(iter++);
85 ++result;
86 } else {
87 ++iter;
88 }
89 }
90 return result;
91 }
92
Contains(const std::string & extension_id,int process_id) const93 bool ProcessMap::Contains(const std::string& extension_id,
94 int process_id) const {
95 for (ItemSet::const_iterator iter = items_.begin(); iter != items_.end();
96 ++iter) {
97 if (iter->process_id == process_id && iter->extension_id == extension_id)
98 return true;
99 }
100 return false;
101 }
102
Contains(int process_id) const103 bool ProcessMap::Contains(int process_id) const {
104 for (ItemSet::const_iterator iter = items_.begin(); iter != items_.end();
105 ++iter) {
106 if (iter->process_id == process_id)
107 return true;
108 }
109 return false;
110 }
111
GetExtensionsInProcess(int process_id) const112 std::set<std::string> ProcessMap::GetExtensionsInProcess(int process_id) const {
113 std::set<std::string> result;
114 for (ItemSet::const_iterator iter = items_.begin(); iter != items_.end();
115 ++iter) {
116 if (iter->process_id == process_id)
117 result.insert(iter->extension_id);
118 }
119 return result;
120 }
121
122 } // namespace extensions
123