blob: 8ff629bfa33f2b733cc03dae8104df02d572e030 [file] [log] [blame]
michaeldo352029b2017-05-10 20:41:381// Copyright 2017 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#import "ios/chrome/browser/ui/webui/terms_ui.h"
6
7#import <Foundation/Foundation.h>
8
9#include "base/mac/bundle_locations.h"
10#include "base/memory/ref_counted_memory.h"
11#import "base/strings/sys_string_conversions.h"
12#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
Gauthier Ambard4e8eff12019-04-04 09:42:4113#include "ios/chrome/browser/ui/util/terms_util.h"
Mark Cogan33a826a2019-05-29 13:56:5714#include "ios/web/public/webui/url_data_source_ios.h"
michaeldo352029b2017-05-10 20:41:3815#include "ios/web/public/webui/web_ui_ios.h"
16
17#if !defined(__has_feature) || !__has_feature(objc_arc)
18#error "This file requires ARC support."
19#endif
20
21namespace {
22
23class TermsUIHTMLSource : public web::URLDataSourceIOS {
24 public:
25 // Construct a data source for the specified |source_name|.
26 explicit TermsUIHTMLSource(const std::string& source_name);
27
28 // web::URLDataSourceIOS implementation.
29 std::string GetSource() const override;
30 void StartDataRequest(
31 const std::string& path,
32 const web::URLDataSourceIOS::GotDataCallback& callback) override;
33 std::string GetMimeType(const std::string& path) const override;
34 bool ShouldDenyXFrameOptions() const override;
35
36 // Send the response data.
37 void FinishDataRequest(
38 const std::string& html,
39 const web::URLDataSourceIOS::GotDataCallback& callback);
40
41 private:
42 ~TermsUIHTMLSource() override;
43
44 std::string source_name_;
45
46 DISALLOW_COPY_AND_ASSIGN(TermsUIHTMLSource);
47};
48
49} // namespace
50
51TermsUIHTMLSource::TermsUIHTMLSource(const std::string& source_name)
52 : source_name_(source_name) {}
53
54TermsUIHTMLSource::~TermsUIHTMLSource() {}
55
56std::string TermsUIHTMLSource::GetSource() const {
57 return source_name_;
58}
59
60void TermsUIHTMLSource::StartDataRequest(
61 const std::string& path,
62 const web::URLDataSourceIOS::GotDataCallback& callback) {
63 NSString* terms_of_service_path =
64 base::SysUTF8ToNSString(GetTermsOfServicePath());
65 NSString* bundle_path = [base::mac::FrameworkBundle() bundlePath];
66 NSString* full_path =
67 [bundle_path stringByAppendingPathComponent:terms_of_service_path];
68 DCHECK(full_path);
69
70 NSError* error = nil;
71 NSString* content = [NSString stringWithContentsOfFile:full_path
72 encoding:NSUTF8StringEncoding
73 error:&error];
74 DCHECK(!error && [content length]);
75 FinishDataRequest(base::SysNSStringToUTF8(content), callback);
76}
77
78void TermsUIHTMLSource::FinishDataRequest(
79 const std::string& html,
80 const web::URLDataSourceIOS::GotDataCallback& callback) {
81 std::string html_copy(html);
82 callback.Run(base::RefCountedString::TakeString(&html_copy));
83}
84
85std::string TermsUIHTMLSource::GetMimeType(const std::string& path) const {
86 return "text/html";
87}
88
89bool TermsUIHTMLSource::ShouldDenyXFrameOptions() const {
90 return web::URLDataSourceIOS::ShouldDenyXFrameOptions();
91}
92
93TermsUI::TermsUI(web::WebUIIOS* web_ui, const std::string& name)
94 : web::WebUIIOSController(web_ui) {
95 web::URLDataSourceIOS::Add(ios::ChromeBrowserState::FromWebUIIOS(web_ui),
96 new TermsUIHTMLSource(name));
97}
98
99TermsUI::~TermsUI() {}