OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/common/file_util.h" | 5 #include "extensions/common/file_util.h" |
6 | 6 |
| 7 #include <set> |
7 #include <string> | 8 #include <string> |
| 9 #include <utility> |
8 | 10 |
9 #include "base/file_util.h" | 11 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "extensions/common/constants.h" |
| 15 #include "extensions/common/extension_l10n_util.h" |
| 16 #include "grit/generated_resources.h" |
11 #include "net/base/escape.h" | 17 #include "net/base/escape.h" |
| 18 #include "ui/base/l10n/l10n_util.h" |
12 #include "url/gurl.h" | 19 #include "url/gurl.h" |
13 | 20 |
14 namespace extensions { | 21 namespace extensions { |
15 namespace file_util { | 22 namespace file_util { |
16 | 23 |
17 base::FilePath ExtensionURLToRelativeFilePath(const GURL& url) { | 24 base::FilePath ExtensionURLToRelativeFilePath(const GURL& url) { |
18 std::string url_path = url.path(); | 25 std::string url_path = url.path(); |
19 if (url_path.empty() || url_path[0] != '/') | 26 if (url_path.empty() || url_path[0] != '/') |
20 return base::FilePath(); | 27 return base::FilePath(); |
21 | 28 |
(...skipping 28 matching lines...) Expand all Loading... |
50 | 57 |
51 base::FilePath path = root.AppendASCII(host).Append(relative_path); | 58 base::FilePath path = root.AppendASCII(host).Append(relative_path); |
52 if (!base::PathExists(path)) | 59 if (!base::PathExists(path)) |
53 return base::FilePath(); | 60 return base::FilePath(); |
54 path = base::MakeAbsoluteFilePath(path); | 61 path = base::MakeAbsoluteFilePath(path); |
55 if (path.empty() || !root.IsParent(path)) | 62 if (path.empty() || !root.IsParent(path)) |
56 return base::FilePath(); | 63 return base::FilePath(); |
57 return path; | 64 return path; |
58 } | 65 } |
59 | 66 |
| 67 MessageBundle* LoadMessageBundle( |
| 68 const base::FilePath& extension_path, |
| 69 const std::string& default_locale, |
| 70 std::string* error) { |
| 71 error->clear(); |
| 72 // Load locale information if available. |
| 73 base::FilePath locale_path = extension_path.Append(kLocaleFolder); |
| 74 if (!base::PathExists(locale_path)) |
| 75 return NULL; |
| 76 |
| 77 std::set<std::string> locales; |
| 78 if (!extension_l10n_util::GetValidLocales(locale_path, &locales, error)) |
| 79 return NULL; |
| 80 |
| 81 if (default_locale.empty() || locales.find(default_locale) == locales.end()) { |
| 82 *error = l10n_util::GetStringUTF8( |
| 83 IDS_EXTENSION_LOCALES_NO_DEFAULT_LOCALE_SPECIFIED); |
| 84 return NULL; |
| 85 } |
| 86 |
| 87 MessageBundle* message_bundle = |
| 88 extension_l10n_util::LoadMessageCatalogs( |
| 89 locale_path, |
| 90 default_locale, |
| 91 extension_l10n_util::CurrentLocaleOrDefault(), |
| 92 locales, |
| 93 error); |
| 94 |
| 95 return message_bundle; |
| 96 } |
| 97 |
| 98 MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMap( |
| 99 const base::FilePath& extension_path, |
| 100 const std::string& extension_id, |
| 101 const std::string& default_locale) { |
| 102 MessageBundle::SubstitutionMap* returnValue = |
| 103 new MessageBundle::SubstitutionMap(); |
| 104 if (!default_locale.empty()) { |
| 105 // Touch disk only if extension is localized. |
| 106 std::string error; |
| 107 scoped_ptr<MessageBundle> bundle( |
| 108 LoadMessageBundle(extension_path, default_locale, &error)); |
| 109 |
| 110 if (bundle.get()) |
| 111 *returnValue = *bundle->dictionary(); |
| 112 } |
| 113 |
| 114 // Add @@extension_id reserved message here, so it's available to |
| 115 // non-localized extensions too. |
| 116 returnValue->insert( |
| 117 std::make_pair(MessageBundle::kExtensionIdKey, extension_id)); |
| 118 |
| 119 return returnValue; |
| 120 } |
| 121 |
60 } // namespace file_util | 122 } // namespace file_util |
61 } // namespace extensions | 123 } // namespace extensions |
OLD | NEW |