find_in_page_manager_impl.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2019 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. #ifndef IOS_WEB_FIND_IN_PAGE_FIND_IN_PAGE_MANAGER_IMPL_H_
  5. #define IOS_WEB_FIND_IN_PAGE_FIND_IN_PAGE_MANAGER_IMPL_H_
  6. #include <string>
  7. #include "base/memory/weak_ptr.h"
  8. #import "ios/web/find_in_page/find_in_page_request.h"
  9. #import "ios/web/public/find_in_page/find_in_page_manager.h"
  10. #include "ios/web/public/web_state_observer.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. @class NSString;
  13. namespace {
  14. // Find in Page UserAction keys.
  15. const char kFindActionName[] = "Find";
  16. const char kFindNextActionName[] = "FindNext";
  17. const char kFindPreviousActionName[] = "FindPrevious";
  18. } // namespace
  19. namespace web {
  20. class WebState;
  21. class WebFrame;
  22. class FindInPageManagerImpl : public FindInPageManager,
  23. public web::WebStateObserver {
  24. public:
  25. explicit FindInPageManagerImpl(web::WebState* web_state);
  26. ~FindInPageManagerImpl() override;
  27. static void CreateForWebState(WebState* web_state);
  28. // FindInPageManager overrides
  29. void Find(NSString* query, FindInPageOptions options) override;
  30. void StopFinding() override;
  31. bool CanSearchContent() override;
  32. FindInPageManagerDelegate* GetDelegate() override;
  33. void SetDelegate(FindInPageManagerDelegate* delegate) override;
  34. private:
  35. friend class web::WebStateUserData<FindInPageManagerImpl>;
  36. // Executes find logic for |FindInPageSearch| option.
  37. void StartSearch(NSString* query);
  38. // Executes find logic for |FindInPageNext| option.
  39. void SelectNextMatch();
  40. // Executes find logic for |FindInPagePrevious| option.
  41. void SelectPreviousMatch();
  42. // Determines whether find is finished. If not, calls pumpSearch to
  43. // continue. If it is, calls UpdateFrameMatchesCount(). If find returned
  44. // null, then does nothing more.
  45. void ProcessFindInPageResult(const std::string& frame_id,
  46. const int request_id,
  47. absl::optional<int> result);
  48. // Calls delegate DidHighlightMatches() method if |delegate_| is set and
  49. // starts a FindInPageNext find. Called when the last frame returns results
  50. // from a Find request.
  51. void LastFindRequestCompleted();
  52. // Calls delegate DidSelectMatch() method to pass back index selected if
  53. // |delegate_| is set. |result| is a byproduct of using base::BindOnce() to
  54. // call this method after making a web_frame->CallJavaScriptFunction() call.
  55. void SelectDidFinish(const base::Value* result);
  56. // Executes highlightResult() JavaScript function in frame which contains the
  57. // currently selected match.
  58. void SelectCurrentMatch();
  59. // WebStateObserver overrides
  60. void WebFrameDidBecomeAvailable(WebState* web_state,
  61. WebFrame* web_frame) override;
  62. void WebFrameWillBecomeUnavailable(WebState* web_state,
  63. WebFrame* web_frame) override;
  64. void WebStateDestroyed(WebState* web_state) override;
  65. protected:
  66. // Holds the state of the most recent find in page request.
  67. FindInPageRequest last_find_request_;
  68. FindInPageManagerDelegate* delegate_ = nullptr;
  69. web::WebState* web_state_ = nullptr;
  70. base::WeakPtrFactory<FindInPageManagerImpl> weak_factory_;
  71. };
  72. } // namespace web
  73. #endif // IOS_WEB_FIND_IN_PAGE_FIND_IN_PAGE_MANAGER_IMPL_H_