electron_plugin_info_host_impl.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2012 The Chromium Authors
  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/electron_plugin_info_host_impl.h"
  5. #include <memory>
  6. #include <utility>
  7. #include <vector>
  8. #include "base/functional/bind.h"
  9. #include "content/public/browser/browser_thread.h"
  10. #include "content/public/browser/plugin_service.h"
  11. #include "content/public/common/content_constants.h"
  12. #include "url/gurl.h"
  13. #include "url/origin.h"
  14. using content::PluginService;
  15. using content::WebPluginInfo;
  16. namespace electron {
  17. ElectronPluginInfoHostImpl::ElectronPluginInfoHostImpl() = default;
  18. ElectronPluginInfoHostImpl::~ElectronPluginInfoHostImpl() = default;
  19. struct ElectronPluginInfoHostImpl::GetPluginInfo_Params {
  20. GURL url;
  21. url::Origin main_frame_origin;
  22. std::string mime_type;
  23. };
  24. void ElectronPluginInfoHostImpl::GetPluginInfo(const GURL& url,
  25. const url::Origin& origin,
  26. const std::string& mime_type,
  27. GetPluginInfoCallback callback) {
  28. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  29. GetPluginInfo_Params params = {url, origin, mime_type};
  30. PluginService::GetInstance()->GetPlugins(
  31. base::BindOnce(&ElectronPluginInfoHostImpl::PluginsLoaded,
  32. weak_factory_.GetWeakPtr(), params, std::move(callback)));
  33. }
  34. void ElectronPluginInfoHostImpl::PluginsLoaded(
  35. const GetPluginInfo_Params& params,
  36. GetPluginInfoCallback callback,
  37. const std::vector<WebPluginInfo>& plugins) {
  38. mojom::PluginInfoPtr output = mojom::PluginInfo::New();
  39. std::vector<WebPluginInfo> matching_plugins;
  40. std::vector<std::string> mime_types;
  41. PluginService::GetInstance()->GetPluginInfoArray(
  42. params.url, params.mime_type, true, &matching_plugins, &mime_types);
  43. if (!matching_plugins.empty()) {
  44. output->plugin = matching_plugins[0];
  45. output->actual_mime_type = mime_types[0];
  46. }
  47. std::move(callback).Run(std::move(output));
  48. }
  49. } // namespace electron