blob: da5d3851d446d0be7ecc1d1a264fa0b295c006fa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DIALOG_CONTROLLER_H_
#define CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DIALOG_CONTROLLER_H_
#include <map>
#include "base/memory/ref_counted.h"
#include "chrome/browser/sessions/session_id.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
class GURL;
namespace content {
struct LoadCommittedDetails;
class RenderProcessHost;
class WebContents;
}
namespace printing {
// For print preview, the WebContents that initiates the printing operation is
// the initiator, and the constrained dialog that shows the print preview is the
// print preview dialog.
// This class manages print preview dialog creation and destruction, and keeps
// track of the 1:1 relationship between initiatora tabs and print preview
// dialogs.
class PrintPreviewDialogController
: public base::RefCounted<PrintPreviewDialogController>,
public content::NotificationObserver {
public:
PrintPreviewDialogController();
static PrintPreviewDialogController* GetInstance();
// Initiate print preview for |initiator|.
// Call this instead of GetOrCreatePreviewDialog().
static void PrintPreview(content::WebContents* initiator);
// Get/Create the print preview dialog for |initiator|.
// Exposed for unit tests.
content::WebContents* GetOrCreatePreviewDialog(
content::WebContents* initiator);
// Returns the preview dialog for |contents|.
// Returns |contents| if |contents| is a preview dialog.
// Returns NULL if no preview dialog exists for |contents|.
content::WebContents* GetPrintPreviewForContents(
content::WebContents* contents) const;
// Returns the initiator for |preview_dialog|.
// Returns NULL if no initiator exists for |preview_dialog|.
content::WebContents* GetInitiator(content::WebContents* preview_dialog);
// content::NotificationObserver implementation.
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
// Returns true if |contents| is a print preview dialog.
static bool IsPrintPreviewDialog(content::WebContents* contents);
// Returns true if |url| is a print preview url.
static bool IsPrintPreviewURL(const GURL& url);
// Erase the initiator info associated with |preview_dialog|.
void EraseInitiatorInfo(content::WebContents* preview_dialog);
bool is_creating_print_preview_dialog() const {
return is_creating_print_preview_dialog_;
}
private:
friend class base::RefCounted<PrintPreviewDialogController>;
// Used to distinguish between the two varieties of WebContents dealt with by
// this class.
enum ContentsType {
INITIATOR,
PREVIEW_DIALOG
};
// 1:1 relationship between a print preview dialog and its initiator.
// Key: Print preview dialog.
// Value: Initiator.
typedef std::map<content::WebContents*, content::WebContents*>
PrintPreviewDialogMap;
virtual ~PrintPreviewDialogController();
// Handler for the RENDERER_PROCESS_CLOSED notification. This is observed when
// the initiator renderer crashed.
void OnRendererProcessClosed(content::RenderProcessHost* rph);
// Handler for the WEB_CONTENTS_DESTROYED notification. This is observed when
// either WebContents is closed.
void OnWebContentsDestroyed(content::WebContents* contents);
// Handler for the NAV_ENTRY_COMMITTED notification. This is observed when the
// renderer is navigated to a different page.
void OnNavEntryCommitted(content::WebContents* contents,
content::LoadCommittedDetails* details);
// Creates a new print preview dialog.
content::WebContents* CreatePrintPreviewDialog(
content::WebContents* initiator);
// Helper function to store the title of the initiator associated with
// |preview_dialog| in |preview_dialog|'s PrintPreviewUI.
void SaveInitiatorTitle(content::WebContents* preview_dialog);
// Adds/Removes observers for notifications from |contents|.
void AddObservers(content::WebContents* contents, ContentsType contents_type);
void RemoveObservers(content::WebContents* contents,
ContentsType contents_type);
// Removes WebContents when they close/crash/navigate.
void RemoveInitiator(content::WebContents* initiator);
void RemovePreviewDialog(content::WebContents* preview_dialog);
// Mapping between print preview dialog and the corresponding initiator.
PrintPreviewDialogMap preview_dialog_map_;
// A registrar for listening notifications.
content::NotificationRegistrar registrar_;
// True if the controller is waiting for a new preview dialog via
// content::NAVIGATION_TYPE_NEW_PAGE.
bool waiting_for_new_preview_page_;
// Whether the PrintPreviewDialogController is in the middle of creating a
// print preview dialog.
bool is_creating_print_preview_dialog_;
DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogController);
};
} // namespace printing
#endif // CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DIALOG_CONTROLLER_H_
|