OLD | NEW |
---|---|
(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); | |
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 // TODO(crbug.com/xxxx): Ignore navigations to offline pages. | |
61 if (pending_item->GetTransitionType() == ui::PAGE_TRANSITION_READING_LIST) { | |
62 timer_.reset(new base::Timer(false, true)); | |
gambard
2016/10/03 09:04:45
Do we want to initialize it with |is_repeating=tru
| |
63 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(1000); | |
64 timer_->Start(FROM_HERE, delay, | |
65 base::Bind(&ReadingListWebStateObserver:: | |
66 VerifyIfReadingListEntryStartedLoading, | |
67 base::Unretained(this))); | |
68 } | |
69 } | |
70 } | |
71 | |
72 void ReadingListWebStateObserver::DidStopLoading() { | |
73 timer_.reset(); | |
74 } | |
75 | |
76 void ReadingListWebStateObserver::PageLoaded( | |
77 web::PageLoadCompletionStatus load_completion_status) { | |
78 timer_.reset(); | |
79 } | |
80 | |
81 void ReadingListWebStateObserver::WebStateDestroyed() { | |
82 timer_.reset(); | |
83 } | |
84 | |
85 void ReadingListWebStateObserver::VerifyIfReadingListEntryStartedLoading() { | |
86 web::NavigationManager* navigation_manager = | |
87 web_state_->GetNavigationManager(); | |
88 web::NavigationItem* pending_item = navigation_manager->GetPendingItem(); | |
89 DCHECK_EQ(ui::PAGE_TRANSITION_READING_LIST, | |
90 pending_item->GetTransitionType()); | |
91 const GURL& url = pending_item->GetURL(); | |
92 double progress = web_state_->GetLoadingProgress(); | |
93 if (progress < 15) { | |
94 auto entry = reading_list_model_->GetEntryFromURL(url); | |
95 if (!entry) | |
96 return; | |
97 web::NavigationManager::WebLoadParams params(entry->DistilledURL()); | |
98 params.transition_type = ui::PAGE_TRANSITION_READING_LIST; | |
99 params.is_renderer_initiated = NO; | |
100 navigation_manager->LoadURLWithParams(params); | |
101 } | |
102 } | |
OLD | NEW |