Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / printing / print_preview_dialog_controller.h
blob792b3fe20ee2cbf10b4840b9866e5fb48be85807
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.
5 #ifndef CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DIALOG_CONTROLLER_H_
6 #define CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DIALOG_CONTROLLER_H_
8 #include <map>
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "components/sessions/session_id.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
17 class GURL;
19 namespace content {
20 struct LoadCommittedDetails;
21 class RenderProcessHost;
22 class WebContents;
25 namespace printing {
27 // For print preview, the WebContents that initiates the printing operation is
28 // the initiator, and the constrained dialog that shows the print preview is the
29 // print preview dialog.
30 // This class manages print preview dialog creation and destruction, and keeps
31 // track of the 1:1 relationship between initiator tabs and print preview
32 // dialogs.
33 class PrintPreviewDialogController
34 : public base::RefCounted<PrintPreviewDialogController>,
35 public content::NotificationObserver {
36 public:
37 PrintPreviewDialogController();
39 static PrintPreviewDialogController* GetInstance();
41 // Initiate print preview for |initiator|.
42 // Call this instead of GetOrCreatePreviewDialog().
43 static void PrintPreview(content::WebContents* initiator);
45 // Get/Create the print preview dialog for |initiator|.
46 // Exposed for unit tests.
47 content::WebContents* GetOrCreatePreviewDialog(
48 content::WebContents* initiator);
50 // Returns the preview dialog for |contents|.
51 // Returns |contents| if |contents| is a preview dialog.
52 // Returns NULL if no preview dialog exists for |contents|.
53 content::WebContents* GetPrintPreviewForContents(
54 content::WebContents* contents) const;
56 // Returns the initiator for |preview_dialog|.
57 // Returns NULL if no initiator exists for |preview_dialog|.
58 content::WebContents* GetInitiator(content::WebContents* preview_dialog);
60 // Run |callback| on the dialog of each active print preview operation.
61 void ForEachPreviewDialog(
62 base::Callback<void(content::WebContents*)> callback);
64 // content::NotificationObserver implementation.
65 void Observe(int type,
66 const content::NotificationSource& source,
67 const content::NotificationDetails& details) override;
69 // Returns true if |contents| is a print preview dialog.
70 static bool IsPrintPreviewDialog(content::WebContents* contents);
72 // Returns true if |url| is a print preview url.
73 static bool IsPrintPreviewURL(const GURL& url);
75 // Erase the initiator info associated with |preview_dialog|.
76 void EraseInitiatorInfo(content::WebContents* preview_dialog);
78 bool is_creating_print_preview_dialog() const {
79 return is_creating_print_preview_dialog_;
82 void AddProxyDialogForWebContents(content::WebContents* source,
83 content::WebContents* target);
85 void RemoveProxyDialogForWebContents(content::WebContents* source);
87 private:
88 friend class base::RefCounted<PrintPreviewDialogController>;
90 // 1:1 relationship between a print preview dialog and its initiator tab.
91 // Key: Print preview dialog.
92 // Value: Initiator.
93 using PrintPreviewDialogMap =
94 std::map<content::WebContents*, content::WebContents*>;
96 ~PrintPreviewDialogController() override;
98 // Handler for the RENDERER_PROCESS_CLOSED notification. This is observed when
99 // the initiator renderer crashed.
100 void OnRendererProcessClosed(content::RenderProcessHost* rph);
102 // Handler for the WEB_CONTENTS_DESTROYED notification. This is observed when
103 // either WebContents is closed.
104 void OnWebContentsDestroyed(content::WebContents* contents);
106 // Handler for the NAV_ENTRY_COMMITTED notification. This is observed when the
107 // renderer is navigated to a different page.
108 void OnNavEntryCommitted(content::WebContents* contents,
109 content::LoadCommittedDetails* details);
111 // Creates a new print preview dialog.
112 content::WebContents* CreatePrintPreviewDialog(
113 content::WebContents* initiator);
115 // Helper function to store the title of the initiator associated with
116 // |preview_dialog| in |preview_dialog|'s PrintPreviewUI.
117 void SaveInitiatorTitle(content::WebContents* preview_dialog);
119 // Adds/Removes observers for notifications from |contents|.
120 void AddObservers(content::WebContents* contents);
121 void RemoveObservers(content::WebContents* contents);
123 // Removes WebContents when they close/crash/navigate.
124 void RemoveInitiator(content::WebContents* initiator);
125 void RemovePreviewDialog(content::WebContents* preview_dialog);
127 // Mapping between print preview dialog and the corresponding initiator.
128 PrintPreviewDialogMap preview_dialog_map_;
130 PrintPreviewDialogMap proxied_dialog_map_;
132 // A registrar for listening to notifications.
133 content::NotificationRegistrar registrar_;
135 // True if the controller is waiting for a new preview dialog via
136 // content::NAVIGATION_TYPE_NEW_PAGE.
137 bool waiting_for_new_preview_page_;
139 // Whether the PrintPreviewDialogController is in the middle of creating a
140 // print preview dialog.
141 bool is_creating_print_preview_dialog_;
143 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogController);
146 } // namespace printing
148 #endif // CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DIALOG_CONTROLLER_H_