mime_util_xdg.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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/lazy_instance.h"
9#include "base/synchronization/lock.h"
10#include "base/third_party/xdg_mime/xdgmime.h"
11#include "base/threading/thread_restrictions.h"
12
13namespace base {
14namespace nix {
15
16namespace {
17
18// None of the XDG stuff is thread-safe, so serialize all access under
19// this lock.
20LazyInstance<Lock>::Leaky g_mime_util_xdg_lock = LAZY_INSTANCE_INITIALIZER;
21
22}  // namespace
23
24std::string GetFileMimeType(const FilePath& filepath) {
25  if (filepath.empty())
26    return std::string();
27  ThreadRestrictions::AssertIOAllowed();
28  AutoLock scoped_lock(g_mime_util_xdg_lock.Get());
29  return xdg_mime_get_mime_type_from_file_name(filepath.value().c_str());
30}
31
32std::string GetDataMimeType(const std::string& data) {
33  ThreadRestrictions::AssertIOAllowed();
34  AutoLock scoped_lock(g_mime_util_xdg_lock.Get());
35  return xdg_mime_get_mime_type_for_data(data.data(), data.length(), NULL);
36}
37
38}  // namespace nix
39}  // namespace base
40