Skip to content

Commit

Permalink
Add ApplicationCache fromm GoogleDev Tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Shay Dratler authored and shs96c committed Sep 10, 2019
1 parent 2260d41 commit ef225e3
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.devtools.applicationCache;

import static org.openqa.selenium.devtools.ConverterFunctions.map;

import com.google.common.collect.ImmutableMap;
import com.google.common.reflect.TypeToken;

import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.applicationCache.model.ApplicationCacheModel;
import org.openqa.selenium.devtools.applicationCache.model.ApplicationCacheStatusUpdated;
import org.openqa.selenium.devtools.applicationCache.model.FrameWithManifest;
import org.openqa.selenium.devtools.page.model.FrameId;

import java.util.List;
import java.util.Objects;

public class ApplicationCache {

/** Enables application cache domain notifications. */
public static Command<Void> enable() {
return new Command<>("ApplicationCache.enable", ImmutableMap.of());
}

/** Returns relevant application cache data for the document in given frame. */
public static Command<ApplicationCacheModel> getApplicationCacheForFrame(FrameId frameId) {
Objects.requireNonNull(frameId, "frameId is required");
return new Command<>(
"ApplicationCache.getApplicationCacheForFrame",
ImmutableMap.of("frameId", frameId),
map("applicationCache", ApplicationCacheModel.class));
}

/**
* Returns array of frame identifiers with manifest urls for each frame containing a document
* associated with some application cache.
*/
public static Command<List<FrameWithManifest>> getFramesWithManifests() {
return new Command<>(
"ApplicationCache.getFramesWithManifests",
ImmutableMap.of(),
map("frameIds", new TypeToken<List<FrameWithManifest>>() {}.getType()));
}

public static Command<String> getManifestForFrame(FrameId frameId) {
Objects.requireNonNull(frameId, "frameId is required");
return new Command<>(
"ApplicationCache.getManifestForFrame",
ImmutableMap.of("frameId", frameId),
map("manifestURL", String.class));
}

public static Event<ApplicationCacheStatusUpdated> applicationCacheStatusUpdated() {
return new Event<>(
"ApplicationCache.applicationCacheStatusUpdated",
map("frameId", ApplicationCacheStatusUpdated.class));
}

public static Event<Boolean> networkStateUpdated() {
return new Event<>("ApplicationCache.networkStateUpdated", map("isNowOnline", Boolean.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.devtools.applicationCache.model;

import org.openqa.selenium.devtools.DevToolsException;
import org.openqa.selenium.json.JsonInput;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/** Detailed application cache information. */
public class ApplicationCacheModel {
/** Manifest URL. */
private final String manifestURL;
/** Application cache size. */
private final double size;
/** Application cache creation time. */
private final double creationTime;
/** Application cache update time. */
private final double updateTime;
/** Application cache resources. */
private final List<ApplicationCacheResource> resources;

public ApplicationCacheModel(
String manifestURL,
double size,
double creationTime,
double updateTime,
List<ApplicationCacheResource> resources) {
this.manifestURL = Objects.requireNonNull(manifestURL, "manifestURL is required");
this.size = Objects.requireNonNull(size, "size is required");
this.creationTime = Objects.requireNonNull(creationTime, "creationTime is required");
this.updateTime = Objects.requireNonNull(updateTime, "updateTime is required");
this.resources = validateResources(resources);
}

private static ApplicationCacheModel fromJson(JsonInput input) {
String manifestURL = input.nextString();
Double size = null, creationTime = null, updateTime = null;
List<ApplicationCacheResource> resources = null;
while (input.hasNext()) {
switch (input.nextName()) {
case "size":
size = input.read(Double.class);
break;
case "creationTime":
creationTime = input.read(Double.class);
break;
case "updateTime":
updateTime = input.read(Double.class);
break;
case "resources":
resources = new ArrayList<>();
input.beginArray();
while (input.hasNext()) {
resources.add(input.read(ApplicationCacheResource.class));
}
input.endArray();
break;
default:
input.skipValue();
break;
}
}
return new ApplicationCacheModel(manifestURL, size, creationTime, updateTime, resources);
}

private List<ApplicationCacheResource> validateResources(List<ApplicationCacheResource> resources) {
Objects.requireNonNull(resources, "resources is required");
if (resources.isEmpty()) {
throw new DevToolsException("resources is empty");
}
return resources;
}

public String getManifestURL() {
return manifestURL;
}

public double getSize() {
return size;
}

public double getCreationTime() {
return creationTime;
}

public double getUpdateTime() {
return updateTime;
}

public List<ApplicationCacheResource> getResources() {
return resources;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.devtools.applicationCache.model;

import org.openqa.selenium.json.JsonInput;

import java.util.Objects;

/** Detailed application cache resource information. */
public class ApplicationCacheResource {

/** Resource url. */
private final String url;
/** Resource size. */
private final int size;
/** Resource type. */
private final String type;

public ApplicationCacheResource(String url, int size, String type) {
this.url = Objects.requireNonNull(url, "url is required");
this.size = Objects.requireNonNull(size, "size is required");
this.type = Objects.requireNonNull(type, "type is required");
}

private static ApplicationCacheResource fromJson(JsonInput input) {
String url = input.nextString(), type = null;
Integer size = null;
while (input.hasNext()) {
switch (input.nextName()) {
case "type":
type = input.nextString();
break;
case "size":
size = input.read(Integer.class);
break;
default:
input.skipValue();
break;
}
}
return new ApplicationCacheResource(url, size, type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.devtools.applicationCache.model;

import org.openqa.selenium.devtools.page.model.FrameId;
import org.openqa.selenium.json.JsonInput;

import java.util.Objects;

public class ApplicationCacheStatusUpdated {

/** Frame identifier. */
private final FrameId frameId;
/** Manifest URL. */
private final String manifestURL;
/** Application cache status. */
private final int status;

public ApplicationCacheStatusUpdated(FrameId frameId, String manifestURL, int status) {
this.frameId = Objects.requireNonNull(frameId, "frameId is required");
this.manifestURL = Objects.requireNonNull(manifestURL, "manifestURL is required");
this.status = Objects.requireNonNull(status, "status is required");
}

private static ApplicationCacheStatusUpdated fromJson(JsonInput input) {
FrameId frameId = input.read(FrameId.class);
String manifestURL = null;
Integer status = null;
while (input.hasNext()) {
switch (input.nextName()) {
case "manifestURL":
manifestURL = input.nextString();
break;
case "status":
status = input.read(Integer.class);
break;
default:
input.skipValue();
break;
}
}
return new ApplicationCacheStatusUpdated(frameId, manifestURL, status);
}

public FrameId getFrameId() {
return frameId;
}

public String getManifestURL() {
return manifestURL;
}

public int getStatus() {
return status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.devtools.applicationCache.model;

import org.openqa.selenium.devtools.page.model.FrameId;
import org.openqa.selenium.json.JsonInput;

import java.util.Objects;

/** Frame identifier - manifest URL pair. */
public class FrameWithManifest {

/** Frame identifier. */
private final FrameId frameId;
/** Manifest URL. */
private final String manifestURL;
/** Application cache status. */
private final int status;

public FrameWithManifest(FrameId frameId, String manifestURL, int status) {
this.frameId = Objects.requireNonNull(frameId, "frameId is required");
this.manifestURL = Objects.requireNonNull(manifestURL, "manifestURL is required");
this.status = Objects.requireNonNull(status, "status is required");
}

private static FrameWithManifest fromJson(JsonInput input) {
FrameId frameId = input.read(FrameId.class);
String manifestURL = null;
Integer status = null;
while (input.hasNext()) {
switch (input.nextName()) {
case "manifestURL":
manifestURL = input.nextString();
break;
case "status":
status = input.read(Integer.class);
break;
default:
input.skipValue();
break;
}
}
return new FrameWithManifest(frameId, manifestURL, status);
}

public FrameId getFrameId() {
return frameId;
}

public String getManifestURL() {
return manifestURL;
}

public int getStatus() {
return status;
}
}

0 comments on commit ef225e3

Please sign in to comment.