plugin_utils.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2016 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. #include "shell/browser/plugins/plugin_utils.h"
  5. #include <vector>
  6. #include "base/containers/contains.h"
  7. #include "base/values.h"
  8. #include "content/public/common/webplugininfo.h"
  9. #include "extensions/buildflags/buildflags.h"
  10. #include "url/gurl.h"
  11. #include "url/origin.h"
  12. #if BUILDFLAG(ENABLE_EXTENSIONS)
  13. #include "extensions/browser/extension_registry.h"
  14. #include "extensions/browser/extension_util.h"
  15. #include "extensions/common/constants.h"
  16. #include "extensions/common/extension.h"
  17. #include "extensions/common/manifest_handlers/mime_types_handler.h"
  18. #endif
  19. // static
  20. std::string PluginUtils::GetExtensionIdForMimeType(
  21. content::BrowserContext* browser_context,
  22. const std::string& mime_type) {
  23. auto map = GetMimeTypeToExtensionIdMap(browser_context);
  24. auto it = map.find(mime_type);
  25. if (it != map.end())
  26. return it->second;
  27. return std::string();
  28. }
  29. base::flat_map<std::string, std::string>
  30. PluginUtils::GetMimeTypeToExtensionIdMap(
  31. content::BrowserContext* browser_context) {
  32. base::flat_map<std::string, std::string> mime_type_to_extension_id_map;
  33. #if BUILDFLAG(ENABLE_EXTENSIONS)
  34. std::vector<std::string> allowed_extension_ids =
  35. MimeTypesHandler::GetMIMETypeAllowlist();
  36. // Go through the white-listed extensions and try to use them to intercept
  37. // the URL request.
  38. for (const std::string& extension_id : allowed_extension_ids) {
  39. const extensions::Extension* extension =
  40. extensions::ExtensionRegistry::Get(browser_context)
  41. ->enabled_extensions()
  42. .GetByID(extension_id);
  43. // The white-listed extension may not be installed, so we have to nullptr
  44. // check |extension|.
  45. if (!extension) {
  46. continue;
  47. }
  48. if (MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension)) {
  49. for (const auto& supported_mime_type : handler->mime_type_set()) {
  50. DCHECK(!base::Contains(mime_type_to_extension_id_map,
  51. supported_mime_type));
  52. mime_type_to_extension_id_map[supported_mime_type] = extension_id;
  53. }
  54. }
  55. }
  56. #endif
  57. return mime_type_to_extension_id_map;
  58. }