blob: 829fae50518ad2cd0127e25b4637d077d6ec3e1d (
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
|
// Copyright 2013 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.
package org.chromium.chrome.browser.printing;
import android.text.TextUtils;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.content_public.browser.WebContents;
import org.chromium.printing.Printable;
import java.lang.ref.WeakReference;
/**
* Wraps printing related functionality of a {@link Tab} object.
*
* This class doesn't have any lifetime expectations with regards to Tab, since we keep a weak
* reference.
*/
@JNINamespace("printing")
public class TabPrinter implements Printable {
private static final String TAG = "printing";
private final WeakReference<Tab> mTab;
private final String mDefaultTitle;
@CalledByNative
private static TabPrinter getPrintable(Tab tab) {
return new TabPrinter(tab);
}
public TabPrinter(Tab tab) {
mTab = new WeakReference<Tab>(tab);
mDefaultTitle = ContextUtils.getApplicationContext().getResources().getString(
R.string.menu_print);
}
@Override
public boolean print(int renderProcessId, int renderFrameId) {
if (!canPrint()) return false;
Tab tab = mTab.get();
assert tab != null && tab.isInitialized();
return nativePrint(tab.getWebContents(), renderProcessId, renderFrameId);
}
@Override
public String getTitle() {
Tab tab = mTab.get();
if (tab == null) return mDefaultTitle;
String title = tab.getTitle();
if (!TextUtils.isEmpty(title)) return title;
String url = tab.getUrl();
if (!TextUtils.isEmpty(url)) return url;
return mDefaultTitle;
}
@Override
public boolean canPrint() {
Tab tab = mTab.get();
if (tab == null || !tab.isInitialized()) {
// tab.isInitialized() will be false if tab is in destroy process.
Log.d(TAG, "Tab is not avaliable for printing.");
return false;
}
return true;
}
private static native boolean nativePrint(
WebContents webContents, int renderProcessId, int renderFrameId);
}
|