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/find_in_page/find_tab_helper.h"
6
7#include "base/memory/ptr_util.h"
8#include "base/metrics/user_metrics.h"
9#include "base/metrics/user_metrics_action.h"
10#import "ios/chrome/browser/find_in_page/find_in_page_controller.h"
11#import "ios/chrome/browser/find_in_page/find_in_page_model.h"
12
13#if !defined(__has_feature) || !__has_feature(objc_arc)
14#error "This file requires ARC support."
15#endif
16
17FindTabHelper::FindTabHelper(web::WebState* web_state) {
18  web_state->AddObserver(this);
19  controller_ = [[FindInPageController alloc] initWithWebState:web_state];
20}
21
22FindTabHelper::~FindTabHelper() {}
23
24void FindTabHelper::SetResponseDelegate(
25    id<FindInPageResponseDelegate> response_delegate) {
26  controller_.responseDelegate = response_delegate;
27}
28
29void FindTabHelper::StartFinding(NSString* search_term) {
30  [controller_ findStringInPage:search_term];
31}
32
33void FindTabHelper::ContinueFinding(FindDirection direction) {
34  if (direction == FORWARD) {
35    [controller_ findNextStringInPage];
36
37  } else if (direction == REVERSE) {
38    [controller_ findPreviousStringInPage];
39
40  } else {
41    NOTREACHED();
42  }
43}
44
45void FindTabHelper::StopFinding() {
46  SetFindUIActive(false);
47  [controller_ disableFindInPage];
48}
49
50FindInPageModel* FindTabHelper::GetFindResult() const {
51  return controller_.findInPageModel;
52}
53
54bool FindTabHelper::CurrentPageSupportsFindInPage() const {
55  return [controller_ canFindInPage];
56}
57
58bool FindTabHelper::IsFindUIActive() const {
59  return controller_.findInPageModel.enabled;
60}
61
62void FindTabHelper::SetFindUIActive(bool active) {
63  controller_.findInPageModel.enabled = active;
64}
65
66void FindTabHelper::PersistSearchTerm() {
67  [controller_ saveSearchTerm];
68}
69
70void FindTabHelper::RestoreSearchTerm() {
71  [controller_ restoreSearchTerm];
72}
73
74void FindTabHelper::WebStateDestroyed(web::WebState* web_state) {
75  [controller_ detachFromWebState];
76  web_state->RemoveObserver(this);
77}
78
79void FindTabHelper::DidFinishNavigation(
80    web::WebState* web_state,
81    web::NavigationContext* navigation_context) {
82  if (IsFindUIActive()) {
83    StopFinding();
84  }
85}
86
87WEB_STATE_USER_DATA_KEY_IMPL(FindTabHelper)
88