Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/dom_distiller/ReaderModeManager.java

Issue 1248643004: Test distillability without JavaScript (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@early
Patch Set: fix browsertest, merge webkit CL, merge http://crrev.com/1403413004 Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/chrome_content_browser_client.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.dom_distiller; 5 package org.chromium.chrome.browser.dom_distiller;
6 6
7 import android.app.Activity; 7 import android.app.Activity;
8 import android.content.Context; 8 import android.content.Context;
9 import android.text.TextUtils; 9 import android.text.TextUtils;
10 10
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 /** 74 /**
75 * Whether the page is an article or not. 75 * Whether the page is an article or not.
76 */ 76 */
77 private int mReaderModeStatus = NOT_POSSIBLE; 77 private int mReaderModeStatus = NOT_POSSIBLE;
78 78
79 /** 79 /**
80 * Whether the fact that the current web page was distillable or not has bee n recorded. 80 * Whether the fact that the current web page was distillable or not has bee n recorded.
81 */ 81 */
82 private boolean mIsUmaRecorded; 82 private boolean mIsUmaRecorded;
83 83
84 private boolean mIsCallbackSet;
85
84 private WebContentsObserver mWebContentsObserver; 86 private WebContentsObserver mWebContentsObserver;
85 87
86 private final Tab mTab; 88 private final Tab mTab;
87 89
88 private final ReaderModePanel mReaderModePanel; 90 private final ReaderModePanel mReaderModePanel;
89 91
90 private final ObserverList<ReaderModeManagerObserver> mObservers; 92 private final ObserverList<ReaderModeManagerObserver> mObservers;
91 93
92 private final int mHeaderBackgroundColor; 94 private final int mHeaderBackgroundColor;
93 95
94 public ReaderModeManager(Tab tab, Context context) { 96 public ReaderModeManager(Tab tab, Context context) {
95 mTab = tab; 97 mTab = tab;
96 mTab.addObserver(this); 98 mTab.addObserver(this);
97 mObservers = new ObserverList<ReaderModeManagerObserver>(); 99 mObservers = new ObserverList<ReaderModeManagerObserver>();
98 mReaderModePanel = isEnabled(context) ? new ReaderModePanel(this) : null ; 100 mReaderModePanel = isEnabled(context) ? new ReaderModePanel(this) : null ;
99 mHeaderBackgroundColor = context != null 101 mHeaderBackgroundColor = context != null
100 ? ApiCompatibilityUtils.getColor( 102 ? ApiCompatibilityUtils.getColor(
101 context.getResources(), R.color.reader_mode_header_bg) 103 context.getResources(), R.color.reader_mode_header_bg)
102 : 0; 104 : 0;
105 mIsCallbackSet = false;
103 } 106 }
104 107
105 /** 108 /**
106 * Adds an observer to be notified about changes to the reader mode status. 109 * Adds an observer to be notified about changes to the reader mode status.
107 */ 110 */
108 public void addObserver(ReaderModeManagerObserver observer) { 111 public void addObserver(ReaderModeManagerObserver observer) {
109 mObservers.addObserver(observer); 112 mObservers.addObserver(observer);
110 } 113 }
111 114
112 /** 115 /**
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 return mReaderModePanel; 243 return mReaderModePanel;
241 } 244 }
242 245
243 private ReaderModeActivityDelegate getReaderModeActivityDelegate() { 246 private ReaderModeActivityDelegate getReaderModeActivityDelegate() {
244 return mTab.getReaderModeActivityDelegate(); 247 return mTab.getReaderModeActivityDelegate();
245 } 248 }
246 249
247 private WebContentsObserver createWebContentsObserver(WebContents webContent s) { 250 private WebContentsObserver createWebContentsObserver(WebContents webContent s) {
248 return new WebContentsObserver(webContents) { 251 return new WebContentsObserver(webContents) {
249 @Override 252 @Override
250 public void didFinishLoad(long frameId, String validatedUrl, boolean isMainFrame) {
251 if (!isMainFrame) return;
252 if (DomDistillerUrlUtils.isDistilledPage(mTab.getUrl())) return;
253 updateStatusBasedOnReaderModeCriteria(true);
254 }
255
256 @Override
257 public void didFailLoad(boolean isProvisionalLoad, boolean isMainFra me, int errorCode,
258 String description, String failingUrl, boolean wasIgnore dByHandler) {
259 if (!isMainFrame) return;
260 if (DomDistillerUrlUtils.isDistilledPage(mTab.getUrl())) return;
261 updateStatusBasedOnReaderModeCriteria(true);
262 }
263
264 @Override
265 public void didStartProvisionalLoadForFrame(long frameId, long paren tFrameId, 253 public void didStartProvisionalLoadForFrame(long frameId, long paren tFrameId,
266 boolean isMainFrame, String validatedUrl, boolean isErrorPag e, 254 boolean isMainFrame, String validatedUrl, boolean isErrorPag e,
267 boolean isIframeSrcdoc) { 255 boolean isIframeSrcdoc) {
268 if (!isMainFrame) return; 256 if (!isMainFrame) return;
269 if (DomDistillerUrlUtils.isDistilledPage(validatedUrl)) { 257 if (DomDistillerUrlUtils.isDistilledPage(validatedUrl)) {
270 mReaderModeStatus = STARTED; 258 mReaderModeStatus = STARTED;
271 sendReaderModeStatusChangedNotification(); 259 sendReaderModeStatusChangedNotification();
272 mReaderModePageUrl = validatedUrl; 260 mReaderModePageUrl = validatedUrl;
273 } 261 }
274 } 262 }
275 263
276 @Override 264 @Override
277 public void didNavigateMainFrame(String url, String baseUrl, 265 public void didNavigateMainFrame(String url, String baseUrl,
278 boolean isNavigationToDifferentPage, boolean isNavigationInP age, 266 boolean isNavigationToDifferentPage, boolean isNavigationInP age,
279 int statusCode) { 267 int statusCode) {
280 // TODO(cjhopman): This should possibly ignore navigations that replace the entry 268 // TODO(cjhopman): This should possibly ignore navigations that replace the entry
281 // (like those from history.replaceState()). 269 // (like those from history.replaceState()).
282 if (isNavigationInPage) return; 270 if (isNavigationInPage) return;
283 if (DomDistillerUrlUtils.isDistilledPage(url)) return; 271 if (DomDistillerUrlUtils.isDistilledPage(url)) return;
284 272
285 mReaderModeStatus = POSSIBLE; 273 mReaderModeStatus = POSSIBLE;
286 if (!TextUtils.equals(url, 274 if (!TextUtils.equals(url,
287 DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl( 275 DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(
288 mReaderModePageUrl))) { 276 mReaderModePageUrl))) {
289 mReaderModeStatus = NOT_POSSIBLE; 277 mReaderModeStatus = NOT_POSSIBLE;
290 mIsUmaRecorded = false; 278 mIsUmaRecorded = false;
291 // Do not call updateStatusBasedOnReaderModeCriteria here. 279 if (!mIsCallbackSet) setDistillabilityCallback();
292 // For ADABOOST_MODEL, it is unlikely to get valid info at t his event.
293 } 280 }
294 mReaderModePageUrl = null; 281 mReaderModePageUrl = null;
295 sendReaderModeStatusChangedNotification(); 282 sendReaderModeStatusChangedNotification();
296 } 283 }
297 }; 284 };
298 } 285 }
299 286
300 // Updates reader mode status based on whether or not the page should be vie wed in reader mode. 287 // Set the callback for updating reader mode status based on whether or not the page should
301 private void updateStatusBasedOnReaderModeCriteria(final boolean forceRecord ) { 288 // be viewed in reader mode.
289 private void setDistillabilityCallback() {
302 if (mTab.getWebContents() == null) return; 290 if (mTab.getWebContents() == null) return;
303 if (mTab.getContentViewCore() == null) return; 291 if (mTab.getContentViewCore() == null) return;
304 292
305 DistillablePageUtils.isPageDistillable(mTab.getWebContents(), 293 DistillablePageUtils.setCallback(mTab.getWebContents(),
306 mTab.getContentViewCore().getIsMobileOptimizedHint(),
307 new DistillablePageUtils.PageDistillableCallback() { 294 new DistillablePageUtils.PageDistillableCallback() {
308 @Override 295 @Override
309 public void onIsPageDistillableResult(boolean isDistillable) { 296 public void onIsPageDistillableResult(boolean isDistillable, boolean isLast) {
310 if (isDistillable) { 297 if (isDistillable) {
311 mReaderModeStatus = POSSIBLE; 298 mReaderModeStatus = POSSIBLE;
312 } else { 299 } else {
313 mReaderModeStatus = NOT_POSSIBLE; 300 mReaderModeStatus = NOT_POSSIBLE;
314 } 301 }
315 if (!mIsUmaRecorded && (mReaderModeStatus == POSSIBLE || forceRecord)) { 302 if (!mIsUmaRecorded && (mReaderModeStatus == POSSIBLE || isLast)) {
316 mIsUmaRecorded = true; 303 mIsUmaRecorded = true;
317 RecordHistogram.recordBooleanHistogram( 304 RecordHistogram.recordBooleanHistogram(
318 "DomDistiller.PageDistillable", mReaderModeS tatus == POSSIBLE); 305 "DomDistiller.PageDistillable", mReaderModeS tatus == POSSIBLE);
319 } 306 }
320 sendReaderModeStatusChangedNotification(); 307 sendReaderModeStatusChangedNotification();
321 } 308 }
322 }); 309 });
310 mIsCallbackSet = true;
323 } 311 }
324 312
325 private void sendReaderModeStatusChangedNotification() { 313 private void sendReaderModeStatusChangedNotification() {
326 for (ReaderModeManagerObserver observer : mObservers) { 314 for (ReaderModeManagerObserver observer : mObservers) {
327 observer.onReaderModeStatusChanged(mReaderModeStatus); 315 observer.onReaderModeStatusChanged(mReaderModeStatus);
328 } 316 }
329 if (mReaderModePanel != null) mReaderModePanel.updateBottomButtonBar(); 317 if (mReaderModePanel != null) mReaderModePanel.updateBottomButtonBar();
330 } 318 }
331 319
332 private ContextualSearchManager getContextualSearchManager(Tab tab) { 320 private ContextualSearchManager getContextualSearchManager(Tab tab) {
(...skipping 11 matching lines...) Expand all
344 if (context == null) return false; 332 if (context == null) return false;
345 333
346 boolean enabled = CommandLine.getInstance().hasSwitch(ChromeSwitches.ENA BLE_DOM_DISTILLER) 334 boolean enabled = CommandLine.getInstance().hasSwitch(ChromeSwitches.ENA BLE_DOM_DISTILLER)
347 && !CommandLine.getInstance().hasSwitch( 335 && !CommandLine.getInstance().hasSwitch(
348 ChromeSwitches.DISABLE_READER_MODE_BOTTOM_BAR) 336 ChromeSwitches.DISABLE_READER_MODE_BOTTOM_BAR)
349 && !DeviceFormFactor.isTablet(context) 337 && !DeviceFormFactor.isTablet(context)
350 && DomDistillerTabUtils.isDistillerHeuristicsEnabled(); 338 && DomDistillerTabUtils.isDistillerHeuristicsEnabled();
351 return enabled; 339 return enabled;
352 } 340 }
353 } 341 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/chrome_content_browser_client.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698