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 "chrome/common/net/net_resource_provider.h"
6
7 #include <string>
8
9 #include "base/i18n/rtl.h"
10 #include "base/strings/string_piece.h"
11 #include "base/values.h"
12 #include "chrome/grit/chromium_strings.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "net/grit/net_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/layout.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/base/webui/jstemplate_builder.h"
19
20 namespace {
21
22 // The net module doesn't have access to this HTML or the strings that need to
23 // be localized. The Chrome locale will never change while we're running, so
24 // it's safe to have a static string that we always return a pointer into.
25 // This allows us to have the ResourceProvider return a pointer into the actual
26 // resource (via a StringPiece), instead of always copying resources.
27 struct LazyDirectoryListerCacher {
LazyDirectoryListerCacher__anone4fe97490111::LazyDirectoryListerCacher28 LazyDirectoryListerCacher() {
29 base::DictionaryValue value;
30 value.SetString("header",
31 l10n_util::GetStringUTF16(IDS_DIRECTORY_LISTING_HEADER));
32 value.SetString("parentDirText",
33 l10n_util::GetStringUTF16(IDS_DIRECTORY_LISTING_PARENT));
34 value.SetString("headerName",
35 l10n_util::GetStringUTF16(IDS_DIRECTORY_LISTING_NAME));
36 value.SetString("headerSize",
37 l10n_util::GetStringUTF16(IDS_DIRECTORY_LISTING_SIZE));
38 value.SetString("headerDateModified",
39 l10n_util::GetStringUTF16(IDS_DIRECTORY_LISTING_DATE_MODIFIED));
40 value.SetString("listingParsingErrorBoxText",
41 l10n_util::GetStringFUTF16(IDS_DIRECTORY_LISTING_PARSING_ERROR_BOX_TEXT,
42 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)));
43 value.SetString("textdirection", base::i18n::IsRTL() ? "rtl" : "ltr");
44 html_data = webui::GetI18nTemplateHtml(
45 ResourceBundle::GetSharedInstance().GetRawDataResource(
46 IDR_DIR_HEADER_HTML),
47 &value);
48 }
49
50 std::string html_data;
51 };
52
53 } // namespace
54
55 namespace chrome_common_net {
56
NetResourceProvider(int key)57 base::StringPiece NetResourceProvider(int key) {
58 CR_DEFINE_STATIC_LOCAL(LazyDirectoryListerCacher, lazy_dir_lister, ());
59
60 if (IDR_DIR_HEADER_HTML == key)
61 return base::StringPiece(lazy_dir_lister.html_data);
62
63 return ResourceBundle::GetSharedInstance().GetRawDataResource(key);
64 }
65
66 } // namespace chrome_common_net
67