OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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/find_in_page/find_tab_helper.h" | 5 #import "ios/chrome/browser/find_in_page/find_tab_helper.h" |
6 | 6 |
7 #import "ios/chrome/browser/find_in_page/find_in_page_controller.h" | 7 #import "ios/chrome/browser/find_in_page/find_in_page_controller.h" |
| 8 #import "ios/chrome/browser/find_in_page/find_in_page_model.h" |
8 | 9 |
9 #if !defined(__has_feature) || !__has_feature(objc_arc) | 10 #if !defined(__has_feature) || !__has_feature(objc_arc) |
10 #error "This file requires ARC support." | 11 #error "This file requires ARC support." |
11 #endif | 12 #endif |
12 | 13 |
13 DEFINE_WEB_STATE_USER_DATA_KEY(FindTabHelper); | 14 DEFINE_WEB_STATE_USER_DATA_KEY(FindTabHelper); |
14 | 15 |
15 // static | 16 // static |
16 void FindTabHelper::CreateForWebState( | 17 void FindTabHelper::CreateForWebState( |
17 web::WebState* web_state, | 18 web::WebState* web_state, |
18 id<FindInPageControllerDelegate> controller_delegate) { | 19 id<FindInPageControllerDelegate> controller_delegate) { |
19 DCHECK(web_state); | 20 DCHECK(web_state); |
20 if (!FromWebState(web_state)) { | 21 if (!FromWebState(web_state)) { |
21 web_state->SetUserData(UserDataKey(), | 22 web_state->SetUserData(UserDataKey(), |
22 new FindTabHelper(web_state, controller_delegate)); | 23 new FindTabHelper(web_state, controller_delegate)); |
23 } | 24 } |
24 } | 25 } |
25 | 26 |
26 FindTabHelper::FindTabHelper( | 27 FindTabHelper::FindTabHelper( |
27 web::WebState* web_state, | 28 web::WebState* web_state, |
28 id<FindInPageControllerDelegate> controller_delegate) { | 29 id<FindInPageControllerDelegate> controller_delegate) |
29 controller_ = | 30 : web::WebStateObserver(web_state) { |
30 [[FindInPageController alloc] initWithWebState:web_state | 31 controller_.reset([[FindInPageController alloc] |
31 delegate:controller_delegate]; | 32 initWithWebState:web_state |
| 33 delegate:controller_delegate]); |
32 } | 34 } |
33 | 35 |
34 FindTabHelper::~FindTabHelper() {} | 36 FindTabHelper::~FindTabHelper() {} |
35 | 37 |
36 FindInPageController* FindTabHelper::GetController() { | 38 void FindTabHelper::StartFinding(NSString* search_term, |
37 return controller_; | 39 FindInPageCompletionBlock completion) { |
| 40 [controller_ findStringInPage:search_term |
| 41 completionHandler:^{ |
| 42 FindInPageModel* model = controller_.get().findInPageModel; |
| 43 completion(model); |
| 44 }]; |
| 45 } |
| 46 |
| 47 void FindTabHelper::ContinueFinding(FindDirection direction, |
| 48 FindInPageCompletionBlock completion) { |
| 49 FindInPageModel* model = controller_.get().findInPageModel; |
| 50 |
| 51 if (direction == FORWARD) { |
| 52 [controller_ findNextStringInPageWithCompletionHandler:^{ |
| 53 completion(model); |
| 54 }]; |
| 55 |
| 56 } else if (direction == REVERSE) { |
| 57 [controller_ findPreviousStringInPageWithCompletionHandler:^{ |
| 58 completion(model); |
| 59 }]; |
| 60 |
| 61 } else { |
| 62 NOTREACHED(); |
| 63 } |
| 64 } |
| 65 |
| 66 void FindTabHelper::StopFinding(ProceduralBlock completion) { |
| 67 [controller_ disableFindInPageWithCompletionHandler:completion]; |
| 68 SetFindUIActive(false); |
| 69 } |
| 70 |
| 71 FindInPageModel* FindTabHelper::GetFindResult() const { |
| 72 return controller_.get().findInPageModel; |
| 73 } |
| 74 |
| 75 bool FindTabHelper::CurrentPageSupportsFindInPage() const { |
| 76 return [controller_ canFindInPage]; |
| 77 } |
| 78 |
| 79 bool FindTabHelper::IsFindUIActive() const { |
| 80 return controller_.get().findInPageModel.enabled; |
| 81 } |
| 82 |
| 83 void FindTabHelper::SetFindUIActive(bool active) { |
| 84 controller_.get().findInPageModel.enabled = active; |
| 85 } |
| 86 |
| 87 void FindTabHelper::PersistSearchTerm() { |
| 88 [controller_ saveSearchTerm]; |
| 89 } |
| 90 |
| 91 void FindTabHelper::RestoreSearchTerm() { |
| 92 [controller_ restoreSearchTerm]; |
38 } | 93 } |
39 | 94 |
40 void FindTabHelper::NavigationItemCommitted( | 95 void FindTabHelper::NavigationItemCommitted( |
41 const web::LoadCommittedDetails& load_details) { | 96 const web::LoadCommittedDetails& load_details) { |
42 [controller_ disableFindInPageWithCompletionHandler:nil]; | 97 StopFinding(nil); |
43 } | 98 } |
44 | 99 |
45 void FindTabHelper::WebStateDestroyed() { | 100 void FindTabHelper::WebStateDestroyed() { |
46 [controller_ detachFromWebState]; | 101 [controller_ detachFromWebState]; |
47 } | 102 } |
OLD | NEW |