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