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 
5 #ifndef CHROME_BROWSER_UI_VIEWS_FRAME_TOP_CONTAINER_LOADING_BAR_H_
6 #define CHROME_BROWSER_UI_VIEWS_FRAME_TOP_CONTAINER_LOADING_BAR_H_
7 
8 #include "chrome/browser/ui/tabs/tab_network_state.h"
9 #include "content/public/browser/web_contents_observer.h"
10 #include "ui/gfx/animation/animation_delegate.h"
11 #include "ui/gfx/animation/linear_animation.h"
12 #include "ui/views/view.h"
13 
14 class Browser;
15 
16 class LoadingBarView : public views::View, public gfx::AnimationDelegate {
17  public:
18   LoadingBarView();
19   LoadingBarView(const LoadingBarView&) = delete;
20   LoadingBarView& operator=(const LoadingBarView&) = delete;
21 
22   void HideImmediately();
23   void Show(double loading_progress);
24   void FinishLoading();
25 
26   void SetLoadingProgress(double loading_progress);
27 
28  private:
29   double GetDisplayedLoadingProgress() const;
30 
31   // views::View:
32   void OnThemeChanged() override;
33   void AddedToWidget() override;
34   void OnPaint(gfx::Canvas* canvas) override;
35 
36   // gfx::AnimationDelegate:
37   void AnimationEnded(const gfx::Animation* animation) override;
38   void AnimationProgressed(const gfx::Animation* animation) override;
39 
40   gfx::LinearAnimation animation_{this};
41   bool is_shown_when_not_animating_ = false;
42   double start_loading_progress_ = 0.0;
43   double target_loading_progress_ = 0.0;
44 };
45 
46 class TopContainerLoadingBar : public LoadingBarView,
47                                public content::WebContentsObserver {
48  public:
49   explicit TopContainerLoadingBar(Browser*);
50   TopContainerLoadingBar(const TopContainerLoadingBar&) = delete;
51   TopContainerLoadingBar& operator=(const TopContainerLoadingBar&) = delete;
52 
53   void SetWebContents(content::WebContents* web_contents);
54 
55  private:
56   void UpdateLoadingProgress();
57   double GetLoadingProgress();
58 
59   // content::WebContentsObserver:
60   void LoadProgressChanged(double progress) override;
61 
62   Browser* browser_;
63   TabNetworkState network_state_ = TabNetworkState::kNone;
64 };
65 
66 #endif  // CHROME_BROWSER_UI_VIEWS_FRAME_TOP_CONTAINER_LOADING_BAR_H_
67