common_mac.mm 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2018 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 "components/services/quarantine/common_mac.h"
  5. #import <ApplicationServices/ApplicationServices.h>
  6. #include "base/files/file_path.h"
  7. #include "base/logging.h"
  8. #include "base/mac/foundation_util.h"
  9. #include "base/mac/mac_logging.h"
  10. #include "base/mac/mac_util.h"
  11. #include "base/mac/scoped_cftyperef.h"
  12. #include "base/strings/sys_string_conversions.h"
  13. namespace quarantine {
  14. bool GetQuarantineProperties(
  15. const base::FilePath& file,
  16. base::scoped_nsobject<NSMutableDictionary>* properties) {
  17. base::scoped_nsobject<NSURL> file_url([[NSURL alloc]
  18. initFileURLWithPath:base::SysUTF8ToNSString(file.value())]);
  19. if (!file_url)
  20. return false;
  21. NSError* error = nil;
  22. id quarantine_properties = nil;
  23. BOOL success = [file_url getResourceValue:&quarantine_properties
  24. forKey:NSURLQuarantinePropertiesKey
  25. error:&error];
  26. if (!success) {
  27. std::string error_message(error ? error.description.UTF8String : "");
  28. LOG(WARNING) << "Unable to get quarantine attributes for file "
  29. << file.value() << ". Error: " << error_message;
  30. return false;
  31. }
  32. if (!quarantine_properties)
  33. return true;
  34. NSDictionary* quarantine_properties_dict =
  35. base::mac::ObjCCast<NSDictionary>(quarantine_properties);
  36. if (!quarantine_properties_dict) {
  37. LOG(WARNING) << "Quarantine properties have wrong class: "
  38. << base::SysNSStringToUTF8(
  39. [[quarantine_properties class] description]);
  40. return false;
  41. }
  42. properties->reset([quarantine_properties_dict mutableCopy]);
  43. return true;
  44. }
  45. } // namespace quarantine