Browse Source

feat(android): add clearHistory, clearCache and clearFormData (#450)

* add clearHistory, clearCache and clearFormData android webview api.

* remove pointless `async`

* add docs for new android webview methods

* Update Reference.md

* update commands types

* add more strict type for RNCWebViewUIManager `Commands` property
Stanislav Shakirov 4 years ago
parent
commit
4a4f4a2c45

+ 30
- 12
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

115
   public static final int COMMAND_INJECT_JAVASCRIPT = 6;
115
   public static final int COMMAND_INJECT_JAVASCRIPT = 6;
116
   public static final int COMMAND_LOAD_URL = 7;
116
   public static final int COMMAND_LOAD_URL = 7;
117
   public static final int COMMAND_FOCUS = 8;
117
   public static final int COMMAND_FOCUS = 8;
118
+
119
+  // android commands
120
+  public static final int COMMAND_CLEAR_FORM_DATA = 1000;
121
+  public static final int COMMAND_CLEAR_CACHE = 1001;
122
+  public static final int COMMAND_CLEAR_HISTORY = 1002;
123
+
118
   protected static final String REACT_CLASS = "RNCWebView";
124
   protected static final String REACT_CLASS = "RNCWebView";
119
   protected static final String HTML_ENCODING = "UTF-8";
125
   protected static final String HTML_ENCODING = "UTF-8";
120
   protected static final String HTML_MIME_TYPE = "text/html";
126
   protected static final String HTML_MIME_TYPE = "text/html";
266
         break;
272
         break;
267
       case "LOAD_CACHE_ELSE_NETWORK":
273
       case "LOAD_CACHE_ELSE_NETWORK":
268
         cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK;
274
         cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK;
269
-        break;  
275
+        break;
270
       case "LOAD_NO_CACHE":
276
       case "LOAD_NO_CACHE":
271
         cacheMode = WebSettings.LOAD_NO_CACHE;
277
         cacheMode = WebSettings.LOAD_NO_CACHE;
272
         break;
278
         break;
545
   @Override
551
   @Override
546
   public @Nullable
552
   public @Nullable
547
   Map<String, Integer> getCommandsMap() {
553
   Map<String, Integer> getCommandsMap() {
548
-    Map map = MapBuilder.of(
549
-      "goBack", COMMAND_GO_BACK,
550
-      "goForward", COMMAND_GO_FORWARD,
551
-      "reload", COMMAND_RELOAD,
552
-      "stopLoading", COMMAND_STOP_LOADING,
553
-      "postMessage", COMMAND_POST_MESSAGE,
554
-      "injectJavaScript", COMMAND_INJECT_JAVASCRIPT,
555
-      "loadUrl", COMMAND_LOAD_URL
556
-    );
557
-    map.put("requestFocus", COMMAND_FOCUS);
558
-    return map;
554
+    return MapBuilder.<String, Integer>builder()
555
+      .put("goBack", COMMAND_GO_BACK)
556
+      .put("goForward", COMMAND_GO_FORWARD)
557
+      .put("reload", COMMAND_RELOAD)
558
+      .put("stopLoading", COMMAND_STOP_LOADING)
559
+      .put("postMessage", COMMAND_POST_MESSAGE)
560
+      .put("injectJavaScript", COMMAND_INJECT_JAVASCRIPT)
561
+      .put("loadUrl", COMMAND_LOAD_URL)
562
+      .put("requestFocus", COMMAND_FOCUS)
563
+      .put("clearFormData", COMMAND_CLEAR_FORM_DATA)
564
+      .put("clearCache", COMMAND_CLEAR_CACHE)
565
+      .put("clearHistory", COMMAND_CLEAR_HISTORY)
566
+      .build();
559
   }
567
   }
560
 
568
 
561
   @Override
569
   @Override
606
       case COMMAND_FOCUS:
614
       case COMMAND_FOCUS:
607
         root.requestFocus();
615
         root.requestFocus();
608
         break;
616
         break;
617
+      case COMMAND_CLEAR_FORM_DATA:
618
+        root.clearFormData();
619
+        break;
620
+      case COMMAND_CLEAR_CACHE:
621
+        boolean includeDiskFiles = args != null && args.getBoolean(0);
622
+        root.clearCache(includeDiskFiles);
623
+        break;
624
+      case COMMAND_CLEAR_HISTORY:
625
+        root.clearHistory();
626
+        break;
609
     }
627
     }
610
   }
628
   }
611
 
629
 

+ 29
- 0
docs/Reference.md View File

70
 - [`reload`](Reference.md#reload)
70
 - [`reload`](Reference.md#reload)
71
 - [`stopLoading`](Reference.md#stoploading)
71
 - [`stopLoading`](Reference.md#stoploading)
72
 - [`injectJavaScript`](Reference.md#injectjavascriptstr)
72
 - [`injectJavaScript`](Reference.md#injectjavascriptstr)
73
+- [`clearFormData`](Reference.md#clearFormData)
74
+- [`clearCache`](Reference.md#clearCache)
75
+- [`clearHistory`](Reference.md#clearHistory)
73
 
76
 
74
 ---
77
 ---
75
 
78
 
1108
 
1111
 
1109
 To learn more, read the [Communicating between JS and Native](Guide.md#communicating-between-js-and-native) guide.
1112
 To learn more, read the [Communicating between JS and Native](Guide.md#communicating-between-js-and-native) guide.
1110
 
1113
 
1114
+### `clearFormData()`
1115
+(android only)
1116
+
1117
+```javascript
1118
+clearFormData();
1119
+```
1120
+Removes the autocomplete popup from the currently focused form field, if present. [developer.android.com reference](https://developer.android.com/reference/android/webkit/WebView.html#clearFormData())
1121
+
1122
+
1123
+### `clearCache(bool)`
1124
+(android only)
1125
+```javascript
1126
+clearCache(true);
1127
+```
1128
+
1129
+Clears the resource cache. Note that the cache is per-application, so this will clear the cache for all WebViews used. [developer.android.com reference](https://developer.android.com/reference/android/webkit/WebView.html#clearCache(boolean))
1130
+
1131
+
1132
+### `clearHistory()`
1133
+(android only)
1134
+```javascript
1135
+clearHistory();
1136
+```
1137
+
1138
+Tells this WebView to clear its internal back/forward list. [developer.android.com reference](https://developer.android.com/reference/android/webkit/WebView.html#clearHistory())
1139
+
1111
 ## Other Docs
1140
 ## Other Docs
1112
 
1141
 
1113
 Also check out our [Getting Started Guide](Getting-Started.md) and [In-Depth Guide](Guide.md).
1142
 Also check out our [Getting Started Guide](Getting-Started.md) and [In-Depth Guide](Guide.md).

+ 26
- 2
src/WebView.android.tsx View File

27
   AndroidWebViewProps,
27
   AndroidWebViewProps,
28
   NativeWebViewAndroid,
28
   NativeWebViewAndroid,
29
   State,
29
   State,
30
-  RNCWebViewUIManager,
30
+  RNCWebViewUIManagerAndroid,
31
 } from './WebViewTypes';
31
 } from './WebViewTypes';
32
 
32
 
33
 import styles from './WebView.styles';
33
 import styles from './WebView.styles';
34
 
34
 
35
-const UIManager = NotTypedUIManager as RNCWebViewUIManager;
35
+const UIManager = NotTypedUIManager as RNCWebViewUIManagerAndroid;
36
 
36
 
37
 const RNCWebView = requireNativeComponent(
37
 const RNCWebView = requireNativeComponent(
38
   'RNCWebView',
38
   'RNCWebView',
123
     );
123
     );
124
   };
124
   };
125
 
125
 
126
+  clearFormData = () => {
127
+    UIManager.dispatchViewManagerCommand(
128
+       this.getWebViewHandle(),
129
+       this.getCommands().clearFormData,
130
+        undefined,
131
+    );
132
+  }
133
+
134
+  clearCache = (includeDiskFiles: boolean) => {
135
+    UIManager.dispatchViewManagerCommand(
136
+       this.getWebViewHandle(),
137
+       this.getCommands().clearCache,
138
+       [includeDiskFiles],
139
+    );
140
+  };
141
+
142
+  clearHistory = () => {
143
+    UIManager.dispatchViewManagerCommand(
144
+       this.getWebViewHandle(),
145
+       this.getCommands().clearHistory,
146
+        undefined,
147
+    );
148
+  };
149
+
126
   /**
150
   /**
127
    * Injects a javascript string into the referenced WebView. Deliberately does not
151
    * Injects a javascript string into the referenced WebView. Deliberately does not
128
    * return a response because using eval() to return a response breaks this method
152
    * return a response because using eval() to return a response breaks this method

+ 2
- 2
src/WebView.ios.tsx View File

28
   NativeWebViewIOS,
28
   NativeWebViewIOS,
29
   ViewManager,
29
   ViewManager,
30
   State,
30
   State,
31
-  RNCWebViewUIManager,
31
+  RNCWebViewUIManagerIOS,
32
 } from './WebViewTypes';
32
 } from './WebViewTypes';
33
 
33
 
34
 import styles from './WebView.styles';
34
 import styles from './WebView.styles';
35
 
35
 
36
-const UIManager = NotTypedUIManager as RNCWebViewUIManager;
36
+const UIManager = NotTypedUIManager as RNCWebViewUIManagerIOS;
37
 
37
 
38
 const { resolveAssetSource } = Image;
38
 const { resolveAssetSource } = Image;
39
 const processDecelerationRate = (
39
 const processDecelerationRate = (

+ 17
- 8
src/WebViewTypes.ts View File

14
 
14
 
15
 type WebViewCommands = 'goForward' | 'goBack' | 'reload' | 'stopLoading' | 'postMessage' | 'injectJavaScript' | 'loadUrl' | 'requestFocus';
15
 type WebViewCommands = 'goForward' | 'goBack' | 'reload' | 'stopLoading' | 'postMessage' | 'injectJavaScript' | 'loadUrl' | 'requestFocus';
16
 
16
 
17
-export interface RNCWebViewUIManager extends UIManagerStatic {
17
+type AndroidWebViewCommands = 'clearHistory' | 'clearCache' | 'clearFormData';
18
+
19
+
20
+
21
+interface RNCWebViewUIManager<Commands extends string> extends UIManagerStatic {
18
   getViewManagerConfig: (
22
   getViewManagerConfig: (
19
-    name: string,
23
+      name: string,
20
   ) => {
24
   ) => {
21
-    Commands: { [key in WebViewCommands]: number };
25
+    Commands: {[key in Commands]: number};
22
   };
26
   };
23
 }
27
 }
24
 
28
 
29
+export type RNCWebViewUIManagerAndroid = RNCWebViewUIManager<WebViewCommands | AndroidWebViewCommands>
30
+export type RNCWebViewUIManagerIOS = RNCWebViewUIManager<WebViewCommands>
31
+
32
+
33
+
25
 type WebViewState = 'IDLE' | 'LOADING' | 'ERROR';
34
 type WebViewState = 'IDLE' | 'LOADING' | 'ERROR';
26
 
35
 
27
 interface BaseState {
36
 interface BaseState {
457
   /**
466
   /**
458
    * https://developer.android.com/reference/android/webkit/WebSettings.html#setCacheMode(int)
467
    * https://developer.android.com/reference/android/webkit/WebSettings.html#setCacheMode(int)
459
    * Set the cacheMode. Possible values are:
468
    * Set the cacheMode. Possible values are:
460
-   * 
469
+   *
461
    * - `'LOAD_DEFAULT'` (default)
470
    * - `'LOAD_DEFAULT'` (default)
462
    * - `'LOAD_CACHE_ELSE_NETWORK'`
471
    * - `'LOAD_CACHE_ELSE_NETWORK'`
463
    * - `'LOAD_NO_CACHE'`
472
    * - `'LOAD_NO_CACHE'`
492
    */
501
    */
493
   geolocationEnabled?: boolean;
502
   geolocationEnabled?: boolean;
494
 
503
 
495
-  
504
+
496
   /**
505
   /**
497
-   * Boolean that sets whether JavaScript running in the context of a file 
498
-   * scheme URL should be allowed to access content from other file scheme URLs. 
506
+   * Boolean that sets whether JavaScript running in the context of a file
507
+   * scheme URL should be allowed to access content from other file scheme URLs.
499
    * Including accessing content from other file scheme URLs
508
    * Including accessing content from other file scheme URLs
500
    * @platform android
509
    * @platform android
501
    */
510
    */
502
   allowFileAccessFromFileURLs?: boolean;
511
   allowFileAccessFromFileURLs?: boolean;
503
-  
512
+
504
   /**
513
   /**
505
    * Boolean that sets whether JavaScript running in the context of a file
514
    * Boolean that sets whether JavaScript running in the context of a file
506
    * scheme URL should be allowed to access content from any origin.
515
    * scheme URL should be allowed to access content from any origin.