CDMStorageIdProvider.cpp (2078B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "CDMStorageIdProvider.h" 7 8 #include "GMPLog.h" 9 #include "mozilla/IntegerPrintfMacros.h" 10 #include "nsCOMPtr.h" 11 #include "nsICryptoHash.h" 12 13 #ifdef SUPPORT_STORAGE_ID 14 # include "rlz/lib/machine_id.h" 15 #endif 16 17 namespace mozilla { 18 19 /*static*/ 20 nsCString CDMStorageIdProvider::ComputeStorageId(const nsCString& aOriginSalt) { 21 #ifndef SUPPORT_STORAGE_ID 22 return ""_ns; 23 #else 24 GMP_LOG_DEBUG("CDMStorageIdProvider::ComputeStorageId"); 25 26 std::string machineId; 27 if (!rlz_lib::GetMachineId(&machineId)) { 28 GMP_LOG_DEBUG( 29 "CDMStorageIdProvider::ComputeStorageId: get machineId failed."); 30 return ""_ns; 31 } 32 33 std::string originSalt(aOriginSalt.BeginReading(), aOriginSalt.Length()); 34 std::string input = 35 machineId + originSalt + CDMStorageIdProvider::kBrowserIdentifier; 36 nsCOMPtr<nsICryptoHash> hasher; 37 nsresult rv = NS_NewCryptoHash(nsICryptoHash::SHA256, getter_AddRefs(hasher)); 38 if (NS_WARN_IF(NS_FAILED(rv))) { 39 GMP_LOG_DEBUG( 40 "CDMStorageIdProvider::ComputeStorageId: failed to initialize " 41 "hash(0x%08" PRIx32 ")", 42 static_cast<uint32_t>(rv)); 43 return ""_ns; 44 } 45 46 rv = hasher->Update(reinterpret_cast<const uint8_t*>(input.c_str()), 47 input.size()); 48 if (NS_WARN_IF(NS_FAILED(rv))) { 49 GMP_LOG_DEBUG( 50 "CDMStorageIdProvider::ComputeStorageId: failed to update " 51 "hash(0x%08" PRIx32 ")", 52 static_cast<uint32_t>(rv)); 53 return ""_ns; 54 } 55 56 nsCString storageId; 57 rv = hasher->Finish(false, storageId); 58 if (NS_WARN_IF(NS_FAILED(rv))) { 59 GMP_LOG_DEBUG( 60 "CDMStorageIdProvider::ComputeStorageId: failed to get the final hash " 61 "result(0x%08" PRIx32 ")", 62 static_cast<uint32_t>(rv)); 63 return ""_ns; 64 } 65 return storageId; 66 #endif 67 } 68 69 } // namespace mozilla