- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / extension_set.cc
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 #include "chrome/common/extensions/extension_set.h"
6
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/stl_util.h"
10 #include "chrome/common/extensions/extension.h"
11 #include "chrome/common/extensions/manifest_handlers/sandboxed_page_info.h"
12 #include "chrome/common/url_constants.h"
13 #include "extensions/common/constants.h"
14
15 using extensions::Extension;
16
17 ExtensionSet::const_iterator::const_iterator() {}
18
19 ExtensionSet::const_iterator::const_iterator(const const_iterator& other)
20     : it_(other.it_) {
21 }
22
23 ExtensionSet::const_iterator::const_iterator(ExtensionMap::const_iterator it)
24     : it_(it) {
25 }
26
27 ExtensionSet::ExtensionSet() {
28 }
29
30 ExtensionSet::~ExtensionSet() {
31 }
32
33 size_t ExtensionSet::size() const {
34   return extensions_.size();
35 }
36
37 bool ExtensionSet::is_empty() const {
38   return extensions_.empty();
39 }
40
41 bool ExtensionSet::Contains(const std::string& extension_id) const {
42   return extensions_.find(extension_id) != extensions_.end();
43 }
44
45 bool ExtensionSet::Insert(const scoped_refptr<const Extension>& extension) {
46   bool was_present = ContainsKey(extensions_, extension->id());
47   extensions_[extension->id()] = extension;
48   if (!was_present && !modification_callback_.is_null())
49     modification_callback_.Run(GetIDs());
50   return !was_present;
51 }
52
53 bool ExtensionSet::InsertAll(const ExtensionSet& extensions) {
54   size_t before = size();
55   for (ExtensionSet::const_iterator iter = extensions.begin();
56        iter != extensions.end(); ++iter) {
57     Insert(*iter);
58   }
59   return size() != before;
60 }
61
62 bool ExtensionSet::Remove(const std::string& id) {
63   bool was_present = extensions_.erase(id) > 0;
64   if (was_present && !modification_callback_.is_null())
65     modification_callback_.Run(GetIDs());
66   return was_present;
67 }
68
69 void ExtensionSet::Clear() {
70   extensions_.clear();
71 }
72
73 std::string ExtensionSet::GetExtensionOrAppIDByURL(const GURL& url) const {
74   if (url.SchemeIs(extensions::kExtensionScheme))
75     return url.host();
76
77   const Extension* extension = GetExtensionOrAppByURL(url);
78   if (!extension)
79     return std::string();
80
81   return extension->id();
82 }
83
84 const Extension* ExtensionSet::GetExtensionOrAppByURL(const GURL& url) const {
85   if (url.SchemeIs(extensions::kExtensionScheme))
86     return GetByID(url.host());
87
88   return GetHostedAppByURL(url);
89 }
90
91 const Extension* ExtensionSet::GetHostedAppByURL(const GURL& url) const {
92   for (ExtensionMap::const_iterator iter = extensions_.begin();
93        iter != extensions_.end(); ++iter) {
94     if (iter->second->web_extent().MatchesURL(url))
95       return iter->second.get();
96   }
97
98   return NULL;
99 }
100
101 const Extension* ExtensionSet::GetHostedAppByOverlappingWebExtent(
102     const extensions::URLPatternSet& extent) const {
103   for (ExtensionMap::const_iterator iter = extensions_.begin();
104        iter != extensions_.end(); ++iter) {
105     if (iter->second->web_extent().OverlapsWith(extent))
106       return iter->second.get();
107   }
108
109   return NULL;
110 }
111
112 bool ExtensionSet::InSameExtent(const GURL& old_url,
113                                 const GURL& new_url) const {
114   return GetExtensionOrAppByURL(old_url) ==
115       GetExtensionOrAppByURL(new_url);
116 }
117
118 const Extension* ExtensionSet::GetByID(const std::string& id) const {
119   ExtensionMap::const_iterator i = extensions_.find(id);
120   if (i != extensions_.end())
121     return i->second.get();
122   else
123     return NULL;
124 }
125
126 extensions::ExtensionIdSet ExtensionSet::GetIDs() const {
127   extensions::ExtensionIdSet ids;
128   for (ExtensionMap::const_iterator it = extensions_.begin();
129        it != extensions_.end(); ++it) {
130     ids.insert(it->first);
131   }
132   return ids;
133 }
134
135 bool ExtensionSet::ExtensionBindingsAllowed(const GURL& url) const {
136   if (url.SchemeIs(extensions::kExtensionScheme))
137     return true;
138
139   ExtensionMap::const_iterator i = extensions_.begin();
140   for (; i != extensions_.end(); ++i) {
141     if (i->second->location() == extensions::Manifest::COMPONENT &&
142         i->second->web_extent().MatchesURL(url))
143       return true;
144   }
145
146   return false;
147 }