1 // Copyright 2014 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.
5 package org.chromium.chrome.browser.dom_distiller;
7 import android.app.Activity;
9 import org.chromium.base.CalledByNative;
10 import org.chromium.base.JNINamespace;
11 import org.chromium.chrome.browser.EmptyTabObserver;
12 import org.chromium.chrome.browser.Tab;
13 import org.chromium.chrome.browser.TabObserver;
14 import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils;
15 import org.chromium.content.browser.ContentViewCore;
16 import org.chromium.content_public.browser.WebContents;
19 * Java implementation of dom_distiller::android::FeedbackReporterAndroid.
21 @JNINamespace("dom_distiller::android")
22 public final class DomDistillerFeedbackReporter implements
23 DomDistillerFeedbackReportingView.FeedbackObserver {
25 private static ExternalFeedbackReporter sExternalFeedbackReporter =
26 new NoOpExternalFeedbackReporter();
28 public static void setExternalFeedbackReporter(ExternalFeedbackReporter reporter) {
29 sExternalFeedbackReporter = reporter;
32 private static class NoOpExternalFeedbackReporter implements ExternalFeedbackReporter {
34 public void reportFeedback(Activity activity, String url, boolean good) {
38 private final long mNativePointer;
39 private final Tab mTab;
41 private ContentViewCore mContentViewCore;
42 private DomDistillerFeedbackReportingView mReportingView;
45 * @return whether the DOM Distiller feature is enabled.
47 public static boolean isEnabled() {
52 * Creates the FeedbackReporter, adds itself as a TabObserver, and ensures
53 * references to ContentView and WebContents are up to date.
55 * @param tab the tab where the overlay should be displayed.
57 public DomDistillerFeedbackReporter(Tab tab) {
58 mNativePointer = nativeInit();
60 mTab.addObserver(createTabObserver());
65 public void onYesPressed(DomDistillerFeedbackReportingView view) {
66 if (view != mReportingView) return;
72 public void onNoPressed(DomDistillerFeedbackReportingView view) {
73 if (view != mReportingView) return;
79 * Records feedback for the distilled content.
81 * @param good whether the perceived quality of the distillation of a web page was good.
83 private void recordQuality(boolean good) {
84 nativeReportQuality(good);
86 Activity activity = mTab.getWindowAndroid().getActivity().get();
87 String url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(
88 mContentViewCore.getWebContents().getUrl());
89 sExternalFeedbackReporter.reportFeedback(activity, url, good);
94 * Start showing the overlay.
97 private void showOverlay() {
98 mReportingView = DomDistillerFeedbackReportingView.create(mContentViewCore, this);
102 * Dismiss the overlay which is currently being displayed.
105 private void dismissOverlay() {
106 if (mReportingView != null) {
107 mReportingView.dismiss(true);
108 mReportingView = null;
113 * Updates which ContentViewCore and WebContents the FeedbackReporter is monitoring.
115 private void updatePointers() {
116 mContentViewCore = mTab.getContentViewCore();
117 nativeReplaceWebContents(mNativePointer, mTab.getWebContents());
121 * Creates a TabObserver for monitoring a Tab, used to react to changes in the ContentViewCore
122 * or to trigger its own destruction.
124 * @return TabObserver that can be used to monitor a Tab.
126 private TabObserver createTabObserver() {
127 return new EmptyTabObserver() {
129 public void onWebContentsSwapped(Tab tab, boolean didStartLoad,
130 boolean didFinishLoad) {
135 public void onContentChanged(Tab tab) {
140 public void onDestroyed(Tab tab) {
141 nativeDestroy(mNativePointer);
142 mContentViewCore = null;
147 private static native boolean nativeIsEnabled();
149 private static native void nativeReportQuality(boolean good);
151 private native long nativeInit();
153 private native void nativeDestroy(long nativeFeedbackReporterAndroid);
155 private native void nativeReplaceWebContents(
156 long nativeFeedbackReporterAndroid, WebContents webContents);