Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / printing / print_preview_dialog_controller.h
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_PRINTING_PRINT_PREVIEW_DIALOG_CONTROLLER_H_
6 #define CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DIALOG_CONTROLLER_H_
7
8 #include <vector>
9
10 #include "base/memory/ref_counted.h"
11 #include "chrome/browser/sessions/session_id.h"
12
13 class GURL;
14
15 namespace content {
16 struct LoadCommittedDetails;
17 class RenderProcessHost;
18 class WebContents;
19 }
20
21 namespace printing {
22
23 // For print preview, the WebContents that initiates the printing operation is
24 // the initiator, and the constrained dialog that shows the print preview is the
25 // print preview dialog.
26 // This class manages print preview dialog creation and destruction, and keeps
27 // track of the 1:1 relationship between initiator tabs and print preview
28 // dialogs.
29 class PrintPreviewDialogController
30     : public base::RefCounted<PrintPreviewDialogController> {
31  public:
32   PrintPreviewDialogController();
33
34   static PrintPreviewDialogController* GetInstance();
35
36   // Initiate print preview for |initiator|.
37   // Call this instead of GetOrCreatePreviewDialog().
38   static void PrintPreview(content::WebContents* initiator);
39
40   // Get/Create the print preview dialog for |initiator|.
41   // Exposed for unit tests.
42   content::WebContents* GetOrCreatePreviewDialog(
43       content::WebContents* initiator);
44
45   // Returns the preview dialog for |contents|.
46   // Returns |contents| if |contents| is a preview dialog.
47   // Returns NULL if no preview dialog exists for |contents|.
48   content::WebContents* GetPrintPreviewForContents(
49       content::WebContents* contents) const;
50
51   // Returns the initiator for |preview_dialog|.
52   // Returns NULL if no initiator exists for |preview_dialog|.
53   content::WebContents* GetInitiator(content::WebContents* preview_dialog);
54
55   // Returns true if |contents| is a print preview dialog.
56   static bool IsPrintPreviewDialog(content::WebContents* contents);
57
58   // Returns true if |url| is a print preview url.
59   static bool IsPrintPreviewURL(const GURL& url);
60
61   // Erase the initiator info associated with |preview_dialog|.
62   void EraseInitiatorInfo(content::WebContents* preview_dialog);
63
64   bool is_creating_print_preview_dialog() const {
65     return is_creating_print_preview_dialog_;
66   }
67
68  private:
69   friend class base::RefCounted<PrintPreviewDialogController>;
70   struct Operation;
71
72   virtual ~PrintPreviewDialogController();
73
74   // Handlers for observed events.
75   void OnRenderProcessExited(content::RenderProcessHost* rph);
76   void OnWebContentsDestroyed(content::WebContents* contents);
77   void OnNavigationEntryCommitted(content::WebContents* contents,
78                                   const content::LoadCommittedDetails* details);
79
80   // Creates a new print preview dialog.
81   content::WebContents* CreatePrintPreviewDialog(
82       content::WebContents* initiator);
83
84   // Helper function to store the title of the initiator associated with
85   // |preview_dialog| in |preview_dialog|'s PrintPreviewUI.
86   void SaveInitiatorTitle(content::WebContents* preview_dialog);
87
88   // Removes WebContents when they close/crash/navigate.
89   void RemoveInitiator(content::WebContents* initiator);
90   void RemovePreviewDialog(content::WebContents* preview_dialog);
91
92   // The list of the currently active preview operations.
93   std::vector<Operation*> preview_operations_;
94
95   // True if the controller is waiting for a new preview dialog via
96   // content::NAVIGATION_TYPE_NEW_PAGE.
97   bool waiting_for_new_preview_page_;
98
99   // Whether the PrintPreviewDialogController is in the middle of creating a
100   // print preview dialog.
101   bool is_creating_print_preview_dialog_;
102
103   DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogController);
104 };
105
106 }  // namespace printing
107
108 #endif  // CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DIALOG_CONTROLLER_H_