find_in_page_request.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_REQUEST_H_
  5. #define IOS_WEB_FIND_IN_PAGE_FIND_IN_PAGE_REQUEST_H_
  6. #include <list>
  7. #include <map>
  8. #include <string>
  9. #import <Foundation/Foundation.h>
  10. @class NSString;
  11. namespace web {
  12. class WebFrame;
  13. // Keeps track of the state of a FindInPageManager::Find() request.
  14. class FindInPageRequest {
  15. public:
  16. FindInPageRequest();
  17. ~FindInPageRequest();
  18. // Clears properties and sets new |query| and |pending_frame_call_count|.
  19. void Reset(NSString* query, int pending_frame_call_count);
  20. int GetTotalMatchCount() const;
  21. int GetRequestId() const;
  22. NSString* GetRequestQuery() const;
  23. // Sets |selected_frame_id| and |selected_match_index_in_selected_frame| to
  24. // the first match on the page. No-op if no known matches exist. Returns
  25. // true if selected a match, false otherwise.
  26. bool GoToFirstMatch();
  27. // Sets |selected_frame_id| and |selected_match_index_in_selected_frame| to
  28. // the next match on the page. No-op if no known matches exist. Returns true
  29. // if selected a match, false otherwise.
  30. bool GoToNextMatch();
  31. // Sets |selected_frame_id| and |selected_match_index_in_selected_frame| to
  32. // the previous match on the page. No-op if no known matches exist. Returns
  33. // true if selected a match, false otherwise.
  34. bool GoToPreviousMatch();
  35. // Returns the number of matches in |selected_frame_id|. If no match is
  36. // currently selected, then returns -1;
  37. int GetMatchCountForSelectedFrame();
  38. // Sets |match_count| for |selected_frame_id|, if one exists.
  39. void SetMatchCountForSelectedFrame(int match_count);
  40. // Returns the index of the currently selected match for all matches on the
  41. // page. If no match is selected, then returns -1.
  42. int GetCurrentSelectedMatchPageIndex();
  43. // Returns the id of the WebFrame containing the currently selected match.
  44. // Returns empty string if no currently selected match.
  45. std::string GetSelectedFrameId();
  46. // Returns the index of the currently selected match relative to the matches
  47. // within its frame. If no match is selected, then returns -1.
  48. int GetCurrentSelectedMatchFrameIndex() const;
  49. // Sets |index| as the currently selected index relative to the selected
  50. // frame.
  51. void SetCurrentSelectedMatchFrameIndex(int index);
  52. // Returns the number of matches in |frame_id|. If |frame_id| is invalid,
  53. // then returns -1.
  54. int GetMatchCountForFrame(const std::string& frame_id);
  55. // Sets |match_count| for |frame_id|.
  56. void SetMatchCountForFrame(int match_count, const std::string& frame_id);
  57. // Removes frame with Id |frame_id| from |frame_order| and
  58. // |frame_match_count|. Resets |selected_frame_id| and
  59. // |selected_match_index_in_selected_frame| if the frame with |frame_id|
  60. // contains the currently selected match.
  61. void RemoveFrame(const std::string& frame_id);
  62. // Adds new frame to |frame_order_| and |frame_match_count_|.
  63. void AddFrame(WebFrame* web_frame);
  64. // After each frame's Find request has finished, call this method to
  65. // decrement |pending_frame_counts| to indicate to the receiver of the
  66. // request completion.
  67. void DidReceiveFindResponseFromOneFrame();
  68. // Returns true if there are no more pending Find requests, false
  69. // otherwise.
  70. bool AreAllFindResponsesReturned();
  71. private:
  72. // Unique identifier for each find used to check that it is the most recent
  73. // find. This ensures that an old find doesn't decrement
  74. // |pending_frame_calls_count| after it has been reset by the new find.
  75. int unique_id_ = 0;
  76. // Query string of find request. NSString type to ensure query passed to
  77. // delegate methods is the same type as what is passed into Find().
  78. NSString* query_ = nil;
  79. // Counter to keep track of pending frame JavaScript calls.
  80. int pending_frame_call_count_ = 0;
  81. // Holds number of matches found for each frame keyed by frame_id.
  82. std::map<std::string, int> frame_match_count_;
  83. // List of frame_ids used for sorting matches.
  84. std::list<std::string> frame_order_;
  85. // Id of frame which has the currently selected match. Set to
  86. // frame_order.end() if there is no currently selected match. All matches
  87. // from the last find will be highlighted. However, the match at
  88. // |selected_match_index_in_selected_frame| will be highlighted in a
  89. // visually unique manner. This match is referred to as the "selected match"
  90. // and can be changed with the FindInPageNext and FindInPagePrevious
  91. // commands.
  92. std::list<std::string>::iterator selected_frame_id_ = frame_order_.end();
  93. // Index of the currently selected match or -1 if there is none.
  94. int selected_match_index_in_selected_frame_ = -1;
  95. // Returns true if |frame_id| contains the currently selected match, false
  96. // otherwise.
  97. bool IsSelectedFrame(const std::string& frame_id);
  98. };
  99. } // namespace web
  100. #endif // IOS_WEB_FIND_IN_PAGE_FIND_IN_PAGE_REQUEST_H_