mime_util_xdg.cc 990 B

12345678910111213141516171819202122232425262728293031
  1. // Copyright (c) 2012 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/nix/mime_util_xdg.h"
  5. #include "base/files/file_path.h"
  6. #include "base/no_destructor.h"
  7. #include "base/synchronization/lock.h"
  8. #include "base/third_party/xdg_mime/xdgmime.h"
  9. #include "base/threading/scoped_blocking_call.h"
  10. namespace base {
  11. namespace nix {
  12. std::string GetFileMimeType(const FilePath& filepath) {
  13. if (filepath.empty())
  14. return std::string();
  15. base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
  16. base::BlockingType::MAY_BLOCK);
  17. // None of the XDG stuff is thread-safe, so serialize all access under this
  18. // lock.
  19. static NoDestructor<Lock> mime_util_xdg_lock;
  20. AutoLock scoped_lock(*mime_util_xdg_lock);
  21. return xdg_mime_get_mime_type_from_file_name(filepath.value().c_str());
  22. }
  23. } // namespace nix
  24. } // namespace base