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

Side by Side Diff: ios/chrome/browser/reading_list/reading_list_web_state_observer.mm

Issue 2378123002: Load offline page if reading list entry takes more than 1s to load. (Closed)
Patch Set: Experimental change (reviewers: do not review this PS). Created 4 years, 2 months 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
OLDNEW
(Empty)
1 // Copyright 2016 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 #include "ios/chrome/browser/reading_list/reading_list_web_state_observer.h"
6
7 #import <Foundation/Foundation.h>
8
9 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
10 #include "ios/chrome/browser/reading_list/reading_list_model.h"
11 #include "ios/chrome/browser/reading_list/reading_list_model_factory.h"
12 #include "ios/web/public/navigation_item.h"
13 #include "ios/web/public/navigation_manager.h"
14
15 DEFINE_WEB_STATE_USER_DATA_KEY(ReadingListWebStateObserver);
16
17 void ReadingListWebStateObserver::CreateForWebState(
18 web::WebState* web_state,
19 web::BrowserState* browser_state) {
20 DCHECK(browser_state);
21 if (!FromWebState(web_state)) {
22 web_state->SetUserData(UserDataKey(), new ReadingListWebStateObserver(
23 web_state, browser_state));
24 }
25 }
26
27 ReadingListWebStateObserver::~ReadingListWebStateObserver() {}
28
29 ReadingListWebStateObserver::ReadingListWebStateObserver(
30 web::WebState* web_state,
31 web::BrowserState* browser_state)
32 : web::WebStateObserver(web_state),
33 web_state_(web_state),
34 reading_list_model_(nullptr) {
35 DCHECK(web_state_);
36
37 ios::ChromeBrowserState* chrome_browser_state =
38 ios::ChromeBrowserState::FromBrowserState(browser_state);
39 reading_list_model_ =
40 ReadingListModelFactory::GetForBrowserState(chrome_browser_state);
sdefresne 2016/10/04 13:26:46 Pass the reading_list_model as a parameter, and re
41 DCHECK(reading_list_model_);
42 }
43
44 void ReadingListWebStateObserver::NavigationItemChanged() {
45 timer_.reset();
46 }
47
48 void ReadingListWebStateObserver::NavigationItemCommitted(
49 const web::LoadCommittedDetails& load_details) {
50 timer_.reset();
51 }
52
53 void ReadingListWebStateObserver::DidStartLoading() {
54 timer_.reset();
55 web::NavigationManager const* navigation_manager =
56 web_state_->GetNavigationManager();
57 DCHECK(navigation_manager);
58 auto* pending_item = navigation_manager->GetPendingItem();
59 if (pending_item) {
60 NSDictionary* extraData = pending_item->GetExtraData();
61 // TODO(crbug.com/xxxx): Ignore navigations to offline pages.
62 if (extraData[@"readingList"]) {
63 timer_.reset(new base::Timer(false, true));
64 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(1000);
65 timer_->Start(FROM_HERE, delay,
66 base::Bind(&ReadingListWebStateObserver::
67 VerifyIfReadingListEntryStartedLoading,
68 base::Unretained(this)));
69 }
70 }
71 }
72
73 void ReadingListWebStateObserver::DidStopLoading() {
74 timer_.reset();
75 }
76
77 void ReadingListWebStateObserver::PageLoaded(
78 web::PageLoadCompletionStatus load_completion_status) {
79 timer_.reset();
80 }
81
82 void ReadingListWebStateObserver::WebStateDestroyed() {
83 timer_.reset();
84 }
85
86 void ReadingListWebStateObserver::VerifyIfReadingListEntryStartedLoading() {
87 web::NavigationManager* navigation_manager =
88 web_state_->GetNavigationManager();
89 web::NavigationItem* pending_item = navigation_manager->GetPendingItem();
90 DCHECK_EQ(ui::PAGE_TRANSITION_READING_LIST,
91 pending_item->GetTransitionType());
92 const GURL& url = pending_item->GetURL();
93 double progress = web_state_->GetLoadingProgress();
94 if (progress < 15) {
sdefresne 2016/10/04 13:26:46 Can you use a named constant instead of "15" so th
95 auto entry = reading_list_model_->GetEntryFromURL(url);
96 if (!entry)
97 return;
98 web::NavigationManager::WebLoadParams params(entry->DistilledURL());
99 params.transition_type = ui::PAGE_TRANSITION_READING_LIST;
100 params.is_renderer_initiated = NO;
101 navigation_manager->LoadURLWithParams(params);
102 }
103 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698