1 // Copyright 2020 The Chromium Embedded Framework 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 "libcef/common/app_manager.h" 6 7 #include "libcef/common/net/scheme_info.h" 8 #include "libcef/common/net/scheme_registration.h" 9 #include "libcef/common/scheme_registrar_impl.h" 10 11 #include "base/command_line.h" 12 #include "base/logging.h" 13 #include "content/public/browser/child_process_security_policy.h" 14 #include "content/public/common/content_switches.h" 15 16 #if BUILDFLAG(IS_WIN) 17 #include <windows.h> 18 #include "base/path_service.h" 19 #endif 20 21 namespace { 22 23 CefAppManager* g_manager = nullptr; 24 25 } // namespace 26 27 // static Get()28CefAppManager* CefAppManager::Get() { 29 return g_manager; 30 } 31 CefAppManager()32CefAppManager::CefAppManager() { 33 // Only a single instance should exist. 34 DCHECK(!g_manager); 35 g_manager = this; 36 } 37 ~CefAppManager()38CefAppManager::~CefAppManager() { 39 g_manager = nullptr; 40 } 41 AddCustomScheme(CefSchemeInfo * scheme_info)42void CefAppManager::AddCustomScheme(CefSchemeInfo* scheme_info) { 43 DCHECK(!scheme_info_list_locked_); 44 scheme_info_list_.push_back(*scheme_info); 45 46 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 47 if (!command_line->HasSwitch(switches::kProcessType)) { 48 // Register as a Web-safe scheme in the browser process so that requests for 49 // the scheme from a render process will be allowed in 50 // resource_dispatcher_host_impl.cc ShouldServiceRequest. 51 content::ChildProcessSecurityPolicy* policy = 52 content::ChildProcessSecurityPolicy::GetInstance(); 53 if (!policy->IsWebSafeScheme(scheme_info->scheme_name)) 54 policy->RegisterWebSafeScheme(scheme_info->scheme_name); 55 } 56 } 57 HasCustomScheme(const std::string & scheme_name)58bool CefAppManager::HasCustomScheme(const std::string& scheme_name) { 59 DCHECK(scheme_info_list_locked_); 60 if (scheme_info_list_.empty()) 61 return false; 62 63 SchemeInfoList::const_iterator it = scheme_info_list_.begin(); 64 for (; it != scheme_info_list_.end(); ++it) { 65 if (it->scheme_name == scheme_name) 66 return true; 67 } 68 69 return false; 70 } 71 GetCustomSchemes()72const CefAppManager::SchemeInfoList* CefAppManager::GetCustomSchemes() { 73 DCHECK(scheme_info_list_locked_); 74 return &scheme_info_list_; 75 } 76 AddAdditionalSchemes(content::ContentClient::Schemes * schemes)77void CefAppManager::AddAdditionalSchemes( 78 content::ContentClient::Schemes* schemes) { 79 DCHECK(!scheme_info_list_locked_); 80 81 auto application = GetApplication(); 82 if (application) { 83 CefSchemeRegistrarImpl schemeRegistrar; 84 application->OnRegisterCustomSchemes(&schemeRegistrar); 85 schemeRegistrar.GetSchemes(schemes); 86 } 87 88 scheme::AddInternalSchemes(schemes); 89 90 scheme_info_list_locked_ = true; 91 } 92 93 #if BUILDFLAG(IS_WIN) GetResourceDllName()94const wchar_t* CefAppManager::GetResourceDllName() { 95 static wchar_t file_path[MAX_PATH + 1] = {0}; 96 97 if (file_path[0] == 0) { 98 // Retrieve the module path (usually libcef.dll). 99 base::FilePath module; 100 base::PathService::Get(base::FILE_MODULE, &module); 101 const std::wstring wstr = module.value(); 102 size_t count = std::min(static_cast<size_t>(MAX_PATH), wstr.size()); 103 wcsncpy(file_path, wstr.c_str(), count); 104 file_path[count] = 0; 105 } 106 107 return file_path; 108 } 109 #endif // BUILDFLAG(IS_WIN) 110