Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 "chrome/browser/pepper_permission_util.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 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 "chrome/browser/extensions/extension_service.h" | |
| 14 #include "chrome/browser/extensions/extension_system.h" | |
| 15 #include "chrome/browser/google/google_util.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/common/extensions/extension.h" | |
| 18 #include "chrome/common/extensions/extension_set.h" | |
| 19 #include "chrome/common/extensions/manifest_handlers/shared_module_info.h" | |
| 20 #include "extensions/common/constants.h" | |
| 21 | |
| 22 using extensions::Extension; | |
| 23 using extensions::Manifest; | |
| 24 | |
| 25 namespace chrome { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 std::string HashHost(const std::string& host) { | |
| 30 const std::string id_hash = base::SHA1HashString(host); | |
| 31 DCHECK(id_hash.length() == base::kSHA1Length); | |
|
sky
2013/05/23 21:14:07
DCHECK_EQ
victorhsieh
2013/05/23 21:39:45
Done.
| |
| 32 return base::HexEncode(id_hash.c_str(), id_hash.length()); | |
| 33 } | |
| 34 | |
| 35 bool HostIsInSet(const std::string& host, const std::set<std::string>& set) { | |
| 36 return set.count(host) > 0 || set.count(HashHost(host)) > 0; | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 bool IsExtensionOrSharedModuleWhitelisted( | |
| 42 Profile* profile, | |
| 43 const GURL& url, | |
| 44 const std::set<std::string>& whitelist, | |
| 45 const char* command_line_switch) { | |
| 46 if (!url.is_valid()) | |
| 47 return false; | |
| 48 | |
| 49 std::string host = url.host(); | |
|
sky
2013/05/23 21:14:07
nit: const (since you seem to be using const in th
victorhsieh
2013/05/23 21:39:45
Done.
| |
| 50 if (url.SchemeIs(extensions::kExtensionScheme) && | |
| 51 HostIsInSet(host, whitelist)) { | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 const Extension* extension = NULL; | |
| 56 ExtensionService* extension_service = !profile ? NULL : | |
| 57 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
| 58 if (extension_service) { | |
| 59 extension = extension_service->extensions()-> | |
| 60 GetExtensionOrAppByURL(ExtensionURLInfo(url)); | |
| 61 } | |
| 62 | |
| 63 // Check the modules that are imported by this extension to see if any of them | |
| 64 // is whitelisted. | |
| 65 if (extension) { | |
| 66 const std::vector<extensions::SharedModuleInfo::ImportInfo>& imports = | |
| 67 extensions::SharedModuleInfo::GetImports(extension); | |
| 68 std::vector<extensions::SharedModuleInfo::ImportInfo>::const_iterator it; | |
|
sky
2013/05/23 21:14:07
nit: move into for loopl
victorhsieh
2013/05/23 21:39:45
Too long. I wish I can use auto :/
| |
| 69 for (it = imports.begin(); it != imports.end(); ++it) { | |
| 70 const Extension* imported_extension = extension_service-> | |
| 71 GetExtensionById(it->extension_id, false); | |
| 72 if (imported_extension && | |
| 73 extensions::SharedModuleInfo::IsSharedModule(imported_extension) && | |
| 74 HostIsInSet(it->extension_id, whitelist)) { | |
| 75 return true; | |
| 76 } | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 81 std::string allowed_list = | |
|
sky
2013/05/23 21:14:07
nit: const
victorhsieh
2013/05/23 21:39:45
Done.
| |
| 82 command_line.GetSwitchValueASCII(command_line_switch); | |
| 83 if (allowed_list == "*") { | |
| 84 // The wildcard allows socket API only for packaged and platform apps. | |
| 85 return extension && | |
| 86 (extension->GetType() == Manifest::TYPE_LEGACY_PACKAGED_APP || | |
| 87 extension->GetType() == Manifest::TYPE_PLATFORM_APP); | |
| 88 } else if (!allowed_list.empty()) { | |
|
sky
2013/05/23 21:14:07
nit: no else (see style guide).
victorhsieh
2013/05/23 21:39:45
Done.
| |
| 89 base::StringTokenizer t(allowed_list, ","); | |
| 90 while (t.GetNext()) { | |
| 91 if (t.token() == host) | |
| 92 return true; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 return false; | |
| 97 } | |
| 98 | |
| 99 } // namespace chrome | |
| OLD | NEW |