summaryrefslogtreecommitdiffstats
path: root/android_webview/browser/aw_printing_message_filter.cc
blob: f0fdee98c25c9e12a3f8d4ace2d88858a5be525c (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
// Copyright 2015 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.

#include "android_webview/browser/aw_printing_message_filter.h"

#include "android_webview/browser/renderer_host/print_manager.h"
#include "components/printing/common/print_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"

using content::BrowserThread;
using content::WebContents;


namespace android_webview {

namespace {

PrintManager* GetPrintManager(int render_process_id, int render_view_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  content::RenderViewHost* rvh = content::RenderViewHost::FromID(
      render_process_id, render_view_id);
  if (rvh == nullptr)
    return nullptr;
  WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
  if (web_contents == nullptr)
    return nullptr;
  return PrintManager::FromWebContents(web_contents);
}

} // namespace

AwPrintingMessageFilter::AwPrintingMessageFilter(int render_process_id)
    : BrowserMessageFilter(PrintMsgStart),
      render_process_id_(render_process_id) {
}

AwPrintingMessageFilter::~AwPrintingMessageFilter() {
}

void AwPrintingMessageFilter::OverrideThreadForMessage(
    const IPC::Message& message, BrowserThread::ID* thread) {
  if (message.type() == PrintHostMsg_AllocateTempFileForPrinting::ID ||
      message.type() == PrintHostMsg_TempFileForPrintingWritten::ID) {
    *thread = BrowserThread::UI;
  }
}

bool AwPrintingMessageFilter::OnMessageReceived(const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(AwPrintingMessageFilter, message)
    IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting,
                        OnAllocateTempFileForPrinting)
    IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten,
                        OnTempFileForPrintingWritten)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void AwPrintingMessageFilter::OnAllocateTempFileForPrinting(
    int render_view_id,
    base::FileDescriptor* temp_file_fd,
    int* sequence_number) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  PrintManager* print_manager =
      GetPrintManager(render_process_id_, render_view_id);
  if (print_manager == nullptr)
    return;
  print_manager->OnAllocateTempFileForPrinting(temp_file_fd, sequence_number);
}

void AwPrintingMessageFilter::OnTempFileForPrintingWritten(
    int render_view_id,
    int sequence_number) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  PrintManager* print_manager =
      GetPrintManager(render_process_id_, render_view_id);
  if (print_manager == nullptr)
    return;
  print_manager->OnTempFileForPrintingWritten(sequence_number);
}

}  // namespace android_webview