browser_controls_navigation_state_handler.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2020 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 WEBLAYER_BROWSER_BROWSER_CONTROLS_NAVIGATION_STATE_HANDLER_H_
  5. #define WEBLAYER_BROWSER_BROWSER_CONTROLS_NAVIGATION_STATE_HANDLER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/timer/timer.h"
  8. #include "cc/input/browser_controls_state.h"
  9. #include "content/public/browser/web_contents_observer.h"
  10. namespace weblayer {
  11. class BrowserControlsNavigationStateHandlerDelegate;
  12. // BrowserControlsNavigationStateHandler is responsible for the tracking the
  13. // value of cc::BrowserControlsState as related to navigation state and
  14. // notifying the delegate when the state changes.
  15. //
  16. // This class is roughly a combination of TopControlsSliderControllerChromeOS
  17. // and TabStateBrowserControlsVisibilityDelegate.
  18. class BrowserControlsNavigationStateHandler
  19. : public content::WebContentsObserver {
  20. public:
  21. BrowserControlsNavigationStateHandler(
  22. content::WebContents* web_contents,
  23. BrowserControlsNavigationStateHandlerDelegate* delegate);
  24. BrowserControlsNavigationStateHandler(
  25. const BrowserControlsNavigationStateHandler&) = delete;
  26. BrowserControlsNavigationStateHandler& operator=(
  27. const BrowserControlsNavigationStateHandler&) = delete;
  28. ~BrowserControlsNavigationStateHandler() override;
  29. // Returns true if the renderer is responsible for controlling the offsets.
  30. // This is normally true, but if the renderer is unresponsive (hung and/or
  31. // crashed), then the renderer won't be able to drive the offsets.
  32. bool IsRendererControllingOffsets();
  33. // content::WebContentsObserver:
  34. void DidStartNavigation(
  35. content::NavigationHandle* navigation_handle) override;
  36. void DidFinishNavigation(
  37. content::NavigationHandle* navigation_handle) override;
  38. void DidFinishLoad(content::RenderFrameHost* render_frame_host,
  39. const GURL& validated_url) override;
  40. void DidFailLoad(content::RenderFrameHost* render_frame_host,
  41. const GURL& validated_url,
  42. int error_code) override;
  43. void DidChangeVisibleSecurityState() override;
  44. void PrimaryMainFrameRenderProcessGone(
  45. base::TerminationStatus status) override;
  46. void OnRendererUnresponsive(
  47. content::RenderProcessHost* render_process_host) override;
  48. void OnRendererResponsive(
  49. content::RenderProcessHost* render_process_host) override;
  50. private:
  51. // Sets the value of |force_show_during_load_|. Calls to UpdateState() if
  52. // the value changed.
  53. void SetForceShowDuringLoad(bool value);
  54. // Schedules a timer to set |force_show_during_load_| to false.
  55. void ScheduleStopDelayedForceShow();
  56. // Checks the current state, and if it has changed notifies the delegate.
  57. void UpdateState();
  58. // Calculates whether the renderer is available to control the browser
  59. // controls.
  60. cc::BrowserControlsState CalculateStateForReasonRendererAvailability();
  61. // Calculates the value of the ControlsVisibilityReason::kOther state.
  62. cc::BrowserControlsState CalculateStateForReasonOther();
  63. bool IsRendererHungOrCrashed();
  64. raw_ptr<BrowserControlsNavigationStateHandlerDelegate> delegate_;
  65. // The controls are forced visible when a navigation starts, and allowed to
  66. // hide a short amount of time after done.
  67. bool force_show_during_load_ = false;
  68. // Timer used to set |force_show_during_load_| to false.
  69. base::OneShotTimer forced_show_during_load_timer_;
  70. // Last values supplied to the delegate.
  71. cc::BrowserControlsState last_renderer_availability_state_ =
  72. cc::BrowserControlsState::kBoth;
  73. cc::BrowserControlsState last_other_state_ = cc::BrowserControlsState::kBoth;
  74. // This is cached as WebContents::IsCrashed() does not always return the
  75. // right thing.
  76. bool is_crashed_ = false;
  77. };
  78. } // namespace weblayer
  79. #endif // WEBLAYER_BROWSER_BROWSER_CONTROLS_NAVIGATION_STATE_HANDLER_H_