| 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_bridge.h" | 5 #include "chrome/browser/android/offline_pages/offline_page_bridge.h" |
| 6 | 6 |
| 7 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 10 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 10 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 offline_page.bookmark_id, | 45 offline_page.bookmark_id, |
| 46 ConvertUTF8ToJavaString(env, offline_page.GetOfflineURL().spec()).obj(), | 46 ConvertUTF8ToJavaString(env, offline_page.GetOfflineURL().spec()).obj(), |
| 47 offline_page.file_size); | 47 offline_page.file_size); |
| 48 } | 48 } |
| 49 | 49 |
| 50 Java_OfflinePageCallback_onLoadAllPagesDone(env, j_callback_ptr->obj(), | 50 Java_OfflinePageCallback_onLoadAllPagesDone(env, j_callback_ptr->obj(), |
| 51 static_cast<int>(result), | 51 static_cast<int>(result), |
| 52 j_result_ptr->obj()); | 52 j_result_ptr->obj()); |
| 53 } | 53 } |
| 54 | 54 |
| 55 #if !defined(OS_ANDROID) |
| 55 void SavePageCallback(ScopedJavaGlobalRef<jobject>* j_callback_obj, | 56 void SavePageCallback(ScopedJavaGlobalRef<jobject>* j_callback_obj, |
| 56 const GURL& url, | 57 const GURL& url, |
| 57 OfflinePageModel::SavePageResult result) { | 58 OfflinePageModel::SavePageResult result) { |
| 58 JNIEnv* env = base::android::AttachCurrentThread(); | 59 JNIEnv* env = base::android::AttachCurrentThread(); |
| 59 | 60 |
| 60 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback_ptr(j_callback_obj); | 61 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback_ptr(j_callback_obj); |
| 61 | 62 |
| 62 Java_OfflinePageCallback_onSavePageDone( | 63 Java_OfflinePageCallback_onSavePageDone( |
| 63 env, j_callback_ptr->obj(), static_cast<int>(result), | 64 env, j_callback_ptr->obj(), static_cast<int>(result), |
| 64 ConvertUTF8ToJavaString(env, url.spec()).obj()); | 65 ConvertUTF8ToJavaString(env, url.spec()).obj()); |
| 65 } | 66 } |
| 67 #endif |
| 66 | 68 |
| 67 } // namespace | 69 } // namespace |
| 68 | 70 |
| 69 static jboolean IsOfflinePagesEnabled(JNIEnv* env, jclass clazz) { | 71 static jboolean IsOfflinePagesEnabled(JNIEnv* env, jclass clazz) { |
| 70 return offline_pages::IsOfflinePagesEnabled(); | 72 return offline_pages::IsOfflinePagesEnabled(); |
| 71 } | 73 } |
| 72 | 74 |
| 73 OfflinePageBridge::OfflinePageBridge(JNIEnv* env, | 75 OfflinePageBridge::OfflinePageBridge(JNIEnv* env, |
| 74 jobject obj, | 76 jobject obj, |
| 75 content::BrowserContext* browser_context) | 77 content::BrowserContext* browser_context) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 101 offline_page_model_->LoadAllPages(base::Bind(&LoadAllPagesCallback, | 103 offline_page_model_->LoadAllPages(base::Bind(&LoadAllPagesCallback, |
| 102 j_callback_obj_ptr.release(), | 104 j_callback_obj_ptr.release(), |
| 103 j_result_obj_ptr.release())); | 105 j_result_obj_ptr.release())); |
| 104 } | 106 } |
| 105 | 107 |
| 106 void OfflinePageBridge::SavePage(JNIEnv* env, | 108 void OfflinePageBridge::SavePage(JNIEnv* env, |
| 107 jobject obj, | 109 jobject obj, |
| 108 jobject j_callback_obj, | 110 jobject j_callback_obj, |
| 109 jobject j_web_contents, | 111 jobject j_web_contents, |
| 110 jlong bookmark_id) { | 112 jlong bookmark_id) { |
| 113 #if !defined(USE_AURA) |
| 111 DCHECK(j_callback_obj); | 114 DCHECK(j_callback_obj); |
| 112 DCHECK(j_web_contents); | 115 DCHECK(j_web_contents); |
| 113 | 116 |
| 114 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback_obj_ptr( | 117 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback_obj_ptr( |
| 115 new ScopedJavaGlobalRef<jobject>()); | 118 new ScopedJavaGlobalRef<jobject>()); |
| 116 j_callback_obj_ptr->Reset(env, j_callback_obj); | 119 j_callback_obj_ptr->Reset(env, j_callback_obj); |
| 117 | 120 |
| 118 content::WebContents* web_contents = | 121 content::WebContents* web_contents = |
| 119 content::WebContents::FromJavaWebContents(j_web_contents); | 122 content::WebContents::FromJavaWebContents(j_web_contents); |
| 120 GURL url(web_contents->GetLastCommittedURL()); | 123 GURL url(web_contents->GetLastCommittedURL()); |
| 121 | 124 |
| 122 scoped_ptr<OfflinePageArchiver> archiver( | 125 scoped_ptr<OfflinePageArchiver> archiver( |
| 123 new OfflinePageMHTMLArchiver(web_contents, GetDownloadsPath())); | 126 new OfflinePageMHTMLArchiver(web_contents, GetDownloadsPath())); |
| 124 | 127 |
| 125 offline_page_model_->SavePage( | 128 offline_page_model_->SavePage( |
| 126 url, bookmark_id, archiver.Pass(), | 129 url, bookmark_id, archiver.Pass(), |
| 127 base::Bind(&SavePageCallback, j_callback_obj_ptr.release(), url)); | 130 base::Bind(&SavePageCallback, j_callback_obj_ptr.release(), url)); |
| 131 #endif |
| 128 } | 132 } |
| 129 | 133 |
| 130 base::FilePath OfflinePageBridge::GetDownloadsPath() const { | 134 base::FilePath OfflinePageBridge::GetDownloadsPath() const { |
| 131 content::DownloadManager* manager = | 135 content::DownloadManager* manager = |
| 132 content::BrowserContext::GetDownloadManager(browser_context_); | 136 content::BrowserContext::GetDownloadManager(browser_context_); |
| 133 if (!manager) { | 137 if (!manager) { |
| 134 DVLOG(1) << "No download manager available in offline page bridge"; | 138 DVLOG(1) << "No download manager available in offline page bridge"; |
| 135 return base::FilePath(); | 139 return base::FilePath(); |
| 136 } | 140 } |
| 137 | 141 |
| 138 return DownloadPrefs::FromDownloadManager(manager)->DownloadPath(); | 142 return DownloadPrefs::FromDownloadManager(manager)->DownloadPath(); |
| 139 } | 143 } |
| 140 | 144 |
| 141 static jlong Init(JNIEnv* env, jobject obj, jobject j_profile) { | 145 static jlong Init(JNIEnv* env, jobject obj, jobject j_profile) { |
| 142 return reinterpret_cast<jlong>(new OfflinePageBridge( | 146 return reinterpret_cast<jlong>(new OfflinePageBridge( |
| 143 env, obj, ProfileAndroid::FromProfileAndroid(j_profile))); | 147 env, obj, ProfileAndroid::FromProfileAndroid(j_profile))); |
| 144 } | 148 } |
| 145 | 149 |
| 146 bool RegisterOfflinePageBridge(JNIEnv* env) { | 150 bool RegisterOfflinePageBridge(JNIEnv* env) { |
| 147 return RegisterNativesImpl(env); | 151 return RegisterNativesImpl(env); |
| 148 } | 152 } |
| 149 | 153 |
| 150 } // namespace android | 154 } // namespace android |
| 151 } // namespace offline_pages | 155 } // namespace offline_pages |
| OLD | NEW |