michaeldo | 352029b | 2017-05-10 20:41:38 | [diff] [blame] | 1 | // 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 Ambard | 4e8eff1 | 2019-04-04 09:42:41 | [diff] [blame] | 13 | #include "ios/chrome/browser/ui/util/terms_util.h" |
Mark Cogan | 33a826a | 2019-05-29 13:56:57 | [diff] [blame] | 14 | #include "ios/web/public/webui/url_data_source_ios.h" |
michaeldo | 352029b | 2017-05-10 20:41:38 | [diff] [blame] | 15 | #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 | |
| 21 | namespace { |
| 22 | |
| 23 | class 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, |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 32 | web::URLDataSourceIOS::GotDataCallback callback) override; |
michaeldo | 352029b | 2017-05-10 20:41:38 | [diff] [blame] | 33 | std::string GetMimeType(const std::string& path) const override; |
| 34 | bool ShouldDenyXFrameOptions() const override; |
| 35 | |
| 36 | // Send the response data. |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 37 | void FinishDataRequest(const std::string& html, |
| 38 | web::URLDataSourceIOS::GotDataCallback callback); |
michaeldo | 352029b | 2017-05-10 20:41:38 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | ~TermsUIHTMLSource() override; |
| 42 | |
| 43 | std::string source_name_; |
| 44 | |
| 45 | DISALLOW_COPY_AND_ASSIGN(TermsUIHTMLSource); |
| 46 | }; |
| 47 | |
| 48 | } // namespace |
| 49 | |
| 50 | TermsUIHTMLSource::TermsUIHTMLSource(const std::string& source_name) |
| 51 | : source_name_(source_name) {} |
| 52 | |
| 53 | TermsUIHTMLSource::~TermsUIHTMLSource() {} |
| 54 | |
| 55 | std::string TermsUIHTMLSource::GetSource() const { |
| 56 | return source_name_; |
| 57 | } |
| 58 | |
| 59 | void TermsUIHTMLSource::StartDataRequest( |
| 60 | const std::string& path, |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 61 | web::URLDataSourceIOS::GotDataCallback callback) { |
michaeldo | 352029b | 2017-05-10 20:41:38 | [diff] [blame] | 62 | 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]); |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 74 | FinishDataRequest(base::SysNSStringToUTF8(content), std::move(callback)); |
michaeldo | 352029b | 2017-05-10 20:41:38 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void TermsUIHTMLSource::FinishDataRequest( |
| 78 | const std::string& html, |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 79 | web::URLDataSourceIOS::GotDataCallback callback) { |
michaeldo | 352029b | 2017-05-10 20:41:38 | [diff] [blame] | 80 | std::string html_copy(html); |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame^] | 81 | std::move(callback).Run(base::RefCountedString::TakeString(&html_copy)); |
michaeldo | 352029b | 2017-05-10 20:41:38 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | std::string TermsUIHTMLSource::GetMimeType(const std::string& path) const { |
| 85 | return "text/html"; |
| 86 | } |
| 87 | |
| 88 | bool TermsUIHTMLSource::ShouldDenyXFrameOptions() const { |
| 89 | return web::URLDataSourceIOS::ShouldDenyXFrameOptions(); |
| 90 | } |
| 91 | |
| 92 | TermsUI::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 | |
| 98 | TermsUI::~TermsUI() {} |