intercept_navigation_throttle.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2015 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_THROTTLE_H_
  5. #define COMPONENTS_NAVIGATION_INTERCEPTION_INTERCEPT_NAVIGATION_THROTTLE_H_
  6. #include "base/callback.h"
  7. #include "base/feature_list.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "content/public/browser/navigation_throttle.h"
  12. namespace content {
  13. class NavigationHandle;
  14. }
  15. namespace navigation_interception {
  16. enum class SynchronyMode {
  17. // Support async interception in some cases (See ShouldCheckAsynchronously).
  18. kAsync,
  19. // Only support synchronous interception.
  20. kSync
  21. };
  22. // This class allows the provider of the Callback to selectively ignore top
  23. // level navigations. This is a UI thread class.
  24. class InterceptNavigationThrottle : public content::NavigationThrottle {
  25. public:
  26. typedef base::RepeatingCallback<bool(
  27. content::NavigationHandle* /* navigation_handle */)>
  28. CheckCallback;
  29. static const base::Feature kAsyncCheck;
  30. InterceptNavigationThrottle(content::NavigationHandle* navigation_handle,
  31. CheckCallback should_ignore_callback,
  32. SynchronyMode async_mode);
  33. InterceptNavigationThrottle(const InterceptNavigationThrottle&) = delete;
  34. InterceptNavigationThrottle& operator=(const InterceptNavigationThrottle&) =
  35. delete;
  36. ~InterceptNavigationThrottle() override;
  37. // content::NavigationThrottle implementation:
  38. ThrottleCheckResult WillStartRequest() override;
  39. ThrottleCheckResult WillRedirectRequest() override;
  40. ThrottleCheckResult WillProcessResponse() override;
  41. const char* GetNameForLogging() override;
  42. private:
  43. ThrottleCheckResult CheckIfShouldIgnoreNavigation();
  44. void RunCheckAsync();
  45. bool ShouldCheckAsynchronously() const;
  46. // This callback should be called at the start of navigation and every
  47. // redirect, until |should_ignore_| is true.
  48. // Note: the callback can delete |this|.
  49. CheckCallback should_ignore_callback_;
  50. // Note that the CheckCallback currently has thread affinity on the Java side.
  51. scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
  52. const SynchronyMode mode_ = SynchronyMode::kSync;
  53. // The remaining members are only set for asynchronous checking.
  54. //
  55. // How many outbound pending checks are running. Normally this will be either
  56. // 0 or 1, but making this a bool makes too many assumptions about the nature
  57. // of Chrome's task queues (e.g. we could be scheduled after the task which
  58. // redirects the navigation).
  59. int pending_checks_ = 0;
  60. // Whether the navigation should be ignored. Updated at every redirect.
  61. bool should_ignore_ = false;
  62. // Whether the navigation is currently deferred.
  63. bool deferring_ = false;
  64. base::WeakPtrFactory<InterceptNavigationThrottle> weak_factory_{this};
  65. };
  66. } // namespace navigation_interception
  67. #endif // COMPONENTS_NAVIGATION_INTERCEPTION_INTERCEPT_NAVIGATION_THROTTLE_H_