OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 package org.chromium.chrome.browser.dom_distiller; |
| 6 |
| 7 import org.chromium.base.CalledByNative; |
| 8 import org.chromium.base.JNINamespace; |
| 9 import org.chromium.chrome.browser.EmptyTabObserver; |
| 10 import org.chromium.chrome.browser.Tab; |
| 11 import org.chromium.chrome.browser.TabObserver; |
| 12 import org.chromium.content.browser.ContentView; |
| 13 import org.chromium.content_public.browser.WebContents; |
| 14 |
| 15 /** |
| 16 * Java implementation of FeedbackReporterAndroid. |
| 17 */ |
| 18 @JNINamespace("dom_distiller::android") |
| 19 public final class FeedbackReporter implements FeedbackReportingView.FeedbackObs
erver { |
| 20 |
| 21 private final long mNativePointer; |
| 22 private final Tab mTab; |
| 23 private ContentView mContentView; |
| 24 private FeedbackReportingView mFeedbackReportingView; |
| 25 private String mOverlayUrl; |
| 26 |
| 27 /** |
| 28 * @return whether the DOM Distiller feature is enabled. |
| 29 */ |
| 30 public static boolean isEnabled() { |
| 31 return nativeIsEnabled(); |
| 32 } |
| 33 |
| 34 /** |
| 35 * Records feedback for the distilled content. |
| 36 * |
| 37 * @param good whether the perceived quality of the distillation of a web pa
ge was good. |
| 38 */ |
| 39 private static void recordQuality(boolean good) { |
| 40 nativeReportQuality(good); |
| 41 } |
| 42 |
| 43 /** |
| 44 * Creates the FeedbackReporter, adds itself as a TabObserver, and ensures |
| 45 * references to ContentView and WebContents are up to date. |
| 46 * |
| 47 * @param tab the tab where the overlay should be displayed. |
| 48 */ |
| 49 public FeedbackReporter(Tab tab) { |
| 50 mNativePointer = nativeInit(); |
| 51 mTab = tab; |
| 52 mTab.addObserver(createTabObserver()); |
| 53 updatePointers(); |
| 54 } |
| 55 |
| 56 @Override |
| 57 public void onYesPressed(FeedbackReportingView view) { |
| 58 if (view != mFeedbackReportingView) return; |
| 59 recordQuality(true); |
| 60 dismissOverlay(); |
| 61 } |
| 62 |
| 63 @Override |
| 64 public void onNoPressed(FeedbackReportingView view) { |
| 65 if (view != mFeedbackReportingView) return; |
| 66 recordQuality(false); |
| 67 dismissOverlay(); |
| 68 } |
| 69 |
| 70 /** |
| 71 * Start showing the overlay. |
| 72 */ |
| 73 private void showOverlay() { |
| 74 mFeedbackReportingView = FeedbackReportingView.create(mContentView, this
); |
| 75 } |
| 76 |
| 77 @CalledByNative |
| 78 private void dismissOverlay() { |
| 79 if (mFeedbackReportingView != null) mFeedbackReportingView.dismiss(); |
| 80 mOverlayUrl = null; |
| 81 mFeedbackReportingView = null; |
| 82 } |
| 83 |
| 84 /** |
| 85 * Dismiss the overlay which is currently being displayed. |
| 86 */ |
| 87 @CalledByNative |
| 88 private String getCurrentOverlayUrl() { |
| 89 return mOverlayUrl; |
| 90 } |
| 91 |
| 92 /** |
| 93 * Updates which ContentView and WebContents the FeedbackReporter is monitor
ing. |
| 94 */ |
| 95 private void updatePointers() { |
| 96 mContentView = mTab.getContentView(); |
| 97 nativeReplaceWebContents(mNativePointer, mTab.getWebContents()); |
| 98 } |
| 99 |
| 100 /** |
| 101 * Creates a TabObserver for monitoring a Tab, used to react to changes in t
he ContentView |
| 102 * or to trigger its own destruction. |
| 103 * |
| 104 * @return TabObserver that can be used to monitor a Tab. |
| 105 */ |
| 106 private TabObserver createTabObserver() { |
| 107 return new EmptyTabObserver() { |
| 108 @Override |
| 109 public void onWebContentsSwapped(Tab tab, boolean didStartLoad, |
| 110 boolean didFinishLoad) { |
| 111 updatePointers(); |
| 112 } |
| 113 |
| 114 @Override |
| 115 public void onContentChanged(Tab tab) { |
| 116 updatePointers(); |
| 117 } |
| 118 |
| 119 @Override |
| 120 public void onUpdateUrl(Tab tab, String url) { |
| 121 boolean reportable = nativeIsReportableUrl(url); |
| 122 if (reportable) { |
| 123 mOverlayUrl = url; |
| 124 showOverlay(); |
| 125 } else { |
| 126 dismissOverlay(); |
| 127 } |
| 128 } |
| 129 |
| 130 @Override |
| 131 public void onDestroyed(Tab tab) { |
| 132 nativeDestroy(mNativePointer); |
| 133 mContentView = null; |
| 134 } |
| 135 }; |
| 136 } |
| 137 |
| 138 private static native boolean nativeIsEnabled(); |
| 139 |
| 140 private static native boolean nativeIsReportableUrl(String url); |
| 141 |
| 142 private static native void nativeReportQuality(boolean good); |
| 143 |
| 144 private native long nativeInit(); |
| 145 |
| 146 private native void nativeDestroy(long nativeFeedbackReporterAndroid); |
| 147 |
| 148 private native void nativeReplaceWebContents( |
| 149 long nativeFeedbackReporterAndroid, WebContents webContents); |
| 150 } |
OLD | NEW |