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 
5 #ifndef CHROME_BROWSER_UI_SAD_TAB_HELPER_H_
6 #define CHROME_BROWSER_UI_SAD_TAB_HELPER_H_
7 
8 #include <memory>
9 
10 #include "base/compiler_specific.h"
11 #include "base/macros.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "content/public/browser/web_contents_user_data.h"
14 
15 class SadTab;
16 
17 // Per-tab class to manage sad tab views. The sad tab view appears when the main
18 // frame of a WebContents has crashed. The behaviour depends on whether
19 // content::ShouldSkipEarlyCommitPendingForCrashedFrame is true or not.
20 //
21 // TODO(https://crbug.com/1072817): The early commit path is being removed, tidy
22 // these docs when that happens.
23 //
24 // If we are doing the early commit then the sad tab is removed when
25 // WebContentsObserver::RenderViewReady is signalled and does not come back
26 // unless the new frame also crashes.
27 //
28 // If we are not doing the early commit then the sad tab is removed when the new
29 // frame is created but the new frame is left invisible, this leaves the empty
30 // WebContents displaying. If the new frame commits, it becomes visible. If the
31 // commit is aborted, we reinstate the sad tab.
32 //
33 class SadTabHelper : public content::WebContentsObserver,
34                      public content::WebContentsUserData<SadTabHelper> {
35  public:
36   ~SadTabHelper() override;
37 
sad_tab()38   SadTab* sad_tab() { return sad_tab_.get(); }
39 
40   // Called when the sad tab needs to be reinstalled in the WebView,
41   // for example because a tab was activated, or because a tab was
42   // dragged to a new browser window.
43   void ReinstallInWebView();
44 
45  private:
46   friend class content::WebContentsUserData<SadTabHelper>;
47 
48   explicit SadTabHelper(content::WebContents* web_contents);
49 
50   void InstallSadTab(base::TerminationStatus status);
51 
52   // Overridden from content::WebContentsObserver:
53   void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
54   void RenderProcessGone(base::TerminationStatus status) override;
55   void RenderViewReady() override;
56   void DidFinishNavigation(
57       content::NavigationHandle* navigation_handle) override;
58 
59   std::unique_ptr<SadTab> sad_tab_;
60 
61   WEB_CONTENTS_USER_DATA_KEY_DECL();
62 
63   DISALLOW_COPY_AND_ASSIGN(SadTabHelper);
64 };
65 
66 #endif  // CHROME_BROWSER_UI_SAD_TAB_HELPER_H_
67