blob: 7c360e27806f92c94f37b606aae58c0b91a200c7 [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,
danakjf4b9e942019-11-29 15:43:0432 web::URLDataSourceIOS::GotDataCallback callback) override;
michaeldo352029b2017-05-10 20:41:3833 std::string GetMimeType(const std::string& path) const override;
34 bool ShouldDenyXFrameOptions() const override;
35
36 // Send the response data.
danakjf4b9e942019-11-29 15:43:0437 void FinishDataRequest(const std::string& html,
38 web::URLDataSourceIOS::GotDataCallback callback);
michaeldo352029b2017-05-10 20:41:3839
40 private:
41 ~TermsUIHTMLSource() override;
42
43 std::string source_name_;
44
45 DISALLOW_COPY_AND_ASSIGN(TermsUIHTMLSource);
46};
47
48} // namespace
49
50TermsUIHTMLSource::TermsUIHTMLSource(const std::string& source_name)
51 : source_name_(source_name) {}
52
53TermsUIHTMLSource::~TermsUIHTMLSource() {}
54
55std::string TermsUIHTMLSource::GetSource() const {
56 return source_name_;
57}
58
59void TermsUIHTMLSource::StartDataRequest(
60 const std::string& path,
danakjf4b9e942019-11-29 15:43:0461 web::URLDataSourceIOS::GotDataCallback callback) {
michaeldo352029b2017-05-10 20:41:3862 NSString* terms_of_service_path =
63 base::SysUTF8ToNSString(GetTermsOfServicePath());
64 NSString* bundle_path = [base::mac::FrameworkBundle() bundlePath];
65 NSString* full_path =
66 [bundle_path stringByAppendingPathComponent:terms_of_service_path];
67 DCHECK(full_path);
68
69 NSError* error = nil;
70 NSString* content = [NSString stringWithContentsOfFile:full_path
71 encoding:NSUTF8StringEncoding
72 error:&error];
73 DCHECK(!error && [content length]);
danakjf4b9e942019-11-29 15:43:0474 FinishDataRequest(base::SysNSStringToUTF8(content), std::move(callback));
michaeldo352029b2017-05-10 20:41:3875}
76
77void TermsUIHTMLSource::FinishDataRequest(
78 const std::string& html,
danakjf4b9e942019-11-29 15:43:0479 web::URLDataSourceIOS::GotDataCallback callback) {
michaeldo352029b2017-05-10 20:41:3880 std::string html_copy(html);
danakjf4b9e942019-11-29 15:43:0481 std::move(callback).Run(base::RefCountedString::TakeString(&html_copy));
michaeldo352029b2017-05-10 20:41:3882}
83
84std::string TermsUIHTMLSource::GetMimeType(const std::string& path) const {
85 return "text/html";
86}
87
88bool TermsUIHTMLSource::ShouldDenyXFrameOptions() const {
89 return web::URLDataSourceIOS::ShouldDenyXFrameOptions();
90}
91
92TermsUI::TermsUI(web::WebUIIOS* web_ui, const std::string& name)
93 : web::WebUIIOSController(web_ui) {
94 web::URLDataSourceIOS::Add(ios::ChromeBrowserState::FromWebUIIOS(web_ui),
95 new TermsUIHTMLSource(name));
96}
97
98TermsUI::~TermsUI() {}