Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Adds support for clear cache
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Aug 6, 2019
1 parent 62f53ae commit 53dc347
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoSessionSettings;
import org.mozilla.geckoview.MediaElement;
import org.mozilla.geckoview.StorageController;
import org.mozilla.geckoview.WebRequestError;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.browser.Media;
Expand Down Expand Up @@ -373,23 +374,34 @@ private int createSession(@NonNull SessionSettings aSettings) {
return result;
}

private void recreateSession(SessionSettings aSettings) {
if (mCurrentSession != null) {
SessionState state = mSessions.get(mCurrentSession.hashCode());
if (state == null) {
return;
}
mCurrentSession.stop();
mCurrentSession.close();
private void recreateAllSessions() {
Map<Integer, SessionState> sessions = (Map<Integer, SessionState>) mSessions.clone();
mSessions.clear();
for (Map.Entry<Integer, SessionState> entry : sessions.entrySet()) {
recreateSession(entry.getValue());
}
}

int oldSessionId = getCurrentSessionId();
int sessionId = createSession(aSettings);
GeckoSession session = getSession(sessionId);
if (state.mSessionState != null) {
session.restoreState(state.mSessionState);
}
setCurrentSession(sessionId);
removeSession(oldSessionId);
private void recreateSession(@NonNull SessionState state) {
mCurrentSession.stop();
mCurrentSession.close();

int oldSessionId = getCurrentSessionId();
int sessionId = createSession(state.mSettings);
GeckoSession session = getSession(sessionId);
if (state.mSessionState != null) {
session.restoreState(state.mSessionState);
}
setCurrentSession(sessionId);
removeSession(oldSessionId);

Integer[] sessionsStack = mSessionsStack.stream().toArray(Integer[]::new);
mSessionsStack.clear();
for (Integer oldId : sessionsStack) {
if (oldId == oldSessionId)
mSessionsStack.push(sessionId);
else
mSessionsStack.push(oldId);
}
}

Expand Down Expand Up @@ -793,7 +805,7 @@ protected void setMultiprocess(final boolean aEnabled) {
SessionState state = mSessions.get(mCurrentSession.hashCode());
if (state != null && state.mSettings.isMultiprocessEnabled() != aEnabled) {
state.mSettings.setMultiprocessEnabled(aEnabled);
recreateSession(state.mSettings);
recreateSession(state);
}
}
}
Expand All @@ -803,8 +815,29 @@ protected void setTrackingProtection(final boolean aEnabled) {
SessionState state = mSessions.get(mCurrentSession.hashCode());
if (state != null && state.mSettings.isTrackingProtectionEnabled() != aEnabled) {
state.mSettings.setTrackingProtectionEnabled(aEnabled);
recreateSession(state.mSettings);
recreateSession(state);
}
}
}

public void clearCache() {
if (mRuntime != null) {
// Per GeckoView Docs:
// Note: Any open session may re-accumulate previously cleared data.
// To ensure that no persistent data is left behind, you need to close all sessions prior to clearing data.
// https://mozilla.github.io/geckoview/javadoc/mozilla-central/org/mozilla/geckoview/StorageController.html#clearData-long-
for (Map.Entry<Integer, SessionState> entry : mSessions.entrySet()) {
SessionState state = entry.getValue();
if (state != null) {
state.mSession.stop();
state.mSession.close();
}
}

mRuntime.getStorageController().clearData(StorageController.ClearFlags.ALL).then(aVoid -> {
recreateAllSessions();
return null;
});
}
}

Expand Down Expand Up @@ -918,7 +951,6 @@ public GeckoResult<GeckoSession> onNewSession(@NonNull GeckoSession aSession, @N
pushSession(getCurrentSessionId());

int sessionId;
boolean isPreviousPrivateMode = mCurrentSession.getSettings().getUsePrivateMode();
SessionSettings settings = new SessionSettings.Builder()
.withDefaultSettings(mContext)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ public void setAutoplayEnabled(final boolean enabled) {

public boolean getAutoplayEnabled() {
if (mRuntime != null) {
return mRuntime.getSettings().getAutoplayDefault() == GeckoRuntimeSettings.AUTOPLAY_DEFAULT_ALLOWED ?
true :
false;
return mRuntime.getSettings().getAutoplayDefault() == GeckoRuntimeSettings.AUTOPLAY_DEFAULT_ALLOWED;
}

return false;
Expand All @@ -234,6 +232,12 @@ public void setLocales(List<String> locales) {
}
}

public void clearCache() {
for (Map.Entry<Integer, SessionStack> entry : mSessionStacks.entrySet()) {
entry.getValue().clearCache();
}
}

// Permission Delegate

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ private void initialize(Context aContext) {
mBinding.servoSwitch.setOnCheckedChangeListener(mServoListener);
setServo(SettingsStore.getInstance(getContext()).isServoEnabled(), false);
}

mBinding.clearCacheButton.setOnClickListener(mClearCacheListener);
}

private SwitchSetting.OnCheckedChangeListener mRemoteDebuggingListener = (compoundButton, value, doApply) -> {
Expand Down Expand Up @@ -167,4 +169,8 @@ private void setServo(boolean value, boolean doApply) {
}
}

private OnClickListener mClearCacheListener = v -> {
SessionStore.get().clearCache();
};

}
7 changes: 7 additions & 0 deletions app/src/main/res/layout/options_developer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@
android:layout_height="wrap_content"
app:description="@string/developer_options_servo" />

<org.mozilla.vrbrowser.ui.views.settings.ButtonSetting
android:id="@+id/clearCacheButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:buttonText="@string/developer_options_clear_cache"
app:description="@string/developer_options_clear_cache_description" />

</LinearLayout>
</ScrollView>

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@
<!-- The string labels an On/Off switch in the developer options dialog and is used to toggle enabling Servo. -->
<string name="developer_options_servo">Enable Servo</string>

<!-- The string labels the description text for the clear cache button in the developer options dialog. -->
<string name="developer_options_clear_cache_description">Clear Cache</string>

<!-- The string labels the button text for the clear cache button in the developer options dialog. -->
<string name="developer_options_clear_cache">Clear</string>

<!-- This string is used to label a numerical-entry field where the user may set a new value to
use for the display density. -->
<string name="developer_options_display_density">Display Density:</string>
Expand Down

0 comments on commit 53dc347

Please sign in to comment.