| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ |
| 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ | 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "components/offline_pages/request_header/offline_page_header.h" |
| 11 #include "content/public/browser/web_contents_observer.h" | 12 #include "content/public/browser/web_contents_observer.h" |
| 12 #include "content/public/browser/web_contents_user_data.h" | 13 #include "content/public/browser/web_contents_user_data.h" |
| 13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 class WebContents; | 17 class WebContents; |
| 17 } | 18 } |
| 18 | 19 |
| 19 namespace offline_pages { | 20 namespace offline_pages { |
| 20 | 21 |
| 21 struct OfflinePageItem; | 22 struct OfflinePageItem; |
| 22 | 23 |
| 23 // Per-tab class that monitors the navigations and stores the necessary info | 24 // Per-tab class that monitors the navigations and stores the necessary info |
| 24 // to facilitate the synchronous access to offline information. | 25 // to facilitate the synchronous access to offline information. |
| 25 class OfflinePageTabHelper : | 26 class OfflinePageTabHelper : |
| 26 public content::WebContentsObserver, | 27 public content::WebContentsObserver, |
| 27 public content::WebContentsUserData<OfflinePageTabHelper> { | 28 public content::WebContentsUserData<OfflinePageTabHelper> { |
| 28 public: | 29 public: |
| 29 ~OfflinePageTabHelper() override; | 30 ~OfflinePageTabHelper() override; |
| 30 | 31 |
| 31 const OfflinePageItem* offline_page() { return offline_page_.get(); } | |
| 32 void SetOfflinePage(const OfflinePageItem& offline_page, | 32 void SetOfflinePage(const OfflinePageItem& offline_page, |
| 33 const OfflinePageHeader& offline_header, |
| 33 bool is_offline_preview); | 34 bool is_offline_preview); |
| 34 | 35 |
| 36 const OfflinePageItem* offline_page() { |
| 37 return offline_info_.offline_page.get(); |
| 38 } |
| 39 |
| 40 const OfflinePageHeader& offline_header() const { |
| 41 return offline_info_.offline_header; |
| 42 } |
| 43 |
| 35 // Whether the page is an offline preview. | 44 // Whether the page is an offline preview. |
| 36 bool is_offline_preview() const { return is_offline_preview_; } | 45 bool is_offline_preview() const { return offline_info_.is_offline_preview; } |
| 37 | 46 |
| 38 // Returns provisional offline page since actual navigation does not happen | 47 // Returns provisional offline page since actual navigation does not happen |
| 39 // during unit tests. | 48 // during unit tests. |
| 40 const OfflinePageItem* GetOfflinePageForTest() const; | 49 const OfflinePageItem* GetOfflinePageForTest() const; |
| 41 | 50 |
| 42 private: | 51 private: |
| 43 friend class content::WebContentsUserData<OfflinePageTabHelper>; | 52 friend class content::WebContentsUserData<OfflinePageTabHelper>; |
| 44 | 53 |
| 54 // Contains the info about the offline page being loaded. |
| 55 struct LoadedOfflinePageInfo { |
| 56 LoadedOfflinePageInfo(); |
| 57 ~LoadedOfflinePageInfo(); |
| 58 |
| 59 // The cached copy of OfflinePageItem. |
| 60 std::unique_ptr<OfflinePageItem> offline_page; |
| 61 |
| 62 // The offline header that is provided when offline page is loaded. |
| 63 OfflinePageHeader offline_header; |
| 64 |
| 65 // Whether the page is an offline preview. Offline page previews are shown |
| 66 // when a user's effective connection type is prohibitively slow. |
| 67 bool is_offline_preview; |
| 68 |
| 69 void Clear(); |
| 70 }; |
| 71 |
| 45 explicit OfflinePageTabHelper(content::WebContents* web_contents); | 72 explicit OfflinePageTabHelper(content::WebContents* web_contents); |
| 46 | 73 |
| 47 // Overridden from content::WebContentsObserver: | 74 // Overridden from content::WebContentsObserver: |
| 48 void DidStartNavigation( | 75 void DidStartNavigation( |
| 49 content::NavigationHandle* navigation_handle) override; | 76 content::NavigationHandle* navigation_handle) override; |
| 50 void DidFinishNavigation( | 77 void DidFinishNavigation( |
| 51 content::NavigationHandle* navigation_handle) override; | 78 content::NavigationHandle* navigation_handle) override; |
| 52 | 79 |
| 53 void SelectPageForOnlineURLDone(const OfflinePageItem* offline_page); | 80 void SelectPageForOnlineURLDone(const OfflinePageItem* offline_page); |
| 54 | 81 |
| 55 // The cached copy of OfflinePageItem if offline page is loaded for current | 82 // The provisional info about the offline page being loaded. This is set when |
| 56 // tab. This can be used to by the Tab to synchronously ask about the offline | 83 // the offline interceptor decides to serve the offline page and it will be |
| 84 // moved to |offline_info_| once the navigation is committed without error. |
| 85 LoadedOfflinePageInfo provisional_offline_info_; |
| 86 |
| 87 // The info about offline page being loaded. This is set from |
| 88 // |provisional_offline_info_| when the navigation is committed without error. |
| 89 // This can be used to by the Tab to synchronously ask about the offline |
| 57 // info. | 90 // info. |
| 58 std::unique_ptr<OfflinePageItem> offline_page_; | 91 LoadedOfflinePageInfo offline_info_; |
| 59 // Potential new offline page copy. This is reset to nullptr at the start | |
| 60 // of every navigation and set by network request interceptor. If the | |
| 61 // interceptor decided to not use offline page for the navigation or was not | |
| 62 // even invoked (as in case with fragment navigation), this stays nullptr. | |
| 63 std::unique_ptr<OfflinePageItem> provisional_offline_page_; | |
| 64 | 92 |
| 65 bool reloading_url_on_net_error_ = false; | 93 bool reloading_url_on_net_error_ = false; |
| 66 | 94 |
| 67 // Whether the page is an offline preview. Offline page previews are shown | |
| 68 // when a user's effective connection type is prohibitively slow. | |
| 69 bool is_offline_preview_; | |
| 70 | |
| 71 base::WeakPtrFactory<OfflinePageTabHelper> weak_ptr_factory_; | 95 base::WeakPtrFactory<OfflinePageTabHelper> weak_ptr_factory_; |
| 72 | 96 |
| 73 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelper); | 97 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelper); |
| 74 }; | 98 }; |
| 75 | 99 |
| 76 } // namespace offline_pages | 100 } // namespace offline_pages |
| 77 | 101 |
| 78 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ | 102 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ |
| OLD | NEW |