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_array.h" |
7 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/basictypes.h" |
8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
9 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
10 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 12 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
11 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 13 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
12 #include "chrome/browser/download/download_prefs.h" | 14 #include "chrome/browser/download/download_prefs.h" |
13 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/profiles/profile_android.h" | 16 #include "chrome/browser/profiles/profile_android.h" |
15 #include "components/offline_pages/offline_page_feature.h" | 17 #include "components/offline_pages/offline_page_feature.h" |
16 #include "components/offline_pages/offline_page_item.h" | 18 #include "components/offline_pages/offline_page_item.h" |
17 #include "components/offline_pages/offline_page_model.h" | 19 #include "components/offline_pages/offline_page_model.h" |
18 #include "content/public/browser/browser_context.h" | 20 #include "content/public/browser/browser_context.h" |
19 #include "content/public/browser/download_manager.h" | 21 #include "content/public/browser/download_manager.h" |
20 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
21 #include "jni/OfflinePageBridge_jni.h" | 23 #include "jni/OfflinePageBridge_jni.h" |
22 #include "net/base/filename_util.h" | 24 #include "net/base/filename_util.h" |
23 | 25 |
24 using base::android::ConvertJavaStringToUTF8; | 26 using base::android::ConvertJavaStringToUTF8; |
25 using base::android::ConvertUTF16ToJavaString; | 27 using base::android::ConvertUTF16ToJavaString; |
26 using base::android::ConvertUTF8ToJavaString; | 28 using base::android::ConvertUTF8ToJavaString; |
27 using base::android::ScopedJavaGlobalRef; | 29 using base::android::ScopedJavaGlobalRef; |
28 using base::android::ScopedJavaLocalRef; | 30 using base::android::ScopedJavaLocalRef; |
29 | 31 |
30 namespace offline_pages { | 32 namespace offline_pages { |
31 namespace android { | 33 namespace android { |
32 | 34 |
33 namespace { | 35 namespace { |
34 | 36 |
35 void SavePageCallback(ScopedJavaGlobalRef<jobject>* j_callback_obj, | 37 void SavePageCallback(const ScopedJavaGlobalRef<jobject>& j_callback_obj, |
36 const GURL& url, | 38 const GURL& url, |
37 OfflinePageModel::SavePageResult result) { | 39 OfflinePageModel::SavePageResult result) { |
38 JNIEnv* env = base::android::AttachCurrentThread(); | 40 JNIEnv* env = base::android::AttachCurrentThread(); |
39 | 41 |
40 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback_ptr(j_callback_obj); | |
41 | |
42 Java_SavePageCallback_onSavePageDone( | 42 Java_SavePageCallback_onSavePageDone( |
43 env, j_callback_ptr->obj(), static_cast<int>(result), | 43 env, j_callback_obj.obj(), static_cast<int>(result), |
44 ConvertUTF8ToJavaString(env, url.spec()).obj()); | 44 ConvertUTF8ToJavaString(env, url.spec()).obj()); |
45 } | 45 } |
46 | 46 |
47 void DeletePageCallback(ScopedJavaGlobalRef<jobject>* j_callback_obj, | 47 void DeletePageCallback(const ScopedJavaGlobalRef<jobject>& j_callback_obj, |
48 OfflinePageModel::DeletePageResult result) { | 48 OfflinePageModel::DeletePageResult result) { |
49 JNIEnv* env = base::android::AttachCurrentThread(); | 49 JNIEnv* env = base::android::AttachCurrentThread(); |
50 | 50 |
51 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback_ptr(j_callback_obj); | 51 Java_DeletePageCallback_onDeletePageDone( |
| 52 env, j_callback_obj.obj(), static_cast<int>(result)); |
| 53 } |
52 | 54 |
53 Java_DeletePageCallback_onDeletePageDone( | 55 void ToJavaOfflinePageList(JNIEnv* env, |
54 env, j_callback_ptr->obj(), static_cast<int>(result)); | 56 jobject j_result_obj, |
| 57 const std::vector<OfflinePageItem>& offline_pages) { |
| 58 for (const OfflinePageItem& offline_page : offline_pages) { |
| 59 Java_OfflinePageBridge_createOfflinePageAndAddToList( |
| 60 env, j_result_obj, |
| 61 ConvertUTF8ToJavaString(env, offline_page.url.spec()).obj(), |
| 62 offline_page.bookmark_id, |
| 63 ConvertUTF8ToJavaString(env, offline_page.GetOfflineURL().spec()).obj(), |
| 64 offline_page.file_size); |
| 65 } |
55 } | 66 } |
56 | 67 |
57 } // namespace | 68 } // namespace |
58 | 69 |
59 static jboolean IsOfflinePagesEnabled(JNIEnv* env, jclass clazz) { | 70 static jboolean IsOfflinePagesEnabled(JNIEnv* env, jclass clazz) { |
60 return offline_pages::IsOfflinePagesEnabled(); | 71 return offline_pages::IsOfflinePagesEnabled(); |
61 } | 72 } |
62 | 73 |
63 OfflinePageBridge::OfflinePageBridge(JNIEnv* env, | 74 OfflinePageBridge::OfflinePageBridge(JNIEnv* env, |
64 jobject obj, | 75 jobject obj, |
(...skipping 16 matching lines...) Expand all Loading... |
81 NotifyIfDoneLoading(); | 92 NotifyIfDoneLoading(); |
82 } | 93 } |
83 | 94 |
84 void OfflinePageBridge::GetAllPages(JNIEnv* env, | 95 void OfflinePageBridge::GetAllPages(JNIEnv* env, |
85 jobject obj, | 96 jobject obj, |
86 jobject j_result_obj) { | 97 jobject j_result_obj) { |
87 DCHECK(offline_page_model_->is_loaded()); | 98 DCHECK(offline_page_model_->is_loaded()); |
88 DCHECK(j_result_obj); | 99 DCHECK(j_result_obj); |
89 const std::vector<OfflinePageItem>& offline_pages = | 100 const std::vector<OfflinePageItem>& offline_pages = |
90 offline_page_model_->GetAllPages(); | 101 offline_page_model_->GetAllPages(); |
| 102 ToJavaOfflinePageList(env, j_result_obj, offline_pages); |
| 103 } |
91 | 104 |
92 for (const OfflinePageItem& offline_page : offline_pages) { | 105 void OfflinePageBridge::GetPagesToCleanUp(JNIEnv* env, |
93 Java_OfflinePageBridge_createOfflinePageAndAddToList( | 106 jobject obj, |
94 env, j_result_obj, | 107 jobject j_result_obj) { |
95 ConvertUTF8ToJavaString(env, offline_page.url.spec()).obj(), | 108 DCHECK(offline_page_model_->is_loaded()); |
96 offline_page.bookmark_id, | 109 DCHECK(j_result_obj); |
97 ConvertUTF8ToJavaString(env, offline_page.GetOfflineURL().spec()).obj(), | 110 const std::vector<OfflinePageItem>& offline_pages = |
98 offline_page.file_size); | 111 offline_page_model_->GetPagesToCleanUp(); |
99 } | 112 ToJavaOfflinePageList(env, j_result_obj, offline_pages); |
100 } | 113 } |
101 | 114 |
102 ScopedJavaLocalRef<jobject> OfflinePageBridge::GetPageByBookmarkId( | 115 ScopedJavaLocalRef<jobject> OfflinePageBridge::GetPageByBookmarkId( |
103 JNIEnv* env, | 116 JNIEnv* env, |
104 jobject obj, | 117 jobject obj, |
105 jlong bookmark_id) { | 118 jlong bookmark_id) { |
106 OfflinePageItem offline_page; | 119 OfflinePageItem offline_page; |
107 if (!offline_page_model_->GetPageByBookmarkId(bookmark_id, &offline_page)) | 120 if (!offline_page_model_->GetPageByBookmarkId(bookmark_id, &offline_page)) |
108 return ScopedJavaLocalRef<jobject>(); | 121 return ScopedJavaLocalRef<jobject>(); |
109 | 122 |
110 return Java_OfflinePageBridge_createOfflinePageItem( | 123 return Java_OfflinePageBridge_createOfflinePageItem( |
111 env, ConvertUTF8ToJavaString(env, offline_page.url.spec()).obj(), | 124 env, ConvertUTF8ToJavaString(env, offline_page.url.spec()).obj(), |
112 offline_page.bookmark_id, | 125 offline_page.bookmark_id, |
113 ConvertUTF8ToJavaString(env, offline_page.GetOfflineURL().spec()).obj(), | 126 ConvertUTF8ToJavaString(env, offline_page.GetOfflineURL().spec()).obj(), |
114 offline_page.file_size); | 127 offline_page.file_size); |
115 } | 128 } |
116 | 129 |
117 void OfflinePageBridge::SavePage(JNIEnv* env, | 130 void OfflinePageBridge::SavePage(JNIEnv* env, |
118 jobject obj, | 131 jobject obj, |
119 jobject j_callback_obj, | 132 jobject j_callback_obj, |
120 jobject j_web_contents, | 133 jobject j_web_contents, |
121 jlong bookmark_id) { | 134 jlong bookmark_id) { |
122 DCHECK(j_callback_obj); | 135 DCHECK(j_callback_obj); |
123 DCHECK(j_web_contents); | 136 DCHECK(j_web_contents); |
124 | 137 |
125 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback_obj_ptr( | 138 ScopedJavaGlobalRef<jobject> j_callback_ref; |
126 new ScopedJavaGlobalRef<jobject>()); | 139 j_callback_ref.Reset(env, j_callback_obj); |
127 j_callback_obj_ptr->Reset(env, j_callback_obj); | |
128 | 140 |
129 content::WebContents* web_contents = | 141 content::WebContents* web_contents = |
130 content::WebContents::FromJavaWebContents(j_web_contents); | 142 content::WebContents::FromJavaWebContents(j_web_contents); |
131 GURL url(web_contents->GetLastCommittedURL()); | 143 GURL url(web_contents->GetLastCommittedURL()); |
132 | 144 |
133 scoped_ptr<OfflinePageArchiver> archiver( | 145 scoped_ptr<OfflinePageArchiver> archiver( |
134 new OfflinePageMHTMLArchiver( | 146 new OfflinePageMHTMLArchiver( |
135 web_contents, GetDownloadsPath(browser_context_))); | 147 web_contents, GetDownloadsPath(browser_context_))); |
136 | 148 |
137 offline_page_model_->SavePage( | 149 offline_page_model_->SavePage( |
138 url, bookmark_id, archiver.Pass(), | 150 url, bookmark_id, archiver.Pass(), |
139 base::Bind(&SavePageCallback, j_callback_obj_ptr.release(), url)); | 151 base::Bind(&SavePageCallback, j_callback_ref, url)); |
140 } | 152 } |
141 | 153 |
142 void OfflinePageBridge::DeletePage(JNIEnv* env, | 154 void OfflinePageBridge::DeletePage(JNIEnv* env, |
143 jobject obj, | 155 jobject obj, |
144 jobject j_callback_obj, | 156 jobject j_callback_obj, |
145 jlong bookmark_id) { | 157 jlong bookmark_id) { |
146 DCHECK(j_callback_obj); | 158 DCHECK(j_callback_obj); |
147 | 159 |
148 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback_obj_ptr( | 160 ScopedJavaGlobalRef<jobject> j_callback_ref; |
149 new ScopedJavaGlobalRef<jobject>()); | 161 j_callback_ref.Reset(env, j_callback_obj); |
150 j_callback_obj_ptr->Reset(env, j_callback_obj); | |
151 | 162 |
152 offline_page_model_->DeletePageByBookmarkId(bookmark_id, base::Bind( | 163 offline_page_model_->DeletePageByBookmarkId(bookmark_id, base::Bind( |
153 &DeletePageCallback, j_callback_obj_ptr.release())); | 164 &DeletePageCallback, j_callback_ref)); |
| 165 } |
| 166 |
| 167 void OfflinePageBridge::DeletePages(JNIEnv* env, |
| 168 jobject obj, |
| 169 jobject j_callback_obj, |
| 170 jlongArray bookmark_ids_array) { |
| 171 DCHECK(j_callback_obj); |
| 172 |
| 173 ScopedJavaGlobalRef<jobject> j_callback_ref; |
| 174 j_callback_ref.Reset(env, j_callback_obj); |
| 175 |
| 176 std::vector<int64> bookmark_ids; |
| 177 base::android::JavaLongArrayToInt64Vector(env, bookmark_ids_array, |
| 178 &bookmark_ids); |
| 179 |
| 180 offline_page_model_->DeletePagesByBookmarkId( |
| 181 bookmark_ids, |
| 182 base::Bind(&DeletePageCallback, j_callback_ref)); |
154 } | 183 } |
155 | 184 |
156 void OfflinePageBridge::NotifyIfDoneLoading() const { | 185 void OfflinePageBridge::NotifyIfDoneLoading() const { |
157 if (!offline_page_model_->is_loaded()) | 186 if (!offline_page_model_->is_loaded()) |
158 return; | 187 return; |
159 JNIEnv* env = base::android::AttachCurrentThread(); | 188 JNIEnv* env = base::android::AttachCurrentThread(); |
160 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 189 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
161 if (obj.is_null()) | 190 if (obj.is_null()) |
162 return; | 191 return; |
163 Java_OfflinePageBridge_offlinePageModelLoaded(env, obj.obj()); | 192 Java_OfflinePageBridge_offlinePageModelLoaded(env, obj.obj()); |
(...skipping 26 matching lines...) Expand all Loading... |
190 return reinterpret_cast<jlong>(new OfflinePageBridge( | 219 return reinterpret_cast<jlong>(new OfflinePageBridge( |
191 env, obj, ProfileAndroid::FromProfileAndroid(j_profile))); | 220 env, obj, ProfileAndroid::FromProfileAndroid(j_profile))); |
192 } | 221 } |
193 | 222 |
194 bool RegisterOfflinePageBridge(JNIEnv* env) { | 223 bool RegisterOfflinePageBridge(JNIEnv* env) { |
195 return RegisterNativesImpl(env); | 224 return RegisterNativesImpl(env); |
196 } | 225 } |
197 | 226 |
198 } // namespace android | 227 } // namespace android |
199 } // namespace offline_pages | 228 } // namespace offline_pages |
OLD | NEW |