Chromium Code Reviews| 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 #include "chrome/browser/android/offline_pages/downloads/offline_page_download_b ridge.h" | 5 #include "chrome/browser/android/offline_pages/downloads/offline_page_download_b ridge.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "base/guid.h" | 10 #include "base/guid.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "chrome/browser/android/offline_pages/downloads/offline_page_notificati on_bridge.h" | |
| 12 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 13 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 13 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 14 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 14 #include "chrome/browser/android/tab_android.h" | 15 #include "chrome/browser/android/tab_android.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/profiles/profile_android.h" | 17 #include "chrome/browser/profiles/profile_android.h" |
| 17 #include "components/offline_pages/client_namespace_constants.h" | 18 #include "components/offline_pages/client_namespace_constants.h" |
| 18 #include "components/offline_pages/downloads/download_ui_item.h" | 19 #include "components/offline_pages/downloads/download_ui_item.h" |
| 19 #include "components/offline_pages/offline_page_model.h" | 20 #include "components/offline_pages/offline_page_model.h" |
| 20 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 21 #include "jni/OfflinePageDownloadBridge_jni.h" | 22 #include "jni/OfflinePageDownloadBridge_jni.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 return; | 141 return; |
| 141 | 142 |
| 142 GURL url = web_contents->GetLastCommittedURL(); | 143 GURL url = web_contents->GetLastCommittedURL(); |
| 143 auto archiver = | 144 auto archiver = |
| 144 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); | 145 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); |
| 145 | 146 |
| 146 offline_pages::ClientId client_id; | 147 offline_pages::ClientId client_id; |
| 147 client_id.name_space = offline_pages::kDownloadNamespace; | 148 client_id.name_space = offline_pages::kDownloadNamespace; |
| 148 client_id.id = base::GenerateGUID(); | 149 client_id.id = base::GenerateGUID(); |
| 149 | 150 |
| 151 DownloadUIItem item; | |
| 152 item.guid = client_id.id; | |
| 153 item.url = url; | |
| 154 | |
| 155 OfflinePageNotificationBridge bridge; | |
| 156 bridge.NotifyDownloadProgress(item); | |
| 157 | |
| 150 offline_page_model->SavePage( | 158 offline_page_model->SavePage( |
| 151 url, client_id, 0ul, std::move(archiver), | 159 url, client_id, 0ul, std::move(archiver), |
| 152 base::Bind(&OfflinePageDownloadBridge::SavePageCallback, | 160 base::Bind(&OfflinePageDownloadBridge::SavePageCallback, |
| 153 weak_ptr_factory_.GetWeakPtr())); | 161 weak_ptr_factory_.GetWeakPtr(), item)); |
| 154 } | 162 } |
| 155 | 163 |
| 156 void OfflinePageDownloadBridge::ItemsLoaded() { | 164 void OfflinePageDownloadBridge::ItemsLoaded() { |
| 157 JNIEnv* env = AttachCurrentThread(); | 165 JNIEnv* env = AttachCurrentThread(); |
| 158 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 166 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
| 159 if (obj.is_null()) | 167 if (obj.is_null()) |
| 160 return; | 168 return; |
| 161 Java_OfflinePageDownloadBridge_downloadItemsLoaded(env, obj); | 169 Java_OfflinePageDownloadBridge_downloadItemsLoaded(env, obj); |
| 162 } | 170 } |
| 163 | 171 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 182 void OfflinePageDownloadBridge::ItemUpdated(const DownloadUIItem& item) { | 190 void OfflinePageDownloadBridge::ItemUpdated(const DownloadUIItem& item) { |
| 183 JNIEnv* env = AttachCurrentThread(); | 191 JNIEnv* env = AttachCurrentThread(); |
| 184 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 192 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
| 185 if (obj.is_null()) | 193 if (obj.is_null()) |
| 186 return; | 194 return; |
| 187 Java_OfflinePageDownloadBridge_downloadItemUpdated( | 195 Java_OfflinePageDownloadBridge_downloadItemUpdated( |
| 188 env, obj, ToJavaOfflinePageDownloadItem(env, item)); | 196 env, obj, ToJavaOfflinePageDownloadItem(env, item)); |
| 189 } | 197 } |
| 190 | 198 |
| 191 void OfflinePageDownloadBridge::SavePageCallback( | 199 void OfflinePageDownloadBridge::SavePageCallback( |
| 192 OfflinePageModel::SavePageResult result, int64_t offline_id) { | 200 const DownloadUIItem& item, |
| 193 // TODO(dimich): Consider adding UMA here. | 201 OfflinePageModel::SavePageResult result, |
| 202 int64_t offline_id) { | |
| 203 OfflinePageNotificationBridge notification_bridge; | |
| 204 if (result == SavePageResult::SUCCESS) { | |
|
qinmin
2016/08/18 23:46:59
no need for {} for if-else statements that has onl
fgorski
2016/08/19 17:03:24
Done.
| |
| 205 notification_bridge.NotifyDownloadSuccessful(item); | |
| 206 } else { | |
| 207 notification_bridge.NotifyDownloadFailed(item); | |
| 208 } | |
| 194 } | 209 } |
| 195 | 210 |
| 196 static jlong Init(JNIEnv* env, | 211 static jlong Init(JNIEnv* env, |
| 197 const JavaParamRef<jobject>& obj, | 212 const JavaParamRef<jobject>& obj, |
| 198 const JavaParamRef<jobject>& j_profile) { | 213 const JavaParamRef<jobject>& j_profile) { |
| 199 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 214 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
| 200 OfflinePageModel* offline_page_model = | 215 OfflinePageModel* offline_page_model = |
| 201 OfflinePageModelFactory::GetForBrowserContext(profile); | 216 OfflinePageModelFactory::GetForBrowserContext(profile); |
| 202 | 217 |
| 203 DownloadUIAdapter* adapter = | 218 DownloadUIAdapter* adapter = |
| 204 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); | 219 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); |
| 205 | 220 |
| 206 return reinterpret_cast<jlong>( | 221 return reinterpret_cast<jlong>( |
| 207 new OfflinePageDownloadBridge(env, obj, adapter)); | 222 new OfflinePageDownloadBridge(env, obj, adapter)); |
| 208 } | 223 } |
| 209 | 224 |
| 210 } // namespace android | 225 } // namespace android |
| 211 } // namespace offline_pages | 226 } // namespace offline_pages |
| OLD | NEW |