core_winrt_util.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2017 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 "base/win/core_winrt_util.h"
  5. #include "base/threading/scoped_thread_priority.h"
  6. namespace {
  7. FARPROC LoadComBaseFunction(const char* function_name) {
  8. static HMODULE const handle = []() {
  9. // Mitigate the issues caused by loading DLLs on a background thread
  10. // (http://crbug/973868).
  11. SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY();
  12. return ::LoadLibraryEx(L"combase.dll", nullptr,
  13. LOAD_LIBRARY_SEARCH_SYSTEM32);
  14. }();
  15. return handle ? ::GetProcAddress(handle, function_name) : nullptr;
  16. }
  17. decltype(&::RoActivateInstance) GetRoActivateInstanceFunction() {
  18. static decltype(&::RoActivateInstance) const function =
  19. reinterpret_cast<decltype(&::RoActivateInstance)>(
  20. LoadComBaseFunction("RoActivateInstance"));
  21. return function;
  22. }
  23. decltype(&::RoGetActivationFactory) GetRoGetActivationFactoryFunction() {
  24. static decltype(&::RoGetActivationFactory) const function =
  25. reinterpret_cast<decltype(&::RoGetActivationFactory)>(
  26. LoadComBaseFunction("RoGetActivationFactory"));
  27. return function;
  28. }
  29. } // namespace
  30. namespace base {
  31. namespace win {
  32. bool ResolveCoreWinRTDelayload() {
  33. // TODO(finnur): Add AssertIOAllowed once crbug.com/770193 is fixed.
  34. return GetRoActivateInstanceFunction() && GetRoGetActivationFactoryFunction();
  35. }
  36. HRESULT RoGetActivationFactory(HSTRING class_id,
  37. const IID& iid,
  38. void** out_factory) {
  39. auto get_factory_func = GetRoGetActivationFactoryFunction();
  40. if (!get_factory_func)
  41. return E_FAIL;
  42. return get_factory_func(class_id, iid, out_factory);
  43. }
  44. HRESULT RoActivateInstance(HSTRING class_id, IInspectable** instance) {
  45. auto activate_instance_func = GetRoActivateInstanceFunction();
  46. if (!activate_instance_func)
  47. return E_FAIL;
  48. return activate_instance_func(class_id, instance);
  49. }
  50. } // namespace win
  51. } // namespace base