summaryrefslogtreecommitdiff
path: root/chromium/net/base/platform_mime_util_win.cc
blob: 33d00d81264d1df89cc811b305e5c2fb16622914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/platform_mime_util.h"

#include <string>

#include "base/strings/utf_string_conversions.h"
#include "base/win/registry.h"

#include <windows.h>

namespace net {

bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
    const base::FilePath::StringType& ext, std::string* result) const {
  // check windows registry for file extension's mime type (registry key
  // names are not case-sensitive).
  base::FilePath::StringType value, key = FILE_PATH_LITERAL(".") + ext;
  base::win::RegKey(HKEY_CLASSES_ROOT, key.c_str(), KEY_READ)
      .ReadValue(L"Content Type", &value);
  if (!value.empty()) {
    *result = base::WideToUTF8(value);
    return true;
  }
  return false;
}

bool PlatformMimeUtil::GetPlatformPreferredExtensionForMimeType(
    const std::string& mime_type,
    base::FilePath::StringType* ext) const {
  base::FilePath::StringType key =
      L"MIME\\Database\\Content Type\\" + base::UTF8ToWide(mime_type);
  if (base::win::RegKey(HKEY_CLASSES_ROOT, key.c_str(), KEY_READ)
          .ReadValue(L"Extension", ext) != ERROR_SUCCESS) {
    return false;
  }
  // Strip off the leading dot, this should always be the case.
  if (!ext->empty() && ext->front() == '.')
    ext->erase(ext->begin());

  return true;
}

void PlatformMimeUtil::GetPlatformExtensionsForMimeType(
    const std::string& mime_type,
    std::unordered_set<base::FilePath::StringType>* extensions) const {
  // Multiple extensions could have the given mime type specified as their types
  // in their 'HKCR\.<extension>\Content Type' keys. Iterating all the HKCR
  // entries, though, is wildly impractical. Cheat by returning just the
  // preferred extension.
  base::FilePath::StringType ext;
  if (GetPlatformPreferredExtensionForMimeType(mime_type, &ext))
    extensions->insert(ext);
}

}  // namespace net