Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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/tab_android.h" | 5 #include "chrome/browser/android/tab_android.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_array.h" | 8 #include "base/android/jni_array.h" |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 | 280 |
| 281 // Only allows the browser-initiated navigation to use POST. | 281 // Only allows the browser-initiated navigation to use POST. |
| 282 if (params->uses_post && !params->is_renderer_initiated) { | 282 if (params->uses_post && !params->is_renderer_initiated) { |
| 283 load_url_params->load_type = | 283 load_url_params->load_type = |
| 284 NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST; | 284 NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST; |
| 285 load_url_params->browser_initiated_post_data = | 285 load_url_params->browser_initiated_post_data = |
| 286 params->browser_initiated_post_data; | 286 params->browser_initiated_post_data; |
| 287 } | 287 } |
| 288 } | 288 } |
| 289 | 289 |
| 290 bool TabAndroid::HasOfflinePages() const { | |
| 291 if (!offline_pages::IsOfflinePagesEnabled()) | |
| 292 return false; | |
| 293 offline_pages::OfflinePageModel* offline_page_model = | |
| 294 offline_pages::OfflinePageModelFactory::GetForBrowserContext( | |
| 295 GetProfile()); | |
| 296 return !offline_page_model->GetAllPages().empty(); | |
| 297 } | |
| 298 | |
| 299 void TabAndroid::ShowOfflinePages() { | |
| 300 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 301 Java_Tab_showOfflinePages(env, weak_java_tab_.get(env).obj()); | |
| 302 } | |
| 303 | |
| 304 void TabAndroid::SwapTabContents(content::WebContents* old_contents, | 290 void TabAndroid::SwapTabContents(content::WebContents* old_contents, |
| 305 content::WebContents* new_contents, | 291 content::WebContents* new_contents, |
| 306 bool did_start_load, | 292 bool did_start_load, |
| 307 bool did_finish_load) { | 293 bool did_finish_load) { |
| 308 JNIEnv* env = base::android::AttachCurrentThread(); | 294 JNIEnv* env = base::android::AttachCurrentThread(); |
| 309 Java_Tab_swapWebContents( | 295 Java_Tab_swapWebContents( |
| 310 env, | 296 env, |
| 311 weak_java_tab_.get(env).obj(), | 297 weak_java_tab_.get(env).obj(), |
| 312 new_contents->GetJavaWebContents().obj(), | 298 new_contents->GetJavaWebContents().obj(), |
| 313 did_start_load, | 299 did_start_load, |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 762 void TabAndroid::LoadOriginalImage(JNIEnv* env, jobject obj) { | 748 void TabAndroid::LoadOriginalImage(JNIEnv* env, jobject obj) { |
| 763 content::RenderFrameHost* render_frame_host = | 749 content::RenderFrameHost* render_frame_host = |
| 764 web_contents()->GetFocusedFrame(); | 750 web_contents()->GetFocusedFrame(); |
| 765 render_frame_host->Send(new ChromeViewMsg_RequestReloadImageForContextNode( | 751 render_frame_host->Send(new ChromeViewMsg_RequestReloadImageForContextNode( |
| 766 render_frame_host->GetRoutingID())); | 752 render_frame_host->GetRoutingID())); |
| 767 } | 753 } |
| 768 | 754 |
| 769 jlong TabAndroid::GetBookmarkId(JNIEnv* env, | 755 jlong TabAndroid::GetBookmarkId(JNIEnv* env, |
| 770 jobject obj, | 756 jobject obj, |
| 771 jboolean only_editable) { | 757 jboolean only_editable) { |
| 758 return GetBookmarkIdHelper(only_editable); | |
| 759 } | |
| 760 | |
| 761 int64_t TabAndroid::GetBookmarkIdHelper(bool only_editable) const { | |
| 772 GURL url = dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( | 762 GURL url = dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( |
| 773 web_contents()->GetURL()); | 763 web_contents()->GetURL()); |
| 774 Profile* profile = GetProfile(); | 764 Profile* profile = GetProfile(); |
| 775 | 765 |
| 776 // If the url points to an offline page, replace it with the original url. | 766 // If the url points to an offline page, replace it with the original url. |
| 777 const offline_pages::OfflinePageItem* offline_page = GetOfflinePage(url); | 767 const offline_pages::OfflinePageItem* offline_page = GetOfflinePage(url); |
| 778 if (offline_page) | 768 if (offline_page) |
| 779 url = offline_page->url; | 769 url = offline_page->url; |
| 780 | 770 |
| 781 // Get all the nodes for |url| and sort them by date added. | 771 // Get all the nodes for |url| and sort them by date added. |
| 782 std::vector<const bookmarks::BookmarkNode*> nodes; | 772 std::vector<const bookmarks::BookmarkNode*> nodes; |
| 783 bookmarks::ManagedBookmarkService* managed = | 773 bookmarks::ManagedBookmarkService* managed = |
| 784 ManagedBookmarkServiceFactory::GetForProfile(profile); | 774 ManagedBookmarkServiceFactory::GetForProfile(profile); |
| 785 bookmarks::BookmarkModel* model = | 775 bookmarks::BookmarkModel* model = |
| 786 BookmarkModelFactory::GetForProfile(profile); | 776 BookmarkModelFactory::GetForProfile(profile); |
| 787 model->GetNodesByURL(url, &nodes); | 777 model->GetNodesByURL(url, &nodes); |
| 788 std::sort(nodes.begin(), nodes.end(), &bookmarks::MoreRecentlyAdded); | 778 std::sort(nodes.begin(), nodes.end(), &bookmarks::MoreRecentlyAdded); |
| 789 | 779 |
| 790 // Return the first node matching the search criteria. | 780 // Return the first node matching the search criteria. |
| 791 for (size_t i = 0; i < nodes.size(); ++i) { | 781 for (size_t i = 0; i < nodes.size(); ++i) { |
| 792 if (only_editable && !managed->CanBeEditedByUser(nodes[i])) | 782 if (only_editable && !managed->CanBeEditedByUser(nodes[i])) |
| 793 continue; | 783 continue; |
| 794 return nodes[i]->id(); | 784 return nodes[i]->id(); |
| 795 } | 785 } |
| 796 | 786 |
| 797 return -1; | 787 return -1; |
| 798 } | 788 } |
| 799 | 789 |
| 800 jboolean TabAndroid::HasOfflineCopy(JNIEnv* env, jobject obj) { | 790 bool TabAndroid::HasOfflinePages() const { |
| 791 if (!offline_pages::IsOfflinePagesEnabled()) | |
| 792 return false; | |
| 793 offline_pages::OfflinePageModel* offline_page_model = | |
| 794 offline_pages::OfflinePageModelFactory::GetForBrowserContext( | |
| 795 GetProfile()); | |
| 796 return !offline_page_model->GetAllPages().empty(); | |
| 797 } | |
| 798 | |
| 799 bool TabAndroid::HasOfflineCopyForCurrentPage() const { | |
| 801 // Offline copy is only saved for a bookmarked page. | 800 // Offline copy is only saved for a bookmarked page. |
| 802 jlong bookmark_id = GetBookmarkId(env, obj, true); | 801 int64_t bookmark_id = GetBookmarkIdHelper(true); |
| 803 if (bookmark_id == -1) | 802 if (bookmark_id == -1) |
| 804 return false; | 803 return false; |
| 805 | 804 |
| 806 offline_pages::OfflinePageModel* offline_page_model = | 805 offline_pages::OfflinePageModel* offline_page_model = |
| 807 offline_pages::OfflinePageModelFactory::GetForBrowserContext( | 806 offline_pages::OfflinePageModelFactory::GetForBrowserContext( |
| 808 GetProfile()); | 807 GetProfile()); |
| 809 if (!offline_page_model) | 808 if (!offline_page_model) |
| 810 return false; | 809 return false; |
| 811 const offline_pages::OfflinePageItem* offline_page = | 810 const offline_pages::OfflinePageItem* offline_page = |
| 812 offline_page_model->GetPageByBookmarkId(bookmark_id); | 811 offline_page_model->GetPageByBookmarkId(bookmark_id); |
| 813 return offline_page && !offline_page->file_path.empty(); | 812 return offline_page && !offline_page->file_path.empty(); |
| 814 } | 813 } |
| 815 | 814 |
| 815 void TabAndroid::ShowOfflinePages() { | |
| 816 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 817 Java_Tab_showOfflinePages(env, weak_java_tab_.get(env).obj()); | |
| 818 } | |
| 819 | |
| 820 void TabAndroid::LoadOfflineCopy(const GURL& url) { | |
|
nasko
2015/11/13 23:55:13
This parameter doesn't seem to be used anywhere. W
jianli
2015/11/14 00:26:04
Added the validation at line 836.
| |
| 821 if (!offline_pages::IsOfflinePagesEnabled()) | |
| 822 return; | |
| 823 | |
| 824 // Offline copy is only saved for a bookmarked page. | |
| 825 int64_t bookmark_id = GetBookmarkIdHelper(true); | |
| 826 if (bookmark_id == -1) | |
| 827 return; | |
| 828 | |
| 829 offline_pages::OfflinePageModel* offline_page_model = | |
| 830 offline_pages::OfflinePageModelFactory::GetForBrowserContext( | |
| 831 GetProfile()); | |
| 832 if (!offline_page_model) | |
| 833 return; | |
|
nasko
2015/11/13 23:55:13
Empty line after the return statement and before t
jianli
2015/11/14 00:26:05
Done.
| |
| 834 const offline_pages::OfflinePageItem* offline_page = | |
| 835 offline_page_model->GetPageByBookmarkId(bookmark_id); | |
| 836 if (!offline_page) | |
| 837 return; | |
| 838 GURL offline_url = offline_page->GetOfflineURL(); | |
| 839 if (!offline_url.is_valid()) | |
| 840 return; | |
| 841 | |
| 842 content::NavigationController::LoadURLParams load_params(offline_url); | |
| 843 web_contents()->GetController().LoadURLWithParams(load_params); | |
| 844 } | |
| 845 | |
| 846 jboolean TabAndroid::HasOfflineCopy(JNIEnv* env, jobject obj) { | |
| 847 return HasOfflineCopyForCurrentPage(); | |
| 848 } | |
| 849 | |
| 816 jboolean TabAndroid::IsOfflinePage(JNIEnv* env, jobject obj) { | 850 jboolean TabAndroid::IsOfflinePage(JNIEnv* env, jobject obj) { |
| 817 GURL url = dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( | 851 GURL url = dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( |
| 818 web_contents()->GetURL()); | 852 web_contents()->GetURL()); |
| 819 return GetOfflinePage(url) != nullptr; | 853 return GetOfflinePage(url) != nullptr; |
| 820 } | 854 } |
| 821 | 855 |
| 822 ScopedJavaLocalRef<jstring> TabAndroid::GetOfflinePageOriginalUrl(JNIEnv* env, | 856 ScopedJavaLocalRef<jstring> TabAndroid::GetOfflinePageOriginalUrl(JNIEnv* env, |
| 823 jobject obj) { | 857 jobject obj) { |
| 824 GURL url = dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( | 858 GURL url = dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( |
| 825 web_contents()->GetURL()); | 859 web_contents()->GetURL()); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 923 static void Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 957 static void Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 924 TRACE_EVENT0("native", "TabAndroid::Init"); | 958 TRACE_EVENT0("native", "TabAndroid::Init"); |
| 925 // This will automatically bind to the Java object and pass ownership there. | 959 // This will automatically bind to the Java object and pass ownership there. |
| 926 new TabAndroid(env, obj); | 960 new TabAndroid(env, obj); |
| 927 } | 961 } |
| 928 | 962 |
| 929 // static | 963 // static |
| 930 bool TabAndroid::RegisterTabAndroid(JNIEnv* env) { | 964 bool TabAndroid::RegisterTabAndroid(JNIEnv* env) { |
| 931 return RegisterNativesImpl(env); | 965 return RegisterNativesImpl(env); |
| 932 } | 966 } |
| OLD | NEW |