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.
5 #include "extensions/browser/process_map.h"
7 #include "content/public/browser/child_process_security_policy.h"
8 #include "extensions/browser/extension_registry.h"
9 #include "extensions/browser/process_map_factory.h"
10 #include "extensions/common/extension.h"
11 #include "extensions/common/features/feature.h"
13 namespace extensions
{
16 struct ProcessMap::Item
{
17 Item() : process_id(0), site_instance_id(0) {
20 // Purposely implicit constructor needed on older gcc's. See:
21 // http://codereview.chromium.org/8769022/
22 explicit Item(const ProcessMap::Item
& other
)
23 : extension_id(other
.extension_id
),
24 process_id(other
.process_id
),
25 site_instance_id(other
.site_instance_id
) {
28 Item(const std::string
& extension_id
, int process_id
,
30 : extension_id(extension_id
),
31 process_id(process_id
),
32 site_instance_id(site_instance_id
) {
38 bool operator<(const ProcessMap::Item
& other
) const {
39 if (extension_id
< other
.extension_id
)
42 if (extension_id
== other
.extension_id
&&
43 process_id
< other
.process_id
) {
47 if (extension_id
== other
.extension_id
&&
48 process_id
== other
.process_id
&&
49 site_instance_id
< other
.site_instance_id
) {
56 std::string extension_id
;
63 ProcessMap::ProcessMap() {
66 ProcessMap::~ProcessMap() {
70 ProcessMap
* ProcessMap::Get(content::BrowserContext
* browser_context
) {
71 return ProcessMapFactory::GetForBrowserContext(browser_context
);
74 bool ProcessMap::Insert(const std::string
& extension_id
, int process_id
,
75 int site_instance_id
) {
76 return items_
.insert(Item(extension_id
, process_id
, site_instance_id
)).second
;
79 bool ProcessMap::Remove(const std::string
& extension_id
, int process_id
,
80 int site_instance_id
) {
81 return items_
.erase(Item(extension_id
, process_id
, site_instance_id
)) > 0;
84 int ProcessMap::RemoveAllFromProcess(int process_id
) {
86 for (ItemSet::iterator iter
= items_
.begin(); iter
!= items_
.end(); ) {
87 if (iter
->process_id
== process_id
) {
97 bool ProcessMap::Contains(const std::string
& extension_id
,
98 int process_id
) const {
99 for (ItemSet::const_iterator iter
= items_
.begin(); iter
!= items_
.end();
101 if (iter
->process_id
== process_id
&& iter
->extension_id
== extension_id
)
107 bool ProcessMap::Contains(int process_id
) const {
108 for (ItemSet::const_iterator iter
= items_
.begin(); iter
!= items_
.end();
110 if (iter
->process_id
== process_id
)
116 std::set
<std::string
> ProcessMap::GetExtensionsInProcess(int process_id
) const {
117 std::set
<std::string
> result
;
118 for (ItemSet::const_iterator iter
= items_
.begin(); iter
!= items_
.end();
120 if (iter
->process_id
== process_id
)
121 result
.insert(iter
->extension_id
);
126 Feature::Context
ProcessMap::GetMostLikelyContextType(
127 const Extension
* extension
,
128 int process_id
) const {
129 // WARNING: This logic must match Dispatcher::ClassifyJavaScriptContext, as
132 if (content::ChildProcessSecurityPolicy::GetInstance()->HasWebUIBindings(
134 return Feature::WEBUI_CONTEXT
;
138 return Feature::WEB_PAGE_CONTEXT
;
141 if (!Contains(extension
->id(), process_id
)) {
142 // This could equally be UNBLESSED_EXTENSION_CONTEXT, but we don't record
143 // which processes have extension frames in them.
144 // TODO(kalman): Investigate this.
145 return Feature::CONTENT_SCRIPT_CONTEXT
;
148 if (extension
->is_hosted_app() &&
149 extension
->location() != Manifest::COMPONENT
) {
150 return Feature::BLESSED_WEB_PAGE_CONTEXT
;
153 return Feature::BLESSED_EXTENSION_CONTEXT
;
156 } // namespace extensions