summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/chrome_web_ui_data_source.cc
blob: ae8b292c6862e1358d84977d35964fc3b4b754b0 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2011 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 "chrome/browser/ui/webui/chrome_web_ui_data_source.h"

#include <string>

#include "base/memory/ref_counted_memory.h"
#include "base/string_util.h"
#include "chrome/common/jstemplate_builder.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"

ChromeWebUIDataSource::ChromeWebUIDataSource(const std::string& source_name)
    : DataSource(source_name, MessageLoop::current()),
      default_resource_(-1) {
}

ChromeWebUIDataSource::ChromeWebUIDataSource(const std::string& source_name,
                                             MessageLoop* loop)
    : DataSource(source_name, loop),
      default_resource_(-1) {
}

ChromeWebUIDataSource::~ChromeWebUIDataSource() {
}

void ChromeWebUIDataSource::AddString(const std::string& name,
                                      const string16& value) {
  localized_strings_.SetString(name, value);
}

void ChromeWebUIDataSource::AddLocalizedString(const std::string& name,
                                               int ids) {
  localized_strings_.SetString(name, l10n_util::GetStringUTF16(ids));
}

void ChromeWebUIDataSource::AddLocalizedStrings(
    const DictionaryValue& localized_strings) {
  localized_strings_.MergeDictionary(&localized_strings);
}

std::string ChromeWebUIDataSource::GetMimeType(const std::string& path) const {
  if (EndsWith(path, ".js", false))
    return "application/javascript";

  if (EndsWith(path, ".json", false))
    return "application/json";

  if (EndsWith(path, ".pdf", false))
    return "application/pdf";

  return "text/html";
}

void ChromeWebUIDataSource::StartDataRequest(const std::string& path,
                                             bool is_incognito,
                                             int request_id) {
  if (!json_path_.empty() && path == json_path_) {
    SendLocalizedStringsAsJSON(request_id);
  } else {
    int resource_id = default_resource_;
    std::map<std::string, int>::iterator result;
    result = path_to_idr_map_.find(path);
    if (result != path_to_idr_map_.end())
      resource_id = result->second;
    DCHECK_NE(resource_id, -1);
    SendFromResourceBundle(request_id, resource_id);
  }
}

void ChromeWebUIDataSource::SendLocalizedStringsAsJSON(int request_id) {
  std::string template_data;
  SetFontAndTextDirection(&localized_strings_);
  jstemplate_builder::AppendJsonJS(&localized_strings_, &template_data);
  SendResponse(request_id, base::RefCountedString::TakeString(&template_data));
}

void ChromeWebUIDataSource::SendFromResourceBundle(int request_id, int idr) {
  scoped_refptr<RefCountedStaticMemory> response(
      ResourceBundle::GetSharedInstance().LoadDataResourceBytes(idr));
  SendResponse(request_id, response);
}