intercept_navigation_delegate.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (c) 2012 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 COMPONENTS_NAVIGATION_INTERCEPTION_INTERCEPT_NAVIGATION_DELEGATE_H_
  5. #define COMPONENTS_NAVIGATION_INTERCEPTION_INTERCEPT_NAVIGATION_DELEGATE_H_
  6. #include <memory>
  7. #include "base/android/jni_weak_ref.h"
  8. #include "base/supports_user_data.h"
  9. #include "components/navigation_interception/intercept_navigation_throttle.h"
  10. #include "ui/base/page_transition_types.h"
  11. namespace content {
  12. class NavigationHandle;
  13. class NavigationThrottle;
  14. class WebContents;
  15. }
  16. namespace url {
  17. class Origin;
  18. }
  19. class GURL;
  20. namespace navigation_interception {
  21. // Native side of the InterceptNavigationDelegate Java interface.
  22. // This is used to create a InterceptNavigationResourceThrottle that calls the
  23. // Java interface method to determine whether a navigation should be ignored or
  24. // not.
  25. // To us this class:
  26. // 1) the Java-side interface implementation must be associated (via the
  27. // Associate method) with a WebContents for which URLRequests are to be
  28. // intercepted,
  29. // 2) the NavigationThrottle obtained via MaybeCreateThrottleFor must be
  30. // associated with the NavigationHandle in the ContentBrowserClient
  31. // implementation.
  32. class InterceptNavigationDelegate : public base::SupportsUserData::Data {
  33. public:
  34. // Pass true for |escape_external_handler_value| to have
  35. // base::EscapeExternalHandlerValue() invoked on URLs passed to
  36. // ShouldIgnoreNavigation() before the navigation is processed.
  37. InterceptNavigationDelegate(JNIEnv* env,
  38. jobject jdelegate,
  39. bool escape_external_handler_value = false);
  40. InterceptNavigationDelegate(const InterceptNavigationDelegate&) = delete;
  41. InterceptNavigationDelegate& operator=(const InterceptNavigationDelegate&) =
  42. delete;
  43. ~InterceptNavigationDelegate() override;
  44. // Associates the InterceptNavigationDelegate with a WebContents using the
  45. // SupportsUserData mechanism.
  46. // As implied by the use of scoped_ptr, the WebContents will assume ownership
  47. // of |delegate|.
  48. static void Associate(content::WebContents* web_contents,
  49. std::unique_ptr<InterceptNavigationDelegate> delegate);
  50. // Gets the InterceptNavigationDelegate associated with the WebContents,
  51. // can be null.
  52. static InterceptNavigationDelegate* Get(content::WebContents* web_contents);
  53. // Creates a InterceptNavigationThrottle that will direct all callbacks to
  54. // the InterceptNavigationDelegate.
  55. static std::unique_ptr<content::NavigationThrottle> MaybeCreateThrottleFor(
  56. content::NavigationHandle* handle,
  57. navigation_interception::SynchronyMode mode);
  58. bool ShouldIgnoreNavigation(content::NavigationHandle* navigation_handle);
  59. void HandleExternalProtocolDialog(
  60. const GURL& url,
  61. ui::PageTransition page_transition,
  62. bool has_user_gesture,
  63. const absl::optional<url::Origin>& initiating_origin);
  64. // To be called when a main frame requests a resource with a user gesture (eg.
  65. // xrh, fetch, etc.)
  66. void OnResourceRequestWithGesture();
  67. private:
  68. JavaObjectWeakGlobalRef weak_jdelegate_;
  69. bool escape_external_handler_value_ = false;
  70. };
  71. } // namespace navigation_interception
  72. #endif // COMPONENTS_NAVIGATION_INTERCEPTION_INTERCEPT_NAVIGATION_DELEGATE_H_