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" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | |
| 10 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 13 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 14 #include "chrome/browser/android/tab_android.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/profiles/profile_android.h" | 16 #include "chrome/browser/profiles/profile_android.h" |
| 17 #include "components/offline_pages/client_namespace_constants.h" | |
| 13 #include "components/offline_pages/downloads/download_ui_item.h" | 18 #include "components/offline_pages/downloads/download_ui_item.h" |
| 14 #include "components/offline_pages/offline_page_model.h" | 19 #include "components/offline_pages/offline_page_model.h" |
| 20 #include "content/public/browser/web_contents.h" | |
| 15 #include "jni/OfflinePageDownloadBridge_jni.h" | 21 #include "jni/OfflinePageDownloadBridge_jni.h" |
| 16 #include "net/base/filename_util.h" | 22 #include "net/base/filename_util.h" |
| 17 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 18 | 24 |
| 19 using base::android::AttachCurrentThread; | 25 using base::android::AttachCurrentThread; |
| 20 using base::android::ConvertJavaStringToUTF8; | 26 using base::android::ConvertJavaStringToUTF8; |
| 21 using base::android::ConvertUTF8ToJavaString; | 27 using base::android::ConvertUTF8ToJavaString; |
| 22 using base::android::JavaParamRef; | 28 using base::android::JavaParamRef; |
| 23 using base::android::ScopedJavaGlobalRef; | 29 using base::android::ScopedJavaGlobalRef; |
| 24 using base::android::ScopedJavaLocalRef; | 30 using base::android::ScopedJavaLocalRef; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 50 ConvertUTF8ToJavaString(env, item.target_path.value()).obj(), | 56 ConvertUTF8ToJavaString(env, item.target_path.value()).obj(), |
| 51 item.start_time.ToJavaTime(), item.total_bytes); | 57 item.start_time.ToJavaTime(), item.total_bytes); |
| 52 } | 58 } |
| 53 } // namespace | 59 } // namespace |
| 54 | 60 |
| 55 OfflinePageDownloadBridge::OfflinePageDownloadBridge( | 61 OfflinePageDownloadBridge::OfflinePageDownloadBridge( |
| 56 JNIEnv* env, | 62 JNIEnv* env, |
| 57 const JavaParamRef<jobject>& obj, | 63 const JavaParamRef<jobject>& obj, |
| 58 DownloadUIAdapter* download_ui_adapter) | 64 DownloadUIAdapter* download_ui_adapter) |
| 59 : weak_java_ref_(env, obj), | 65 : weak_java_ref_(env, obj), |
| 60 download_ui_adapter_(download_ui_adapter) { | 66 download_ui_adapter_(download_ui_adapter), |
| 67 weak_ptr_factory_(this) { | |
| 61 DCHECK(download_ui_adapter_); | 68 DCHECK(download_ui_adapter_); |
| 62 download_ui_adapter_->AddObserver(this); | 69 download_ui_adapter_->AddObserver(this); |
| 63 } | 70 } |
| 64 | 71 |
| 65 OfflinePageDownloadBridge::~OfflinePageDownloadBridge() {} | 72 OfflinePageDownloadBridge::~OfflinePageDownloadBridge() {} |
| 66 | 73 |
| 67 // static | 74 // static |
| 68 bool OfflinePageDownloadBridge::Register(JNIEnv* env) { | 75 bool OfflinePageDownloadBridge::Register(JNIEnv* env) { |
| 69 return RegisterNativesImpl(env); | 76 return RegisterNativesImpl(env); |
| 70 } | 77 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 JNIEnv* env, | 114 JNIEnv* env, |
| 108 const JavaParamRef<jobject>& obj, | 115 const JavaParamRef<jobject>& obj, |
| 109 const JavaParamRef<jstring>& j_guid) { | 116 const JavaParamRef<jstring>& j_guid) { |
| 110 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | 117 std::string guid = ConvertJavaStringToUTF8(env, j_guid); |
| 111 GURL url = download_ui_adapter_->GetOfflineUrlByGuid(guid); | 118 GURL url = download_ui_adapter_->GetOfflineUrlByGuid(guid); |
| 112 if (!url.is_valid()) | 119 if (!url.is_valid()) |
| 113 return ScopedJavaLocalRef<jstring>(); | 120 return ScopedJavaLocalRef<jstring>(); |
| 114 return ConvertUTF8ToJavaString(env, url.spec()); | 121 return ConvertUTF8ToJavaString(env, url.spec()); |
| 115 } | 122 } |
| 116 | 123 |
| 124 void OfflinePageDownloadBridge::StartDownload( | |
| 125 JNIEnv* env, | |
| 126 const JavaParamRef<jobject>& obj, | |
| 127 const JavaParamRef<jobject>& j_tab) { | |
| 128 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); | |
| 129 if (!tab) | |
| 130 return; | |
| 131 | |
| 132 content::WebContents* web_contents = tab->web_contents(); | |
| 133 if (!web_contents) | |
| 134 return; | |
| 135 | |
| 136 offline_pages::OfflinePageModel* offline_page_model = | |
| 137 OfflinePageModelFactory::GetForBrowserContext( | |
| 138 tab->GetProfile()->GetOriginalProfile()); | |
| 139 if (!offline_page_model) | |
| 140 return; | |
| 141 | |
| 142 GURL url = web_contents->GetLastCommittedURL(); | |
| 143 auto archiver = | |
| 144 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); | |
| 145 | |
| 146 offline_pages::ClientId client_id; | |
| 147 client_id.name_space = offline_pages::kDownloadNamespace; | |
| 148 client_id.id = base::GenerateGUID(); | |
| 149 | |
| 150 offline_page_model->SavePage( | |
| 151 url, client_id, std::move(archiver), | |
|
Pete Williamson
2016/08/15 20:52:58
FYI - This line has an impending merge conflict wi
| |
| 152 base::Bind(&OfflinePageDownloadBridge::SavePageCallback, | |
| 153 weak_ptr_factory_.GetWeakPtr())); | |
| 154 } | |
| 155 | |
| 117 void OfflinePageDownloadBridge::ItemsLoaded() { | 156 void OfflinePageDownloadBridge::ItemsLoaded() { |
| 118 JNIEnv* env = AttachCurrentThread(); | 157 JNIEnv* env = AttachCurrentThread(); |
| 119 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 158 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
| 120 if (obj.is_null()) | 159 if (obj.is_null()) |
| 121 return; | 160 return; |
| 122 Java_OfflinePageDownloadBridge_downloadItemsLoaded(env, obj.obj()); | 161 Java_OfflinePageDownloadBridge_downloadItemsLoaded(env, obj.obj()); |
| 123 } | 162 } |
| 124 | 163 |
| 125 void OfflinePageDownloadBridge::ItemAdded(const DownloadUIItem& item) { | 164 void OfflinePageDownloadBridge::ItemAdded(const DownloadUIItem& item) { |
| 126 JNIEnv* env = AttachCurrentThread(); | 165 JNIEnv* env = AttachCurrentThread(); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 142 | 181 |
| 143 void OfflinePageDownloadBridge::ItemUpdated(const DownloadUIItem& item) { | 182 void OfflinePageDownloadBridge::ItemUpdated(const DownloadUIItem& item) { |
| 144 JNIEnv* env = AttachCurrentThread(); | 183 JNIEnv* env = AttachCurrentThread(); |
| 145 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 184 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
| 146 if (obj.is_null()) | 185 if (obj.is_null()) |
| 147 return; | 186 return; |
| 148 Java_OfflinePageDownloadBridge_downloadItemUpdated( | 187 Java_OfflinePageDownloadBridge_downloadItemUpdated( |
| 149 env, obj.obj(), ToJavaOfflinePageDownloadItem(env, item).obj()); | 188 env, obj.obj(), ToJavaOfflinePageDownloadItem(env, item).obj()); |
| 150 } | 189 } |
| 151 | 190 |
| 191 void OfflinePageDownloadBridge::SavePageCallback( | |
| 192 OfflinePageModel::SavePageResult result, int64_t offline_id) { | |
| 193 // TODO(dimich): Consider adding UMA here. | |
| 194 } | |
| 195 | |
| 152 static jlong Init(JNIEnv* env, | 196 static jlong Init(JNIEnv* env, |
| 153 const JavaParamRef<jobject>& obj, | 197 const JavaParamRef<jobject>& obj, |
| 154 const JavaParamRef<jobject>& j_profile) { | 198 const JavaParamRef<jobject>& j_profile) { |
| 155 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 199 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
| 156 OfflinePageModel* offline_page_model = | 200 OfflinePageModel* offline_page_model = |
| 157 OfflinePageModelFactory::GetForBrowserContext(profile); | 201 OfflinePageModelFactory::GetForBrowserContext(profile); |
| 158 | 202 |
| 159 DownloadUIAdapter* adapter = | 203 DownloadUIAdapter* adapter = |
| 160 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); | 204 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); |
| 161 | 205 |
| 162 return reinterpret_cast<jlong>( | 206 return reinterpret_cast<jlong>( |
| 163 new OfflinePageDownloadBridge(env, obj, adapter)); | 207 new OfflinePageDownloadBridge(env, obj, adapter)); |
| 164 } | 208 } |
| 165 | 209 |
| 166 } // namespace android | 210 } // namespace android |
| 167 } // namespace offline_pages | 211 } // namespace offline_pages |
| OLD | NEW |