| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/ui/reader_mode/reader_mode_controller.h" | 5 #import "ios/chrome/browser/ui/reader_mode/reader_mode_controller.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/ios/weak_nsobject.h" | |
| 11 #include "base/mac/bind_objc_block.h" | 10 #include "base/mac/bind_objc_block.h" |
| 12 #include "base/mac/scoped_nsobject.h" | |
| 13 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 15 #include "components/infobars/core/infobar.h" | 13 #include "components/infobars/core/infobar.h" |
| 16 #include "components/infobars/core/infobar_manager.h" | 14 #include "components/infobars/core/infobar_manager.h" |
| 17 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 15 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 18 #include "ios/chrome/browser/dom_distiller/distiller_viewer.h" | 16 #include "ios/chrome/browser/dom_distiller/distiller_viewer.h" |
| 19 #include "ios/chrome/browser/dom_distiller/dom_distiller_service_factory.h" | 17 #include "ios/chrome/browser/dom_distiller/dom_distiller_service_factory.h" |
| 20 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" | 18 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" |
| 21 #import "ios/chrome/browser/ui/reader_mode/reader_mode_checker.h" | 19 #import "ios/chrome/browser/ui/reader_mode/reader_mode_checker.h" |
| 22 #import "ios/chrome/browser/ui/reader_mode/reader_mode_infobar_delegate.h" | 20 #import "ios/chrome/browser/ui/reader_mode/reader_mode_infobar_delegate.h" |
| 23 #import "ios/chrome/browser/ui/reader_mode/reader_mode_view.h" | 21 #import "ios/chrome/browser/ui/reader_mode/reader_mode_view.h" |
| 24 #include "ios/web/public/browser_state.h" | 22 #include "ios/web/public/browser_state.h" |
| 25 #include "ios/web/public/web_state/web_state.h" | 23 #include "ios/web/public/web_state/web_state.h" |
| 26 | 24 |
| 25 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 26 #error "This file requires ARC support." |
| 27 #endif |
| 28 |
| 27 @protocol ReaderModeCheckerObserverBridgeProtocol | 29 @protocol ReaderModeCheckerObserverBridgeProtocol |
| 28 - (void)pageIsDistillable; | 30 - (void)pageIsDistillable; |
| 29 @end | 31 @end |
| 30 | 32 |
| 31 @protocol InfoBarManagerObserverBridgeProtocol | 33 @protocol InfoBarManagerObserverBridgeProtocol |
| 32 - (void)infoBarRemoved:(infobars::InfoBar*)infobar; | 34 - (void)infoBarRemoved:(infobars::InfoBar*)infobar; |
| 33 @end | 35 @end |
| 34 | 36 |
| 35 namespace { | 37 namespace { |
| 36 // Used to find the ReaderModeView in the view hierarchy. | 38 // Used to find the ReaderModeView in the view hierarchy. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 ReaderModeCheckerObserverBridgeProtocol, | 91 ReaderModeCheckerObserverBridgeProtocol, |
| 90 InfoBarManagerObserverBridgeProtocol> { | 92 InfoBarManagerObserverBridgeProtocol> { |
| 91 std::unique_ptr<ReaderModeChecker> _checker; | 93 std::unique_ptr<ReaderModeChecker> _checker; |
| 92 std::unique_ptr<ReaderModeCheckerObserverBridge> _checkerBridge; | 94 std::unique_ptr<ReaderModeCheckerObserverBridge> _checkerBridge; |
| 93 std::unique_ptr<InfoBarManagerObserverBridge> _infoBarBridge; | 95 std::unique_ptr<InfoBarManagerObserverBridge> _infoBarBridge; |
| 94 std::unique_ptr<dom_distiller::DistillerViewer> _viewer; | 96 std::unique_ptr<dom_distiller::DistillerViewer> _viewer; |
| 95 // The currently displayed infobar. | 97 // The currently displayed infobar. |
| 96 infobars::InfoBar* infobar_; | 98 infobars::InfoBar* infobar_; |
| 97 web::WebState* _webState; | 99 web::WebState* _webState; |
| 98 } | 100 } |
| 99 @property(readonly, nonatomic) id<ReaderModeControllerDelegate> delegate; | 101 @property(weak, readonly, nonatomic) id<ReaderModeControllerDelegate> delegate; |
| 100 | 102 |
| 101 // Triggers a distillation and returns a DistillerViewer to keep as a handle to | 103 // Triggers a distillation and returns a DistillerViewer to keep as a handle to |
| 102 // the running distillation. | 104 // the running distillation. |
| 103 - (std::unique_ptr<dom_distiller::DistillerViewer>)startDistillation | 105 - (std::unique_ptr<dom_distiller::DistillerViewer>)startDistillation |
| 104 WARN_UNUSED_RESULT; | 106 WARN_UNUSED_RESULT; |
| 105 - (void)distillationFinished:(const std::string&)html forURL:(const GURL&)url; | 107 - (void)distillationFinished:(const std::string&)html forURL:(const GURL&)url; |
| 106 | 108 |
| 107 // Returns a ReaderModeView that presents a waiting UI while the distillation | 109 // Returns a ReaderModeView that presents a waiting UI while the distillation |
| 108 // is taking place. Releasing this view will stop the distillation in progress. | 110 // is taking place. Releasing this view will stop the distillation in progress. |
| 109 - (ReaderModeView*)readerModeViewWithFrame:(CGRect)frame; | 111 - (ReaderModeView*)readerModeViewWithFrame:(CGRect)frame; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 135 } | 137 } |
| 136 | 138 |
| 137 - (instancetype)init { | 139 - (instancetype)init { |
| 138 NOTREACHED(); | 140 NOTREACHED(); |
| 139 return nil; | 141 return nil; |
| 140 } | 142 } |
| 141 | 143 |
| 142 - (void)dealloc { | 144 - (void)dealloc { |
| 143 if (_webState) | 145 if (_webState) |
| 144 [self detachFromWebState]; | 146 [self detachFromWebState]; |
| 145 [super dealloc]; | |
| 146 } | 147 } |
| 147 | 148 |
| 148 - (void)detachFromWebState { | 149 - (void)detachFromWebState { |
| 149 [self removeInfoBar]; | 150 [self removeInfoBar]; |
| 150 _webState = nullptr; | 151 _webState = nullptr; |
| 151 } | 152 } |
| 152 | 153 |
| 153 // Property accessor. | 154 // Property accessor. |
| 154 - (ReaderModeChecker*)checker { | 155 - (ReaderModeChecker*)checker { |
| 155 return _checker.get(); | 156 return _checker.get(); |
| 156 } | 157 } |
| 157 | 158 |
| 158 #pragma mark - Private methods. | 159 #pragma mark - Private methods. |
| 159 #pragma mark distillation | 160 #pragma mark distillation |
| 160 | 161 |
| 161 - (std::unique_ptr<dom_distiller::DistillerViewer>)startDistillation { | 162 - (std::unique_ptr<dom_distiller::DistillerViewer>)startDistillation { |
| 162 DCHECK(_webState); | 163 DCHECK(_webState); |
| 163 base::WeakNSObject<ReaderModeController> weakSelf(self); | 164 __weak ReaderModeController* weakSelf = self; |
| 164 GURL pageURL = _webState->GetLastCommittedURL(); | 165 GURL pageURL = _webState->GetLastCommittedURL(); |
| 165 ios::ChromeBrowserState* browserState = | 166 ios::ChromeBrowserState* browserState = |
| 166 ios::ChromeBrowserState::FromBrowserState(_webState->GetBrowserState()); | 167 ios::ChromeBrowserState::FromBrowserState(_webState->GetBrowserState()); |
| 167 return base::MakeUnique<dom_distiller::DistillerViewer>( | 168 return base::MakeUnique<dom_distiller::DistillerViewer>( |
| 168 dom_distiller::DomDistillerServiceFactory::GetForBrowserState( | 169 dom_distiller::DomDistillerServiceFactory::GetForBrowserState( |
| 169 browserState), | 170 browserState), |
| 170 browserState->GetPrefs(), pageURL, | 171 browserState->GetPrefs(), pageURL, |
| 171 base::BindBlock(^( | 172 base::BindBlockArc(^( |
| 172 const GURL& pageURL, const std::string& html, | 173 const GURL& pageURL, const std::string& html, |
| 173 const std::vector<dom_distiller::DistillerViewer::ImageInfo>& images, | 174 const std::vector<dom_distiller::DistillerViewer::ImageInfo>& images, |
| 174 const std::string& title) { | 175 const std::string& title) { |
| 175 [weakSelf distillationFinished:html forURL:pageURL]; | 176 [weakSelf distillationFinished:html forURL:pageURL]; |
| 176 })); | 177 })); |
| 177 } | 178 } |
| 178 | 179 |
| 179 - (void)distillationFinished:(const std::string&)html forURL:(const GURL&)url { | 180 - (void)distillationFinished:(const std::string&)html forURL:(const GURL&)url { |
| 180 UIView* superview = [self.delegate superviewForReaderModePanel]; | 181 UIView* superview = [self.delegate superviewForReaderModePanel]; |
| 181 DCHECK(_viewer || [superview viewWithTag:kReaderModeViewTag]); | 182 DCHECK(_viewer || [superview viewWithTag:kReaderModeViewTag]); |
| 182 if ([superview viewWithTag:kReaderModeViewTag]) { | 183 if ([superview viewWithTag:kReaderModeViewTag]) { |
| 183 [self.delegate loadReaderModeHTML:base::SysUTF8ToNSString(html) forURL:url]; | 184 [self.delegate loadReaderModeHTML:base::SysUTF8ToNSString(html) forURL:url]; |
| 184 } else if (_viewer) { | 185 } else if (_viewer) { |
| 185 [self showInfoBar:html forURL:url]; | 186 [self showInfoBar:html forURL:url]; |
| 186 } | 187 } |
| 187 } | 188 } |
| 188 | 189 |
| 189 #pragma mark view creation | 190 #pragma mark view creation |
| 190 | 191 |
| 191 - (ReaderModeView*)readerModeViewWithFrame:(CGRect)frame { | 192 - (ReaderModeView*)readerModeViewWithFrame:(CGRect)frame { |
| 192 DCHECK(_checker->CanSwitchToReaderMode()); | 193 DCHECK(_checker->CanSwitchToReaderMode()); |
| 193 ReaderModeView* view = | 194 ReaderModeView* view = |
| 194 [[[ReaderModeView alloc] initWithFrame:frame delegate:self] autorelease]; | 195 [[ReaderModeView alloc] initWithFrame:frame delegate:self]; |
| 195 [view assignDistillerViewer:[self startDistillation]]; | 196 [view assignDistillerViewer:[self startDistillation]]; |
| 196 return view; | 197 return view; |
| 197 } | 198 } |
| 198 | 199 |
| 199 #pragma mark infobar. | 200 #pragma mark infobar. |
| 200 | 201 |
| 201 - (void)showInfoBar:(const std::string&)html forURL:(const GURL&)url { | 202 - (void)showInfoBar:(const std::string&)html forURL:(const GURL&)url { |
| 202 DCHECK(_webState); | 203 DCHECK(_webState); |
| 203 base::WeakNSProtocol<id<ReaderModeControllerDelegate>> weakDelegate( | 204 __weak id<ReaderModeControllerDelegate> weakDelegate = self.delegate; |
| 204 self.delegate); | |
| 205 | 205 |
| 206 // Non reference version of the variables needed. | 206 // Non reference version of the variables needed. |
| 207 const std::string html_non_ref(html); | 207 const std::string html_non_ref(html); |
| 208 const GURL url_non_ref(url); | 208 const GURL url_non_ref(url); |
| 209 auto infoBarDelegate = | 209 auto infoBarDelegate = |
| 210 base::MakeUnique<ReaderModeInfoBarDelegate>(base::BindBlock(^{ | 210 base::MakeUnique<ReaderModeInfoBarDelegate>(base::BindBlockArc(^{ |
| 211 [weakDelegate loadReaderModeHTML:base::SysUTF8ToNSString(html_non_ref) | 211 [weakDelegate loadReaderModeHTML:base::SysUTF8ToNSString(html_non_ref) |
| 212 forURL:url_non_ref]; | 212 forURL:url_non_ref]; |
| 213 })); | 213 })); |
| 214 | 214 |
| 215 infobars::InfoBarManager* infobar_manager = | 215 infobars::InfoBarManager* infobar_manager = |
| 216 InfoBarManagerImpl::FromWebState(_webState); | 216 InfoBarManagerImpl::FromWebState(_webState); |
| 217 | 217 |
| 218 std::unique_ptr<infobars::InfoBar> infobar = | 218 std::unique_ptr<infobars::InfoBar> infobar = |
| 219 infobar_manager->CreateConfirmInfoBar(std::move(infoBarDelegate)); | 219 infobar_manager->CreateConfirmInfoBar(std::move(infoBarDelegate)); |
| 220 if (infobar_) | 220 if (infobar_) |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 #pragma mark - InfoBarManagerObserverBridgeProtocol. | 300 #pragma mark - InfoBarManagerObserverBridgeProtocol. |
| 301 | 301 |
| 302 - (void)infoBarRemoved:(infobars::InfoBar*)infobar { | 302 - (void)infoBarRemoved:(infobars::InfoBar*)infobar { |
| 303 if (infobar == infobar_) { | 303 if (infobar == infobar_) { |
| 304 _viewer.reset(); | 304 _viewer.reset(); |
| 305 infobar_ = nullptr; | 305 infobar_ = nullptr; |
| 306 } | 306 } |
| 307 } | 307 } |
| 308 | 308 |
| 309 @end | 309 @end |
| OLD | NEW |