Index: ios/chrome/browser/reading_list/reading_list_web_state_observer.mm |
diff --git a/ios/chrome/browser/reading_list/reading_list_web_state_observer.mm b/ios/chrome/browser/reading_list/reading_list_web_state_observer.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bb8a78b3b47acb9af9dbd7a754a3f07fd5a3a2c4 |
--- /dev/null |
+++ b/ios/chrome/browser/reading_list/reading_list_web_state_observer.mm |
@@ -0,0 +1,103 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ios/chrome/browser/reading_list/reading_list_web_state_observer.h" |
+ |
+#import <Foundation/Foundation.h> |
+ |
+#include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
+#include "ios/chrome/browser/reading_list/reading_list_model.h" |
+#include "ios/chrome/browser/reading_list/reading_list_model_factory.h" |
+#include "ios/web/public/navigation_item.h" |
+#include "ios/web/public/navigation_manager.h" |
+ |
+DEFINE_WEB_STATE_USER_DATA_KEY(ReadingListWebStateObserver); |
+ |
+void ReadingListWebStateObserver::CreateForWebState( |
+ web::WebState* web_state, |
+ web::BrowserState* browser_state) { |
+ DCHECK(browser_state); |
+ if (!FromWebState(web_state)) { |
+ web_state->SetUserData(UserDataKey(), new ReadingListWebStateObserver( |
+ web_state, browser_state)); |
+ } |
+} |
+ |
+ReadingListWebStateObserver::~ReadingListWebStateObserver() {} |
+ |
+ReadingListWebStateObserver::ReadingListWebStateObserver( |
+ web::WebState* web_state, |
+ web::BrowserState* browser_state) |
+ : web::WebStateObserver(web_state), |
+ web_state_(web_state), |
+ reading_list_model_(nullptr) { |
+ DCHECK(web_state_); |
+ |
+ ios::ChromeBrowserState* chrome_browser_state = |
+ ios::ChromeBrowserState::FromBrowserState(browser_state); |
+ reading_list_model_ = |
+ ReadingListModelFactory::GetForBrowserState(chrome_browser_state); |
sdefresne
2016/10/04 13:26:46
Pass the reading_list_model as a parameter, and re
|
+ DCHECK(reading_list_model_); |
+} |
+ |
+void ReadingListWebStateObserver::NavigationItemChanged() { |
+ timer_.reset(); |
+} |
+ |
+void ReadingListWebStateObserver::NavigationItemCommitted( |
+ const web::LoadCommittedDetails& load_details) { |
+ timer_.reset(); |
+} |
+ |
+void ReadingListWebStateObserver::DidStartLoading() { |
+ timer_.reset(); |
+ web::NavigationManager const* navigation_manager = |
+ web_state_->GetNavigationManager(); |
+ DCHECK(navigation_manager); |
+ auto* pending_item = navigation_manager->GetPendingItem(); |
+ if (pending_item) { |
+ NSDictionary* extraData = pending_item->GetExtraData(); |
+ // TODO(crbug.com/xxxx): Ignore navigations to offline pages. |
+ if (extraData[@"readingList"]) { |
+ timer_.reset(new base::Timer(false, true)); |
+ base::TimeDelta delay = base::TimeDelta::FromMilliseconds(1000); |
+ timer_->Start(FROM_HERE, delay, |
+ base::Bind(&ReadingListWebStateObserver:: |
+ VerifyIfReadingListEntryStartedLoading, |
+ base::Unretained(this))); |
+ } |
+ } |
+} |
+ |
+void ReadingListWebStateObserver::DidStopLoading() { |
+ timer_.reset(); |
+} |
+ |
+void ReadingListWebStateObserver::PageLoaded( |
+ web::PageLoadCompletionStatus load_completion_status) { |
+ timer_.reset(); |
+} |
+ |
+void ReadingListWebStateObserver::WebStateDestroyed() { |
+ timer_.reset(); |
+} |
+ |
+void ReadingListWebStateObserver::VerifyIfReadingListEntryStartedLoading() { |
+ web::NavigationManager* navigation_manager = |
+ web_state_->GetNavigationManager(); |
+ web::NavigationItem* pending_item = navigation_manager->GetPendingItem(); |
+ DCHECK_EQ(ui::PAGE_TRANSITION_READING_LIST, |
+ pending_item->GetTransitionType()); |
+ const GURL& url = pending_item->GetURL(); |
+ double progress = web_state_->GetLoadingProgress(); |
+ if (progress < 15) { |
sdefresne
2016/10/04 13:26:46
Can you use a named constant instead of "15" so th
|
+ auto entry = reading_list_model_->GetEntryFromURL(url); |
+ if (!entry) |
+ return; |
+ web::NavigationManager::WebLoadParams params(entry->DistilledURL()); |
+ params.transition_type = ui::PAGE_TRANSITION_READING_LIST; |
+ params.is_renderer_initiated = NO; |
+ navigation_manager->LoadURLWithParams(params); |
+ } |
+} |