Skip to content

Commit

Permalink
[Android WebView] Add an API to toggle the JS navigator.online property
Browse files Browse the repository at this point in the history
BUG=279929

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@219854 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
benm@chromium.org committed Aug 27, 2013
1 parent 6bf9fd9 commit 6ec2787
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ void AwRenderViewHostExt::SetBackgroundColor(SkColor c) {
}
}

void AwRenderViewHostExt::SetJsOnlineProperty(bool network_up) {
Send(new AwViewMsg_SetJsOnlineProperty(network_up));
}

void AwRenderViewHostExt::RenderViewCreated(
content::RenderViewHost* render_view_host) {
Send(new AwViewMsg_SetBackgroundColor(web_contents()->GetRoutingID(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class AwRenderViewHostExt : public content::WebContentsObserver,
// the meta viewport tag.
void SetInitialPageScale(double page_scale_factor);
void SetBackgroundColor(SkColor c);
void SetJsOnlineProperty(bool network_up);

private:
// content::WebContentsObserver implementation.
Expand Down
4 changes: 3 additions & 1 deletion android_webview/common/render_view_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ IPC_MESSAGE_ROUTED1(AwViewMsg_SetInitialPageScale,
IPC_MESSAGE_ROUTED1(AwViewMsg_SetBackgroundColor,
SkColor);

IPC_MESSAGE_CONTROL1(AwViewMsg_SetJsOnlineProperty,
bool /* network_up */)

//-----------------------------------------------------------------------------
// RenderView messages
// These are messages sent from the renderer to the browser process.
Expand All @@ -87,4 +90,3 @@ IPC_MESSAGE_ROUTED1(AwViewHostMsg_UpdateHitTestData,
// Sent whenever the page scale factor (as seen by RenderView) is changed.
IPC_MESSAGE_ROUTED1(AwViewHostMsg_PageScaleFactorChanged,
float /* page_scale_factor */)

Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,11 @@ public void hideAutofillPopup() {
mAwAutofillManagerDelegate.hideAutofillPopup();
}

public void setNetworkAvailable(boolean networkUp) {
if (mNativeAwContents == 0) return;
nativeSetJsOnlineProperty(mNativeAwContents, networkUp);
}

//--------------------------------------------------------------------------------------------
// Methods called from native via JNI
//--------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1917,4 +1922,6 @@ private native void nativeSetDisplayedPageScaleFactor(int nativeAwContents,

private native void nativeInvokeGeolocationCallback(
int nativeAwContents, boolean value, String requestingFrame);

private native void nativeSetJsOnlineProperty(int nativeAwContents, boolean networkUp);
}
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,29 @@ public void testDownload() throws Throwable {
if (webServer != null) webServer.shutdown();
}
}

@Feature({"AndroidWebView", "setNetworkAvailable"})
@SmallTest
public void testSetNetworkAvailable() throws Throwable {
AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
AwContents awContents = testView.getAwContents();
String SCRIPT = "navigator.onLine";

enableJavaScriptOnUiThread(awContents);
loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), "about:blank");

// Default to "online".
assertEquals("true", executeJavaScriptAndWaitForResult(awContents, mContentsClient,
SCRIPT));

// Forcing "offline".
awContents.setNetworkAvailable(false);
assertEquals("false", executeJavaScriptAndWaitForResult(awContents, mContentsClient,
SCRIPT));

// Forcing "online".
awContents.setNetworkAvailable(true);
assertEquals("true", executeJavaScriptAndWaitForResult(awContents, mContentsClient,
SCRIPT));
}
}
6 changes: 6 additions & 0 deletions android_webview/native/aw_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -780,4 +780,10 @@ void AwContents::EnableOnNewPicture(JNIEnv* env,
browser_view_renderer_->EnableOnNewPicture(enabled);
}

void AwContents::SetJsOnlineProperty(JNIEnv* env,
jobject obj,
jboolean network_up) {
render_view_host_ext_->SetJsOnlineProperty(network_up);
}

} // namespace android_webview
3 changes: 3 additions & 0 deletions android_webview/native/aw_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ class AwContents : public FindHelper::Listener,

// Sets the java delegate
void SetAwAutofillManagerDelegate(jobject delegate);

void SetJsOnlineProperty(JNIEnv* env, jobject obj, jboolean network_up);

private:
void InitAutofillIfNecessary(bool enabled);

Expand Down
7 changes: 7 additions & 0 deletions android_webview/renderer/aw_render_process_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "android_webview/common/render_view_messages.h"
#include "ipc/ipc_message_macros.h"
#include "third_party/WebKit/public/web/WebCache.h"
#include "third_party/WebKit/public/web/WebNetworkStateNotifier.h"

namespace android_webview {

Expand All @@ -22,6 +23,7 @@ bool AwRenderProcessObserver::OnControlMessageReceived(
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(AwRenderProcessObserver, message)
IPC_MESSAGE_HANDLER(AwViewMsg_ClearCache, OnClearCache)
IPC_MESSAGE_HANDLER(AwViewMsg_SetJsOnlineProperty, OnSetJsOnlineProperty)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
Expand All @@ -36,4 +38,9 @@ void AwRenderProcessObserver::OnClearCache() {
WebKit::WebCache::clear();
}

void AwRenderProcessObserver::OnSetJsOnlineProperty(bool network_up) {
if (webkit_initialized_)
WebKit::WebNetworkStateNotifier::setOnLine(network_up);
}

} // nanemspace android_webview
1 change: 1 addition & 0 deletions android_webview/renderer/aw_render_process_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AwRenderProcessObserver : public content::RenderProcessObserver {

private:
void OnClearCache();
void OnSetJsOnlineProperty(bool network_up);

bool webkit_initialized_;
};
Expand Down

0 comments on commit 6ec2787

Please sign in to comment.