Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / base / platform_mime_util_win.cc
blobfcac48faab53e3c10e0b12555a2c42edf720e444
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.
5 #include "net/base/platform_mime_util.h"
7 #include <string>
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/win/registry.h"
12 namespace net {
14 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
15 const base::FilePath::StringType& ext, std::string* result) const {
16 // check windows registry for file extension's mime type (registry key
17 // names are not case-sensitive).
18 std::wstring value, key = L"." + ext;
19 base::win::RegKey(HKEY_CLASSES_ROOT, key.c_str(), KEY_READ).ReadValue(
20 L"Content Type", &value);
21 if (!value.empty()) {
22 *result = base::WideToUTF8(value);
23 return true;
25 return false;
28 bool PlatformMimeUtil::GetPreferredExtensionForMimeType(
29 const std::string& mime_type, base::FilePath::StringType* ext) const {
30 std::wstring key(
31 L"MIME\\Database\\Content Type\\" + base::UTF8ToWide(mime_type));
32 if (base::win::RegKey(HKEY_CLASSES_ROOT, key.c_str(), KEY_READ).ReadValue(
33 L"Extension", ext) != ERROR_SUCCESS) {
34 return false;
36 // Strip off the leading dot, this should always be the case.
37 if (!ext->empty() && ext->at(0) == L'.')
38 ext->erase(ext->begin());
40 return true;
43 void PlatformMimeUtil::GetPlatformExtensionsForMimeType(
44 const std::string& mime_type,
45 base::hash_set<base::FilePath::StringType>* extensions) const {
46 // Multiple extensions could have the given mime type specified as their types
47 // in their 'HKCR\.<extension>\Content Type' keys. Iterating all the HKCR
48 // entries, though, is wildly impractical. Cheat by returning just the
49 // preferred extension.
50 base::FilePath::StringType ext;
51 if (GetPreferredExtensionForMimeType(mime_type, &ext))
52 extensions->insert(ext);
55 } // namespace net