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_HATS_HATS_WEB_DIALOG_H_
6 #define CHROME_BROWSER_UI_VIEWS_HATS_HATS_WEB_DIALOG_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/gtest_prod_util.h"
12 #include "base/macros.h"
13 #include "base/timer/timer.h"
14 #include "chrome/browser/profiles/profile_observer.h"
15 #include "ui/views/window/dialog_delegate.h"
16 #include "ui/web_dialogs/web_dialog_delegate.h"
17 
18 class Browser;
19 class Profile;
20 
21 namespace views {
22 class WebDialogView;
23 class Widget;
24 }
25 
26 // Happiness tracking survey dialog which shows the survey content.
27 // This class lives on the UI thread and is self deleting.
28 // TODO(weili): This dialog shares a lot of common code with the one in
29 // chrome/browser/chromeos/hats/, should be merged into one.
30 class HatsWebDialog : public ui::WebDialogDelegate,
31                       public views::DialogDelegateView,
32                       public ProfileObserver {
33  public:
34   // Create an instance of HatsWebDialog and load its content without showing.
35   static void Create(Browser* browser, const std::string& site_id);
36 
37  private:
38   FRIEND_TEST_ALL_PREFIXES(HatsWebDialogBrowserTest, Cookies);
39   friend class TestHatsWebDialog;
40   friend class HatsWebDialogBrowserTest;
41 
42   // Use Create() above. An off the record profile is created from the given
43   // browser profile which is used for navigating to the survey. |site_id| is
44   // used to select the survey.
45   HatsWebDialog(Browser* browser, const std::string& site_id);
46   ~HatsWebDialog() override;
47 
48   // ui::WebDialogDelegate implementation.
49   ui::ModalType GetDialogModalType() const override;
50   base::string16 GetDialogTitle() const override;
51   GURL GetDialogContentURL() const override;
52   void GetWebUIMessageHandlers(
53       std::vector<content::WebUIMessageHandler*>* handlers) const override;
54   void GetDialogSize(gfx::Size* size) const override;
55   std::string GetDialogArgs() const override;
56   void OnDialogClosed(const std::string& json_retval) override;
57   void OnCloseContents(content::WebContents* source,
58                        bool* out_close_dialog) override;
59   bool ShouldShowDialogTitle() const override;
60   bool HandleContextMenu(content::RenderFrameHost* render_frame_host,
61                          const content::ContextMenuParams& params) override;
62   void OnWebContentsFinishedLoad() override;
63   void OnMainFrameResourceLoadComplete(
64       const blink::mojom::ResourceLoadInfo& resource_load_info) override;
65   ui::WebDialogDelegate::FrameKind GetWebDialogFrameKind() const override;
66 
67   // views::DialogDelegateView implementation.
68   views::View* GetContentsView() override;
69   views::Widget* GetWidget() override;
70   const views::Widget* GetWidget() const override;
71   ui::ModalType GetModalType() const override;
72 
73   // These are virtual for tests.
74   virtual void OnLoadTimedOut();
75   virtual const base::TimeDelta ContentLoadingTimeout() const;
76 
otr_profile_for_testing()77   Profile* otr_profile_for_testing() { return otr_profile_; }
78 
79   void CreateWebDialog(Browser* browser);
80   // ProfileObserver:
81   void OnProfileWillBeDestroyed(Profile* profile) override;
82   void Show(views::Widget* widget, bool accept);
83 
84   Profile* otr_profile_;
85   Browser* browser_;
86   const std::string site_id_;
87 
88   // A timer to prevent unresponsive loading of survey dialog.
89   base::OneShotTimer loading_timer_;
90 
91   // The widget created for preloading. It is owned by us until it is shown to
92   // user.
93   views::Widget* preloading_widget_ = nullptr;
94   views::WebDialogView* webview_ = nullptr;
95 
96   // Indicate whether HaTS resources were loaded successfully.
97   bool resource_loaded_ = false;
98 
99   base::WeakPtrFactory<HatsWebDialog> weak_factory_{this};
100 
101   DISALLOW_COPY_AND_ASSIGN(HatsWebDialog);
102 };
103 
104 #endif  // CHROME_BROWSER_UI_VIEWS_HATS_HATS_WEB_DIALOG_H_
105