summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/print_preview_ui.h
blob: 4f3176fcc4430abaf041b5a59e3eb10bc37ecd20 (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
// Copyright (c) 2011 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_UI_WEBUI_PRINT_PREVIEW_UI_H_
#define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_UI_H_
#pragma once

#include <string>

#include "base/memory/ref_counted.h"
#include "base/memory/ref_counted_memory.h"
#include "base/time.h"
#include "chrome/browser/printing/print_preview_data_service.h"
#include "chrome/browser/ui/webui/chrome_web_ui.h"

class PrintPreviewDataService;
class PrintPreviewHandler;
struct PrintHostMsg_DidGetPreviewPageCount_Params;

class PrintPreviewUI : public ChromeWebUI {
 public:
  explicit PrintPreviewUI(TabContents* contents);
  virtual ~PrintPreviewUI();

  // Gets the print preview |data|. |index| is zero-based, and can be
  // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to get the entire preview
  // document.
  void GetPrintPreviewDataForIndex(int index,
                                   scoped_refptr<RefCountedBytes>* data);

  // Sets the print preview |data|. |index| is zero-based, and can be
  // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to set the entire preview
  // document.
  void SetPrintPreviewDataForIndex(int index, const RefCountedBytes* data);

  // Clear the existing print preview data.
  void ClearAllPreviewData();

  // Setters
  void SetInitiatorTabURL(const std::string& initiator_url);

  // Notifies the Web UI that there is a print preview request. There should be
  // a matching call to OnPreviewDataIsAvailable() or OnPrintPreviewFailed().
  void OnPrintPreviewRequest();

  // Notifies the Web UI about the page count of the request preview.
  void OnDidGetPreviewPageCount(
      const PrintHostMsg_DidGetPreviewPageCount_Params& params);

  // Notifies the Web UI that the 0-based page |page_number| has been rendered.
  // |preview_request_id| indicates wich request resulted in this response.
  void OnDidPreviewPage(int page_number, int preview_request_id);

  // Notifies the Web UI renderer that preview data is available.
  // |expected_pages_count| specifies the total number of pages. |job_title| is
  // the title of the page being previewed. |preview_request_id| indicates which
  // request resulted in this response.
  void OnPreviewDataIsAvailable(int expected_pages_count,
                                const string16& job_title,
                                int preview_request_id);

  void OnReusePreviewData(int preview_request_id);

  // Notifies the Web UI that preview tab is destroyed. This is the last chance
  // to communicate with the source tab before the association is erased.
  void OnTabDestroyed();

  // Notifies the Web UI that the print preview failed to render.
  void OnPrintPreviewFailed();

  // Notifies the Web UI that the print preview request has been cancelled.
  void OnPrintPreviewCancelled();

  // Notifies the Web UI that initiator tab is closed, so we can disable all the
  // controls that need the initiator tab for generating the preview data.
  void OnInitiatorTabClosed();

  // Notifies the Web UI that the initiator tab has crashed.
  void OnInitiatorTabCrashed();

  // Notifies the Web UI renderer that file selection has been cancelled.
  void OnFileSelectionCancelled();

  // Returns true if there are pending requests.
  bool HasPendingRequests();

  int document_cookie();

 private:
  // Returns the Singleton instance of the PrintPreviewDataService.
  PrintPreviewDataService* print_preview_data_service();

  void DecrementRequestCount();

  base::TimeTicks initial_preview_start_time_;

  // Store the PrintPreviewUI address string.
  std::string preview_ui_addr_str_;

  // Weak pointer to the WebUI handler.
  PrintPreviewHandler* handler_;

  // The number of print preview requests in flight.
  uint32 request_count_;

  // Document cookie from the initiator renderer.
  int document_cookie_;

  // Store the |initiator_url| in order to display an accurate error message
  // when the initiator tab is closed/crashed.
  std::string initiator_url_;

  DISALLOW_COPY_AND_ASSIGN(PrintPreviewUI);
};

#endif  // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_UI_H_