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_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); | |
| 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(Profile* profile, | |
| 42 const GURL& url, | |
| 43 std::set<std::string> whitelist, | |
| 44 const char command_line_switch[]) { | |
| 45 if (!url.is_valid()) | |
| 46 return false; | |
| 47 | |
| 48 std::string host = url.host(); | |
| 49 if (url.SchemeIs(extensions::kExtensionScheme) && | |
| 50 HostIsInSet(host, whitelist)) { | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 const Extension* extension = NULL; | |
| 55 ExtensionService* extension_service = !profile ? NULL : | |
| 56 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
| 57 if (extension_service) { | |
| 58 extension = extension_service->extensions()-> | |
| 59 GetExtensionOrAppByURL(ExtensionURLInfo(url)); | |
| 60 } | |
| 61 | |
| 62 // Check the modules that are imported by this extension to see if any of them | |
| 63 // is whitelisted. | |
| 64 if (extension) { | |
| 65 const std::vector<extensions::SharedModuleInfo::ImportInfo>& imports = | |
| 66 extensions::SharedModuleInfo::GetImports(extension); | |
| 67 std::vector<extensions::SharedModuleInfo::ImportInfo>::const_iterator it; | |
| 68 for (it = imports.begin(); it != imports.end(); ++it) { | |
| 69 const Extension* imported_extension = extension_service-> | |
| 70 GetExtensionById(it->extension_id, false); | |
| 71 if (imported_extension && | |
| 72 extensions::SharedModuleInfo::IsSharedModule(imported_extension) && | |
| 73 HostIsInSet(it->extension_id, whitelist)) { | |
| 74 return true; | |
| 75 } | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 // Need to check this now and not on construction because otherwise it won't | |
|
yzshen1
2013/05/22 18:14:23
This comment seems confusing now. Please revise or
victorhsieh
2013/05/22 19:47:33
It was moved from constructor of original code. I
| |
| 80 // work with browser_tests. | |
| 81 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 82 std::string allowed_list = | |
| 83 command_line.GetSwitchValueASCII(command_line_switch); | |
| 84 if (allowed_list == "*") { | |
| 85 // The wildcard allows socket API only for packaged and platform apps. | |
| 86 return extension && | |
| 87 (extension->GetType() == Manifest::TYPE_LEGACY_PACKAGED_APP || | |
| 88 extension->GetType() == Manifest::TYPE_PLATFORM_APP); | |
| 89 } else if (!allowed_list.empty()) { | |
| 90 base::StringTokenizer t(allowed_list, ","); | |
| 91 while (t.GetNext()) { | |
| 92 if (t.token() == host) | |
| 93 return true; | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 } // namespace chrome | |
| OLD | NEW |