summaryrefslogtreecommitdiffstats
path: root/ios/web/webui/web_ui_ios_data_source_impl.cc
blob: ee0ed423c50983ba8d7853f7f084f13dbc6fa007 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2014 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 "ios/web/webui/web_ui_ios_data_source_impl.h"

#include <string>

#include "base/bind.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_util.h"
#include "ios/web/public/web_client.h"
#include "ui/base/webui/jstemplate_builder.h"
#include "ui/base/webui/web_ui_util.h"

namespace web {

// static
WebUIIOSDataSource* WebUIIOSDataSource::Create(const std::string& source_name) {
  return new WebUIIOSDataSourceImpl(source_name);
}

// static
void WebUIIOSDataSource::Add(BrowserState* browser_state,
                             WebUIIOSDataSource* source) {
  URLDataManagerIOS::AddWebUIIOSDataSource(browser_state, source);
}

// Internal class to hide the fact that WebUIIOSDataSourceImpl implements
// URLDataSourceIOS.
class WebUIIOSDataSourceImpl::InternalDataSource : public URLDataSourceIOS {
 public:
  InternalDataSource(WebUIIOSDataSourceImpl* parent) : parent_(parent) {}

  ~InternalDataSource() override {}

  // URLDataSourceIOS implementation.
  std::string GetSource() const override { return parent_->GetSource(); }
  std::string GetMimeType(const std::string& path) const override {
    return parent_->GetMimeType(path);
  }
  void StartDataRequest(
      const std::string& path,
      const URLDataSourceIOS::GotDataCallback& callback) override {
    return parent_->StartDataRequest(path, callback);
  }
  bool ShouldReplaceExistingSource() const override {
    return parent_->replace_existing_source_;
  }
  bool AllowCaching() const override { return false; }
  bool ShouldDenyXFrameOptions() const override {
    return parent_->deny_xframe_options_;
  }

 private:
  WebUIIOSDataSourceImpl* parent_;
};

WebUIIOSDataSourceImpl::WebUIIOSDataSourceImpl(const std::string& source_name)
    : URLDataSourceIOSImpl(source_name, new InternalDataSource(this)),
      source_name_(source_name),
      default_resource_(-1),
      deny_xframe_options_(true),
      disable_set_font_strings_(false),
      replace_existing_source_(true) {
}

WebUIIOSDataSourceImpl::~WebUIIOSDataSourceImpl() {
}

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

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

void WebUIIOSDataSourceImpl::AddLocalizedString(const std::string& name,
                                                int ids) {
  localized_strings_.SetString(name, GetWebClient()->GetLocalizedString(ids));
}

void WebUIIOSDataSourceImpl::AddBoolean(const std::string& name, bool value) {
  localized_strings_.SetBoolean(name, value);
}

void WebUIIOSDataSourceImpl::SetJsonPath(const std::string& path) {
  json_path_ = path;
}

void WebUIIOSDataSourceImpl::AddResourcePath(const std::string& path,
                                             int resource_id) {
  path_to_idr_map_[path] = resource_id;
}

void WebUIIOSDataSourceImpl::SetDefaultResource(int resource_id) {
  default_resource_ = resource_id;
}

void WebUIIOSDataSourceImpl::DisableDenyXFrameOptions() {
  deny_xframe_options_ = false;
}

std::string WebUIIOSDataSourceImpl::GetSource() const {
  return source_name_;
}

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

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

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

  if (base::EndsWith(path, ".css", false))
    return "text/css";

  if (base::EndsWith(path, ".svg", false))
    return "image/svg+xml";

  return "text/html";
}

void WebUIIOSDataSourceImpl::StartDataRequest(
    const std::string& path,
    const URLDataSourceIOS::GotDataCallback& callback) {
  if (!json_path_.empty() && path == json_path_) {
    SendLocalizedStringsAsJSON(callback);
    return;
  }

  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(callback, resource_id);
}

void WebUIIOSDataSourceImpl::SendLocalizedStringsAsJSON(
    const URLDataSourceIOS::GotDataCallback& callback) {
  std::string template_data;
  if (!disable_set_font_strings_) {
    webui::SetLoadTimeDataDefaults(web::GetWebClient()->GetApplicationLocale(),
                                   &localized_strings_);
  }

  webui::AppendJsonJS(&localized_strings_, &template_data);
  callback.Run(base::RefCountedString::TakeString(&template_data));
}

void WebUIIOSDataSourceImpl::SendFromResourceBundle(
    const URLDataSourceIOS::GotDataCallback& callback,
    int idr) {
  scoped_refptr<base::RefCountedStaticMemory> response(
      GetWebClient()->GetDataResourceBytes(idr));
  callback.Run(response.get());
}

}  // namespace web