find_in_page_java_script_feature.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2021 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_JAVA_SCRIPT_FEATURE_H_
  5. #define IOS_WEB_FIND_IN_PAGE_FIND_IN_PAGE_JAVA_SCRIPT_FEATURE_H_
  6. #include "base/callback.h"
  7. #include "base/no_destructor.h"
  8. #include "base/values.h"
  9. #include "ios/web/public/js_messaging/java_script_feature.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace web {
  12. class WebFrame;
  13. namespace find_in_page {
  14. // Value returned when a |Search| or |Pump| call times out.
  15. extern const int kFindInPagePending;
  16. } // namespace find_in_page
  17. // A feature which searches for, highlights and allows navigating through text
  18. // search results.
  19. class FindInPageJavaScriptFeature : public JavaScriptFeature {
  20. public:
  21. // This feature holds no state, so only a single static instance is ever
  22. // needed.
  23. static FindInPageJavaScriptFeature* GetInstance();
  24. // Searches for string |query| in |frame|. |callback| returns the number of
  25. // search results found or |kFindInPagePending| if a call to |Pump| is
  26. // necessary before match count is available.
  27. bool Search(WebFrame* frame,
  28. const std::string& query,
  29. base::OnceCallback<void(absl::optional<int>)> callback);
  30. // Continues an ongoing search started with |Search| which hasn't yet
  31. // completed. |callback| returns the number of search results found or
  32. // |kFindInPagePending| if more calls to |Pump| are necessary before match
  33. // count is available.
  34. void Pump(WebFrame* frame,
  35. base::OnceCallback<void(absl::optional<int>)> callback);
  36. // Selects the given match at |index| in |frame|. |callback| is called with
  37. // the dictionary value results from the selection.
  38. void SelectMatch(WebFrame* frame,
  39. int index,
  40. base::OnceCallback<void(const base::Value*)> callback);
  41. // Stops any current search and clears highlighted/selected state from the
  42. // page.
  43. void Stop(WebFrame* frame);
  44. private:
  45. friend class base::NoDestructor<FindInPageJavaScriptFeature>;
  46. // Processes the JavaScript |result| to extract the match count and send it
  47. // to |callback|.
  48. void ProcessSearchResult(
  49. base::OnceCallback<void(const absl::optional<int>)> callback,
  50. const base::Value* result);
  51. FindInPageJavaScriptFeature();
  52. ~FindInPageJavaScriptFeature() override;
  53. FindInPageJavaScriptFeature(const FindInPageJavaScriptFeature&) = delete;
  54. FindInPageJavaScriptFeature& operator=(const FindInPageJavaScriptFeature&) =
  55. delete;
  56. };
  57. } // namespace web
  58. #endif // IOS_WEB_FIND_IN_PAGE_FIND_IN_PAGE_JAVA_SCRIPT_FEATURE_H_