0

Add DownloadControllerAndroid public interface for android.

BUG=


Review URL: https://chromiumcodereview.appspot.com/11112022

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@161964 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
nileshagrawal@chromium.org
2012-10-15 21:22:57 +00:00
parent 5752e6b1de
commit 7f8619f5ef
12 changed files with 152 additions and 98 deletions

@ -8,7 +8,9 @@
#include "android_webview/browser/find_helper.h"
#include "android_webview/native/aw_contents.h"
#include "android_webview/native/aw_javascript_dialog_creator.h"
#include "content/public/browser/android/download_controller_android.h"
#include "content/public/browser/web_contents.h"
#include "net/http/http_request_headers.h"
using content::WebContents;
@ -47,4 +49,19 @@ void AwWebContentsDelegate::FindReply(WebContents* web_contents,
final_update);
}
bool AwWebContentsDelegate::CanDownload(content::RenderViewHost* source,
int request_id,
const std::string& request_method) {
if (request_method == net::HttpRequestHeaders::kGetMethod) {
content::DownloadControllerAndroid::Get()->CreateGETDownload(
source, request_id);
}
return false;
}
void AwWebContentsDelegate::OnStartDownload(WebContents* source,
content::DownloadItem* download) {
NOTREACHED(); // We always return false in CanDownload.
}
} // namespace android_webview

@ -27,6 +27,11 @@ class AwWebContentsDelegate
const gfx::Rect& selection_rect,
int active_match_ordinal,
bool final_update) OVERRIDE;
virtual bool CanDownload(content::RenderViewHost* source,
int request_id,
const std::string& request_method) OVERRIDE;
virtual void OnStartDownload(content::WebContents* source,
content::DownloadItem* download) OVERRIDE;
};
} // namespace android_webview

@ -11,12 +11,14 @@
#include "chrome/browser/ui/find_bar/find_notification_details.h"
#include "chrome/browser/ui/find_bar/find_tab_helper.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/android/download_controller_android.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/file_chooser_params.h"
#include "jni/ChromeWebContentsDelegateAndroid_jni.h"
#include "net/http/http_request_headers.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/rect_f.h"
@ -234,5 +236,24 @@ ChromeWebContentsDelegateAndroid::GetJavaScriptDialogCreator() {
return GetJavaScriptDialogCreatorInstance();
}
bool ChromeWebContentsDelegateAndroid::CanDownload(
content::RenderViewHost* source,
int request_id,
const std::string& request_method) {
if (request_method == net::HttpRequestHeaders::kGetMethod) {
content::DownloadControllerAndroid::Get()->CreateGETDownload(
source, request_id);
return false;
}
return true;
}
void ChromeWebContentsDelegateAndroid::OnStartDownload(
WebContents* source,
content::DownloadItem* download) {
content::DownloadControllerAndroid::Get()->OnPostDownloadStarted(
source, download);
}
} // namespace android
} // namespace chrome

@ -52,6 +52,11 @@ class ChromeWebContentsDelegateAndroid
const gfx::RectF& active_rect) OVERRIDE;
virtual content::JavaScriptDialogCreator*
GetJavaScriptDialogCreator() OVERRIDE;
virtual bool CanDownload(content::RenderViewHost* source,
int request_id,
const std::string& request_method) OVERRIDE;
virtual void OnStartDownload(content::WebContents* source,
content::DownloadItem* download) OVERRIDE;
private:
// NotificationObserver implementation.

@ -0,0 +1 @@
joth@chromium.org

@ -9,7 +9,6 @@
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/download_item.h"
#include "content/public/browser/invalidate_type.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/navigation_controller.h"
@ -18,21 +17,14 @@
#include "content/public/common/page_transition_types.h"
#include "content/public/common/referrer.h"
#include "jni/WebContentsDelegateAndroid_jni.h"
#include "net/http/http_request_headers.h"
#include "ui/gfx/rect.h"
#include "webkit/glue/window_open_disposition.h"
using base::android::AttachCurrentThread;
using base::android::CheckException;
using base::android::ConvertUTF8ToJavaString;
using base::android::ConvertUTF16ToJavaString;
using base::android::GetClass;
using base::android::GetMethodID;
using base::android::HasClass;
using base::android::ScopedJavaLocalRef;
using content::DownloadItem;
using content::JavaScriptDialogCreator;
using content::RenderViewHost;
using content::WebContents;
namespace web_contents_delegate_android {
@ -274,28 +266,6 @@ void WebContentsDelegateAndroid::UpdateTargetURL(WebContents* source,
java_url.obj());
}
bool WebContentsDelegateAndroid::CanDownload(
RenderViewHost* source,
int request_id,
const std::string& request_method) {
if (request_method == net::HttpRequestHeaders::kGetMethod) {
// TODO(leandrogracia): re-enable this when calling DownloadController
// doesn't introduce a DEPS layering violation.
// DownloadController::GetInstance()->CreateGETDownload(
// source, request_id);
return false;
}
return true;
}
void WebContentsDelegateAndroid::OnStartDownload(WebContents* source,
DownloadItem* download) {
// TODO(leandrogracia): re-enable this when calling DownloadController
// doesn't introduce a DEPS layering violation.
// DownloadController::GetInstance()->OnPostDownloadStarted(
// source, download);
}
bool WebContentsDelegateAndroid::ShouldOverrideLoading(const GURL& url) {
if (!url.is_valid())
return false;

@ -18,7 +18,6 @@
#include "net/base/net_errors.h"
namespace content {
class DownloadItem;
class JavaScriptDialogCreator;
class RenderViewHost;
class WebContents;
@ -94,11 +93,6 @@ class WebContentsDelegateAndroid : public content::WebContentsDelegate {
virtual void UpdateTargetURL(content::WebContents* source,
int32 page_id,
const GURL& url) OVERRIDE;
virtual bool CanDownload(content::RenderViewHost* source,
int request_id,
const std::string& request_method) OVERRIDE;
virtual void OnStartDownload(content::WebContents* source,
content::DownloadItem* download) OVERRIDE;
virtual bool ShouldOverrideLoading(const GURL& url) OVERRIDE;
virtual void HandleKeyboardEvent(
content::WebContents* source,

@ -11,7 +11,7 @@
#include "content/browser/android/content_video_view.h"
#include "content/browser/android/content_view_core_impl.h"
#include "content/browser/android/content_view_statics.h"
#include "content/browser/android/download_controller.h"
#include "content/browser/android/download_controller_android_impl.h"
#include "content/browser/android/load_url_params.h"
#include "content/browser/android/sandboxed_process_launcher.h"
#include "content/browser/android/surface_texture_peer_browser_impl.h"
@ -33,8 +33,8 @@ base::android::RegistrationMethod kContentRegisteredMethods[] = {
{ "ContentSettings", content::ContentSettings::RegisterContentSettings },
{ "ContentVideoView", content::ContentVideoView::RegisterContentVideoView },
{ "ContentViewCore", content::RegisterContentViewCore },
{ "DownloadController",
content::DownloadController::RegisterDownloadController },
{ "DownloadControllerAndroidImpl",
content::DownloadControllerAndroidImpl::RegisterDownloadController },
{ "RegisterImeAdapter", content::RegisterImeAdapter },
{ "JavaBoundObject", JavaBoundObject::RegisterJavaBoundObject },
{ "LoadUrlParams", content::RegisterLoadUrlParams },

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/android/download_controller.h"
#include "content/browser/android/download_controller_android_impl.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
@ -40,10 +40,10 @@ namespace content {
// JNI methods
static void Init(JNIEnv* env, jobject obj) {
DownloadController::GetInstance()->Init(env, obj);
DownloadControllerAndroidImpl::GetInstance()->Init(env, obj);
}
struct DownloadController::JavaObject {
struct DownloadControllerAndroidImpl::JavaObject {
ScopedJavaLocalRef<jobject> Controller(JNIEnv* env) {
return GetRealObject(env, obj);
}
@ -51,19 +51,25 @@ struct DownloadController::JavaObject {
};
// static
bool DownloadController::RegisterDownloadController(JNIEnv* env) {
bool DownloadControllerAndroidImpl::RegisterDownloadController(JNIEnv* env) {
return RegisterNativesImpl(env);
}
DownloadController* DownloadController::GetInstance() {
return Singleton<DownloadController>::get();
// static
DownloadControllerAndroid* DownloadControllerAndroid::Get() {
return DownloadControllerAndroidImpl::GetInstance();
}
DownloadController::DownloadController()
// static
DownloadControllerAndroidImpl* DownloadControllerAndroidImpl::GetInstance() {
return Singleton<DownloadControllerAndroidImpl>::get();
}
DownloadControllerAndroidImpl::DownloadControllerAndroidImpl()
: java_object_(NULL) {
}
DownloadController::~DownloadController() {
DownloadControllerAndroidImpl::~DownloadControllerAndroidImpl() {
if (java_object_) {
JNIEnv* env = AttachCurrentThread();
env->DeleteWeakGlobalRef(java_object_->obj);
@ -73,12 +79,12 @@ DownloadController::~DownloadController() {
}
// Initialize references to Java object.
void DownloadController::Init(JNIEnv* env, jobject obj) {
void DownloadControllerAndroidImpl::Init(JNIEnv* env, jobject obj) {
java_object_ = new JavaObject;
java_object_->obj = env->NewWeakGlobalRef(obj);
}
void DownloadController::CreateGETDownload(
void DownloadControllerAndroidImpl::CreateGETDownload(
RenderViewHost* render_view_host,
int request_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
@ -90,13 +96,13 @@ void DownloadController::CreateGETDownload(
// to retrieve it later (if it still exists).
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&DownloadController::PrepareDownloadInfo,
base::Bind(&DownloadControllerAndroidImpl::PrepareDownloadInfo,
base::Unretained(this), global_id,
render_process_id,
render_view_host->GetRoutingID()));
}
void DownloadController::PrepareDownloadInfo(
void DownloadControllerAndroidImpl::PrepareDownloadInfo(
const GlobalRequestID& global_id,
int render_process_id, int render_view_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
@ -113,7 +119,7 @@ void DownloadController::PrepareDownloadInfo(
if (cookie_monster) {
cookie_monster->GetAllCookiesForURLAsync(
request->url(),
base::Bind(&DownloadController::CheckPolicyAndLoadCookies,
base::Bind(&DownloadControllerAndroidImpl::CheckPolicyAndLoadCookies,
base::Unretained(this), info_android, render_process_id,
render_view_id, global_id));
} else {
@ -126,7 +132,7 @@ void DownloadController::PrepareDownloadInfo(
}
}
void DownloadController::CheckPolicyAndLoadCookies(
void DownloadControllerAndroidImpl::CheckPolicyAndLoadCookies(
const DownloadInfoAndroid& info, int render_process_id,
int render_view_id, const GlobalRequestID& global_id,
const net::CookieList& cookie_list) {
@ -142,7 +148,7 @@ void DownloadController::CheckPolicyAndLoadCookies(
}
}
void DownloadController::DoLoadCookies(
void DownloadControllerAndroidImpl::DoLoadCookies(
const DownloadInfoAndroid& info, int render_process_id,
int render_view_id, const GlobalRequestID& global_id) {
net::CookieOptions options;
@ -154,22 +160,23 @@ void DownloadController::DoLoadCookies(
request->context()->cookie_store()->GetCookiesWithOptionsAsync(
info.url, options,
base::Bind(&DownloadController::OnCookieResponse,
base::Bind(&DownloadControllerAndroidImpl::OnCookieResponse,
base::Unretained(this), info, render_process_id,
render_view_id));
}
void DownloadController::OnCookieResponse(DownloadInfoAndroid download_info,
int render_process_id,
int render_view_id,
const std::string& cookie) {
void DownloadControllerAndroidImpl::OnCookieResponse(
DownloadInfoAndroid download_info,
int render_process_id,
int render_view_id,
const std::string& cookie) {
download_info.cookie = cookie;
// We have everything we need, start Android download.
StartAndroidDownload(download_info, render_process_id, render_view_id);
}
void DownloadController::StartAndroidDownload(
void DownloadControllerAndroidImpl::StartAndroidDownload(
const DownloadInfoAndroid& info,
int render_process_id,
int render_view_id) {
@ -177,7 +184,7 @@ void DownloadController::StartAndroidDownload(
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&DownloadController::StartAndroidDownload,
base::Bind(&DownloadControllerAndroidImpl::StartAndroidDownload,
base::Unretained(this), info, render_process_id,
render_view_id));
return;
@ -213,7 +220,7 @@ void DownloadController::StartAndroidDownload(
jcookie.obj(), jreferer.obj(), info.total_bytes);
}
void DownloadController::OnPostDownloadStarted(
void DownloadControllerAndroidImpl::OnPostDownloadStarted(
WebContents* web_contents,
DownloadItem* download_item) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
@ -233,7 +240,7 @@ void DownloadController::OnPostDownloadStarted(
env, GetJavaObject()->Controller(env).obj(), view.obj());
}
void DownloadController::OnDownloadUpdated(DownloadItem* item) {
void DownloadControllerAndroidImpl::OnDownloadUpdated(DownloadItem* item) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (item->GetState() != DownloadItem::COMPLETE)
@ -263,10 +270,10 @@ void DownloadController::OnDownloadUpdated(DownloadItem* item) {
item->GetReceivedBytes(), true);
}
void DownloadController::OnDownloadOpened(DownloadItem* item) {
void DownloadControllerAndroidImpl::OnDownloadOpened(DownloadItem* item) {
}
ScopedJavaLocalRef<jobject> DownloadController::GetContentView(
ScopedJavaLocalRef<jobject> DownloadControllerAndroidImpl::GetContentView(
int render_process_id, int render_view_id) {
RenderViewHost* render_view_host =
RenderViewHost::FromID(render_process_id, render_view_id);
@ -284,7 +291,7 @@ ScopedJavaLocalRef<jobject> DownloadController::GetContentView(
}
ScopedJavaLocalRef<jobject>
DownloadController::GetContentViewCoreFromWebContents(
DownloadControllerAndroidImpl::GetContentViewCoreFromWebContents(
WebContents* web_contents) {
if (!web_contents)
return ScopedJavaLocalRef<jobject>();
@ -294,7 +301,8 @@ ScopedJavaLocalRef<jobject>
ScopedJavaLocalRef<jobject>();
}
DownloadController::JavaObject* DownloadController::GetJavaObject() {
DownloadControllerAndroidImpl::JavaObject*
DownloadControllerAndroidImpl::GetJavaObject() {
if (!java_object_) {
// Initialize Java DownloadController by calling
// DownloadController.getInstance(), which will call Init()
@ -313,10 +321,10 @@ DownloadController::JavaObject* DownloadController::GetJavaObject() {
return java_object_;
}
DownloadController::DownloadInfoAndroid::DownloadInfoAndroid(
DownloadControllerAndroidImpl::DownloadInfoAndroid::DownloadInfoAndroid(
net::URLRequest* request) {
request->GetResponseHeaderByName("content-disposition", &content_disposition);
request->GetMimeType(&original_mime_type);
request->GetResponseHeaderByName("mime-type", &original_mime_type);
request->extra_request_headers().GetHeader(
net::HttpRequestHeaders::kUserAgent, &user_agent);
GURL referer_url(request->GetSanitizedReferrer());
@ -328,6 +336,6 @@ DownloadController::DownloadInfoAndroid::DownloadInfoAndroid(
}
}
DownloadController::DownloadInfoAndroid::~DownloadInfoAndroid() {}
DownloadControllerAndroidImpl::DownloadInfoAndroid::~DownloadInfoAndroid() {}
} // namespace content

@ -10,20 +10,21 @@
//
// Call sequence
// GET downloads:
// DownloadController::NewGetDownload() =>
// DownloadControllerAndroid::CreateGETDownload() =>
// DownloadController.newHttpGetDownload() =>
// DownloadListener.onDownloadStart() /
// DownloadListener2.requestHttpGetDownload()
//
#ifndef CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_H_
#define CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_H_
#ifndef CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_
#define CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_
#include <string>
#include "base/android/jni_helper.h"
#include "base/android/scoped_java_ref.h"
#include "base/memory/singleton.h"
#include "content/public/browser/android/download_controller_android.h"
#include "content/public/browser/download_item.h"
#include "googleurl/src/gurl.h"
#include "net/cookies/cookie_monster.h"
@ -37,28 +38,15 @@ struct GlobalRequestID;
class RenderViewHost;
class WebContents;
class DownloadController : public DownloadItem::Observer {
class DownloadControllerAndroidImpl : public DownloadControllerAndroid,
public DownloadItem::Observer {
public:
static DownloadControllerAndroidImpl* GetInstance();
static bool RegisterDownloadController(JNIEnv* env);
static DownloadController* GetInstance();
// Called when DownloadController Java object is instantiated.
void Init(JNIEnv* env, jobject obj);
// Starts a new download request with Android. Should be called on the
// UI thread.
void CreateGETDownload(RenderViewHost* source,
int request_id);
// Should be called when a POST download is started. Notifies the embedding
// app about the download. Called on the UI thread.
void OnPostDownloadStarted(WebContents* web_contents,
DownloadItem* download_item);
// DownloadItem::Observer interface.
virtual void OnDownloadUpdated(DownloadItem* item) OVERRIDE;
virtual void OnDownloadOpened(DownloadItem* item) OVERRIDE;
private:
// Used to store all the information about an Android download.
struct DownloadInfoAndroid {
@ -80,11 +68,21 @@ class DownloadController : public DownloadItem::Observer {
WebContents* web_contents;
// Default copy constructor is used for passing this struct by value.
};
struct JavaObject;
friend struct DefaultSingletonTraits<DownloadController>;
DownloadController();
virtual ~DownloadController();
friend struct DefaultSingletonTraits<DownloadControllerAndroidImpl>;
DownloadControllerAndroidImpl();
virtual ~DownloadControllerAndroidImpl();
// DownloadControllerAndroid implementation.
virtual void CreateGETDownload(RenderViewHost* source,
int request_id) OVERRIDE;
virtual void OnPostDownloadStarted(WebContents* web_contents,
DownloadItem* download_item) OVERRIDE;
// DownloadItem::Observer interface.
virtual void OnDownloadUpdated(DownloadItem* item) OVERRIDE;
virtual void OnDownloadOpened(DownloadItem* item) OVERRIDE;
void PrepareDownloadInfo(const GlobalRequestID& global_id,
int render_process_id,
@ -121,9 +119,9 @@ class DownloadController : public DownloadItem::Observer {
JavaObject* java_object_;
DISALLOW_COPY_AND_ASSIGN(DownloadController);
DISALLOW_COPY_AND_ASSIGN(DownloadControllerAndroidImpl);
};
} // namespace content
#endif // CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_H_
#endif // CONTENT_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_IMPL_H_

@ -26,6 +26,7 @@
'public/browser/android/compositor.h',
'public/browser/android/content_view_core.h',
'public/browser/android/devtools_auth.h',
'public/browser/android/download_controller_android.h',
'public/browser/android/draw_delegate.h',
'public/browser/browser_accessibility_state.h',
'public/browser/browser_child_process_host.h',
@ -205,8 +206,8 @@
'browser/android/content_view_statics.h',
'browser/android/cookie_getter_impl.cc',
'browser/android/cookie_getter_impl.h',
'browser/android/download_controller.cc',
'browser/android/download_controller.h',
'browser/android/download_controller_android_impl.cc',
'browser/android/download_controller_android_impl.h',
'browser/android/devtools_auth.cc',
'browser/android/draw_delegate_impl.h',
'browser/android/draw_delegate_impl.cc',

@ -0,0 +1,34 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_PUBLIC_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_H_
#define CONTENT_PUBLIC_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_H_
namespace content {
class DownloadItem;
class RenderViewHost;
class WebContents;
// Interface to request GET downloads and send notifications for POST
// downloads.
class DownloadControllerAndroid {
public:
// Returns the singleton instance of the DownloadControllerAndroid.
static DownloadControllerAndroid* Get();
// Starts a new download request with Android. Should be called on the
// UI thread.
virtual void CreateGETDownload(RenderViewHost* source, int request_id) = 0;
// Should be called when a POST download is started. Notifies the embedding
// app about the download. Should be called on the UI thread.
virtual void OnPostDownloadStarted(WebContents* web_contents,
DownloadItem* download_item) = 0;
protected:
virtual ~DownloadControllerAndroid() {};
};
} // namespace content
#endif // CONTENT_PUBLIC_BROWSER_ANDROID_DOWNLOAD_CONTROLLER_ANDROID_H_