| OLD | NEW |
| 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 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | 5 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 13 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 14 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 14 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 15 #include "components/offline_pages/offline_page_feature.h" | 15 #include "components/offline_pages/offline_page_feature.h" |
| 16 #include "components/offline_pages/offline_page_item.h" | 16 #include "components/offline_pages/offline_page_item.h" |
| 17 #include "components/offline_pages/offline_page_model.h" | 17 #include "components/offline_pages/offline_page_model.h" |
| 18 #include "content/public/browser/browser_context.h" | 18 #include "content/public/browser/browser_context.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 20 | 21 |
| 21 namespace offline_pages { | 22 namespace offline_pages { |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 25 const char kWebContentsUserDataKey[] = "OfflinePageForTab"; |
| 26 |
| 24 // Returns an offline page originated from the |online_url|. | 27 // Returns an offline page originated from the |online_url|. |
| 25 const OfflinePageItem* MaybeGetBestOfflinePageForOnlineURL( | 28 const OfflinePageItem* MaybeGetBestOfflinePageForOnlineURL( |
| 26 content::BrowserContext* browser_context, | 29 content::BrowserContext* browser_context, |
| 27 const GURL& online_url) { | 30 const GURL& online_url) { |
| 28 DCHECK(browser_context); | 31 DCHECK(browser_context); |
| 29 | 32 |
| 30 if (!IsOfflinePagesEnabled()) | 33 if (!IsOfflinePagesEnabled()) |
| 31 return nullptr; | 34 return nullptr; |
| 32 | 35 |
| 33 OfflinePageModel* offline_page_model = | 36 OfflinePageModel* offline_page_model = |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 GetOfflinePageForOfflineURL(browser_context, offline_url); | 175 GetOfflinePageForOfflineURL(browser_context, offline_url); |
| 173 if (!offline_page) | 176 if (!offline_page) |
| 174 return; | 177 return; |
| 175 | 178 |
| 176 OfflinePageModel* offline_page_model = | 179 OfflinePageModel* offline_page_model = |
| 177 OfflinePageModelFactory::GetForBrowserContext(browser_context); | 180 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 178 DCHECK(offline_page_model); | 181 DCHECK(offline_page_model); |
| 179 offline_page_model->MarkPageAccessed(offline_page->offline_id); | 182 offline_page_model->MarkPageAccessed(offline_page->offline_id); |
| 180 } | 183 } |
| 181 | 184 |
| 185 const OfflinePageItem* OfflinePageUtils::GetCurrentOfflinePage( |
| 186 content::WebContents* web_contents) { |
| 187 return static_cast<const OfflinePageItem*>( |
| 188 web_contents->GetUserData(kWebContentsUserDataKey)); |
| 189 } |
| 190 |
| 191 void OfflinePageUtils::SetCurrentOfflinePage( |
| 192 content::WebContents* web_contents, |
| 193 const OfflinePageItem* offline_page) { |
| 194 if (!offline_page) { |
| 195 web_contents->RemoveUserData(kWebContentsUserDataKey); |
| 196 return; |
| 197 } |
| 198 |
| 199 // |SetUserData| takes ownership of the data object, so we need to make a copy |
| 200 // here. |
| 201 web_contents->SetUserData(kWebContentsUserDataKey, |
| 202 new OfflinePageItem(*offline_page)); |
| 203 } |
| 182 } // namespace offline_pages | 204 } // namespace offline_pages |
| OLD | NEW |