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 #ifndef IOS_CHROME_BROWSER_FIND_IN_PAGE_FIND_TAB_HELPER_H_
6 #define IOS_CHROME_BROWSER_FIND_IN_PAGE_FIND_TAB_HELPER_H_
7 
8 #include <Foundation/Foundation.h>
9 
10 #include "base/ios/block_types.h"
11 #include "base/macros.h"
12 #include "ios/web/public/web_state_observer.h"
13 #import "ios/web/public/web_state_user_data.h"
14 
15 @class FindInPageController;
16 @class FindInPageModel;
17 @protocol FindInPageResponseDelegate;
18 
19 // Adds support for the "Find in page" feature.
20 class FindTabHelper : public web::WebStateObserver,
21                       public web::WebStateUserData<FindTabHelper> {
22  public:
23   ~FindTabHelper() override;
24 
25   enum FindDirection {
26     FORWARD,
27     REVERSE,
28   };
29 
30   // Sets the FindInPageResponseDelegate delegate to send responses to
31   // StartFinding(), ContinueFinding(), and StopFinding().
32   void SetResponseDelegate(id<FindInPageResponseDelegate> response_delegate);
33 
34   // Starts an asynchronous Find operation that will call the given completion
35   // handler with results.  Highlights matches on the current page.  Always
36   // searches in the FORWARD direction.
37   void StartFinding(NSString* search_string);
38 
39   // Runs an asynchronous Find operation that will call the given completion
40   // handler with results.  Highlights matches on the current page.  Uses the
41   // previously remembered search string and searches in the given |direction|.
42   void ContinueFinding(FindDirection direction);
43 
44   // Stops any running find operations and runs the given completion block.
45   // Removes any highlighting from the current page.
46   void StopFinding();
47 
48   // Returns the FindInPageModel that contains the latest find results.
49   FindInPageModel* GetFindResult() const;
50 
51   // Returns true if the currently loaded page supports Find in Page.
52   bool CurrentPageSupportsFindInPage() const;
53 
54   // Returns true if the Find in Page UI is currently visible.
55   bool IsFindUIActive() const;
56 
57   // Marks the Find in Page UI as visible or not.  This method does not directly
58   // show or hide the UI.  It simply acts as a marker for whether or not the UI
59   // is visible.
60   void SetFindUIActive(bool active);
61 
62   // Saves the current find text to persistent storage.
63   void PersistSearchTerm();
64 
65   // Restores the current find text from persistent storage.
66   void RestoreSearchTerm();
67 
68  private:
69   friend class FindTabHelperTest;
70   friend class web::WebStateUserData<FindTabHelper>;
71 
72   // Private constructor used by CreateForWebState().
73   FindTabHelper(web::WebState* web_state);
74 
75   // web::WebStateObserver.
76   void WebStateDestroyed(web::WebState* web_state) override;
77   void DidFinishNavigation(web::WebState* web_state,
78                            web::NavigationContext* navigation_context) override;
79 
80   // The ObjC find in page controller.
81   FindInPageController* controller_;
82 
83   WEB_STATE_USER_DATA_KEY_DECL();
84 
85   DISALLOW_COPY_AND_ASSIGN(FindTabHelper);
86 };
87 
88 #endif  // IOS_CHROME_BROWSER_FIND_IN_PAGE_FIND_TAB_HELPER_H_
89