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 "chrome/common/pepper_permission_util.h"
9 #include "base/command_line.h"
10 #include "base/sha1.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_tokenizer.h"
13 #include "extensions/common/constants.h"
14 #include "extensions/common/extension.h"
15 #include "extensions/common/extension_set.h"
16 #include "extensions/common/manifest_handlers/shared_module_info.h"
18 using extensions::Extension
;
19 using extensions::Manifest
;
25 std::string
HashHost(const std::string
& host
) {
26 const std::string id_hash
= base::SHA1HashString(host
);
27 DCHECK_EQ(id_hash
.length(), base::kSHA1Length
);
28 return base::HexEncode(id_hash
.c_str(), id_hash
.length());
31 bool HostIsInSet(const std::string
& host
, const std::set
<std::string
>& set
) {
32 return set
.count(host
) > 0 || set
.count(HashHost(host
)) > 0;
37 bool IsExtensionOrSharedModuleWhitelisted(
39 const extensions::ExtensionSet
* extension_set
,
40 const std::set
<std::string
>& whitelist
) {
41 if (!url
.is_valid() || !url
.SchemeIs(extensions::kExtensionScheme
))
44 const std::string host
= url
.host();
45 if (HostIsInSet(host
, whitelist
))
48 // Check the modules that are imported by this extension to see if any of them
50 const Extension
* extension
= extension_set
? extension_set
->GetByID(host
)
53 typedef std::vector
<extensions::SharedModuleInfo::ImportInfo
>
55 const ImportInfoVector
& imports
=
56 extensions::SharedModuleInfo::GetImports(extension
);
57 for (ImportInfoVector::const_iterator it
= imports
.begin();
58 it
!= imports
.end(); ++it
) {
59 const Extension
* imported_extension
= extension_set
->GetByID(
61 if (imported_extension
&&
62 extensions::SharedModuleInfo::IsSharedModule(imported_extension
) &&
63 HostIsInSet(it
->extension_id
, whitelist
)) {
72 bool IsHostAllowedByCommandLine(const GURL
& url
,
73 const extensions::ExtensionSet
* extension_set
,
74 const char* command_line_switch
) {
78 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
79 const std::string allowed_list
=
80 command_line
.GetSwitchValueASCII(command_line_switch
);
81 if (allowed_list
.empty())
84 const std::string host
= url
.host();
85 if (allowed_list
== "*") {
86 // For now, we only allow packaged and platform apps in this wildcard.
87 if (!extension_set
|| !url
.SchemeIs(extensions::kExtensionScheme
))
90 const Extension
* extension
= extension_set
->GetByID(host
);
92 (extension
->GetType() == Manifest::TYPE_LEGACY_PACKAGED_APP
||
93 extension
->GetType() == Manifest::TYPE_PLATFORM_APP
);
96 base::StringTokenizer
t(allowed_list
, ",");
98 if (t
.token() == host
)
105 } // namespace chrome