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