1// Copyright 2013 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
5package org.chromium.chrome.browser.printing;
6
7import android.text.TextUtils;
8
9import org.chromium.chrome.browser.Tab;
10import org.chromium.printing.Printable;
11
12import java.lang.ref.WeakReference;
13
14/**
15 * Wraps printing related functionality of a {@link Tab} object.
16 *
17 * This class doesn't have any lifetime expectations with regards to Tab, since we keep a weak
18 * reference.
19 */
20public class TabPrinter implements Printable {
21    private static String sDefaultTitle;
22
23    private final WeakReference<Tab> mTab;
24
25    public TabPrinter(Tab tab) {
26        mTab = new WeakReference<Tab>(tab);
27    }
28
29    public static void setDefaultTitle(String defaultTitle) {
30        sDefaultTitle = defaultTitle;
31    }
32
33    @Override
34    public boolean print() {
35        Tab tab = mTab.get();
36        return tab != null && tab.print();
37    }
38
39    @Override
40    public String getTitle() {
41        Tab tab = mTab.get();
42        if (tab == null) return sDefaultTitle;
43
44        String title = tab.getTitle();
45        if (!TextUtils.isEmpty(title)) return title;
46
47        String url = tab.getUrl();
48        if (!TextUtils.isEmpty(url)) return url;
49
50        return sDefaultTitle;
51    }
52}
53