Merge "Match rename of WebResourceRequest.hasUserGestureInsecure to hasGesture." into lmp-dev
diff --git a/chromium/java/com/android/webview/chromium/FileChooserParamsAdapter.java b/chromium/java/com/android/webview/chromium/FileChooserParamsAdapter.java
index 44f765b..07222f2 100644
--- a/chromium/java/com/android/webview/chromium/FileChooserParamsAdapter.java
+++ b/chromium/java/com/android/webview/chromium/FileChooserParamsAdapter.java
@@ -16,20 +16,34 @@
 
 package com.android.webview.chromium;
 
+import android.app.Activity;
 import android.content.Context;
+import android.content.Intent;
 import android.net.Uri;
 import android.webkit.WebChromeClient.FileChooserParams;
-import android.webkit.WebChromeClient.UploadHelper;
 
 import org.chromium.android_webview.AwContentsClient;
 
 public class FileChooserParamsAdapter extends FileChooserParams {
     private AwContentsClient.FileChooserParams mParams;
-    private Context mContext;
+
+    public static Uri[] parseFileChooserResult(int resultCode, Intent intent) {
+        if (resultCode == Activity.RESULT_CANCELED) {
+            return null;
+        }
+        Uri result = intent == null || resultCode != Activity.RESULT_OK ? null
+                : intent.getData();
+
+        Uri[] uris = null;
+        if (result != null) {
+            uris = new Uri[1];
+            uris[0] = result;
+        }
+        return uris;
+    }
 
     FileChooserParamsAdapter(AwContentsClient.FileChooserParams params, Context context) {
         mParams = params;
-        mContext = context;
     }
 
     @Override
@@ -60,7 +74,16 @@
     }
 
     @Override
-    public UploadHelper getUploadHelper() {
-        return new UploadHelperImpl(mParams, mContext);
+    public Intent createIntent() {
+        // TODO: Move this code to Aw. Once code is moved
+        // and merged to M37 get rid of this.
+        String mimeType = "*/*";
+        if (mParams.acceptTypes != null && !mParams.acceptTypes.trim().isEmpty())
+            mimeType = mParams.acceptTypes.split(";")[0];
+
+        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
+        i.addCategory(Intent.CATEGORY_OPENABLE);
+        i.setType(mimeType);
+        return i;
     }
 }
diff --git a/chromium/java/com/android/webview/chromium/ResourceRewriter.java b/chromium/java/com/android/webview/chromium/ResourceRewriter.java
index 3410044..99fa46f 100644
--- a/chromium/java/com/android/webview/chromium/ResourceRewriter.java
+++ b/chromium/java/com/android/webview/chromium/ResourceRewriter.java
@@ -34,102 +34,22 @@
         // Rewrite the R 'constants' for all library apks.
         SparseArray<String> packageIdentifiers = ctx.getResources().getAssets()
                 .getAssignedPackageIdentifiers();
+
         final int N = packageIdentifiers.size();
         for (int i = 0; i < N; i++) {
             final int id = packageIdentifiers.keyAt(i);
-            final String name = packageIdentifiers.valueAt(i);
             if (id == 0x01 || id == 0x7f) {
                 continue;
             }
 
-            // TODO(mkosiba): We should use jarjar to remove the redundant R classes here, but due
+            // TODO: We should use jarjar to remove the redundant R classes here, but due
             // to a bug in jarjar it's not possible to rename classes with '$' in their name.
             // See b/15684775.
-            rewriteRValues(com.android.webview.chromium.R.class, id);
-            rewriteRValues(org.chromium.content.R.class, id);
-            rewriteRValues(org.chromium.ui.R.class, id);
+            com.android.webview.chromium.R.onResourcesLoaded(id);
+            org.chromium.ui.R.onResourcesLoaded(id);
+            org.chromium.content.R.onResourcesLoaded(id);
 
             break;
         }
-
     }
-
-    private static void rewriteIntField(Field field, int packageId) throws IllegalAccessException {
-        int requiredModifiers = Modifier.STATIC | Modifier.PUBLIC;
-        int bannedModifiers = Modifier.FINAL;
-
-        int mod = field.getModifiers();
-        if ((mod & requiredModifiers) != requiredModifiers ||
-                (mod & bannedModifiers) != 0) {
-            throw new IllegalArgumentException("Field " + field.getName() +
-                    " is not rewritable");
-        }
-
-        if (field.getType() != int.class && field.getType() != Integer.class) {
-            throw new IllegalArgumentException("Field " + field.getName() +
-                    " is not an integer");
-        }
-
-        try {
-            int resId = field.getInt(null);
-            int newId = (resId & 0x00ffffff) | (packageId << 24);
-            field.setInt(null, newId);
-        } catch (IllegalAccessException e) {
-            // This should not occur (we check above if we can write to it)
-            throw new IllegalArgumentException(e);
-        }
-    }
-
-    private static void rewriteIntArrayField(Field field, int packageId) {
-        int requiredModifiers = Modifier.STATIC | Modifier.PUBLIC;
-
-        if ((field.getModifiers() & requiredModifiers) != requiredModifiers) {
-            throw new IllegalArgumentException("Field " + field.getName() +
-                    " is not rewritable");
-        }
-
-        if (field.getType() != int[].class) {
-            throw new IllegalArgumentException("Field " + field.getName() +
-                    " is not an integer array");
-        }
-
-        try {
-            int[] array = (int[]) field.get(null);
-            for (int i = 0; i < array.length; i++) {
-                array[i] = (array[i] & 0x00ffffff) | (packageId << 24);
-            }
-        } catch (IllegalAccessException e) {
-            // This should not occur (we check above if we can write to it)
-            throw new IllegalArgumentException(e);
-        }
-    }
-
-    private static void rewriteRValues(final Class<?> rClazz, int id) {
-        try {
-            Class<?>[] declaredClasses = rClazz.getDeclaredClasses();
-            for (Class<?> clazz : declaredClasses) {
-                try {
-                    if (clazz.getSimpleName().equals("styleable")) {
-                        for (Field field : clazz.getDeclaredFields()) {
-                            if (field.getType() == int[].class) {
-                                rewriteIntArrayField(field, id);
-                            }
-                        }
-
-                    } else {
-                        for (Field field : clazz.getDeclaredFields()) {
-                            rewriteIntField(field, id);
-                        }
-                    }
-                } catch (Exception e) {
-                    throw new IllegalArgumentException("Failed to rewrite R values for " +
-                            clazz.getName(), e);
-                }
-            }
-
-        } catch (Exception e) {
-            throw new IllegalArgumentException("Failed to rewrite R values", e);
-        }
-    }
-
 }
diff --git a/chromium/java/com/android/webview/chromium/UploadHelperImpl.java b/chromium/java/com/android/webview/chromium/UploadHelperImpl.java
deleted file mode 100644
index 0892d1b..0000000
--- a/chromium/java/com/android/webview/chromium/UploadHelperImpl.java
+++ /dev/null
@@ -1,172 +0,0 @@
-/*
- * Copyright (C) 2014 The Android Open Source Project
- *
- * Licensed 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 com.android.webview.chromium;
-
-import android.app.Activity;
-import android.content.Context;
-import android.content.Intent;
-import android.net.Uri;
-import android.os.Environment;
-import android.provider.MediaStore;
-import android.webkit.WebChromeClient;
-
-import org.chromium.android_webview.AwContentsClient;
-
-import java.io.File;
-
-public class UploadHelperImpl extends WebChromeClient.UploadHelper {
-
-    private final static String IMAGE_MIME_TYPE = "image/*";
-    private final static String VIDEO_MIME_TYPE = "video/*";
-    private final static String AUDIO_MIME_TYPE = "audio/*";
-
-    private AwContentsClient.FileChooserParams mParams;
-    private Context mContext;
-    private String mCameraFilePath;
-
-    public UploadHelperImpl(AwContentsClient.FileChooserParams params, Context context) {
-        mParams = params;
-        mContext = context;
-    }
-
-    @Override
-    public Intent buildIntent() {
-      // TODO(sgurun) Move this code to Aw. Once code is moved
-      // and merged to M37 get rid of this.
-      String mimeType = "*/*";
-      if (mParams.acceptTypes != null) {
-          mimeType = mParams.acceptTypes.split(";")[0];
-      }
-      boolean capture = mParams.capture;
-      if (mimeType.equals(IMAGE_MIME_TYPE)) {
-          if (capture) {
-              // Specified 'image/*' and requested capture. Launch the camera.
-              return createCameraIntent();
-          } else {
-              // Specified just 'image/*', and no capture. Show a traditional picker filtered
-              // on accept type by sending an intent for both the Camera and image/* OPENABLE.
-              Intent chooser = createChooserIntent(createCameraIntent());
-              chooser.putExtra(Intent.EXTRA_INTENT, createOpenableIntent(IMAGE_MIME_TYPE));
-              return chooser;
-          }
-      } else if (mimeType.equals(VIDEO_MIME_TYPE)) {
-          if (capture) {
-              // Specified 'video/*' and requested capture. Launch the camcorder.
-              return createCamcorderIntent();
-          } else {
-              // Specified just 'video/*', and no capture. Show a traditional file picker,
-              // filtered on accept type by sending an intent for both camcorder
-              // and video/* OPENABLE.
-              Intent chooser = createChooserIntent(createCamcorderIntent());
-              chooser.putExtra(Intent.EXTRA_INTENT, createOpenableIntent(VIDEO_MIME_TYPE));
-              return chooser;
-          }
-      } else if (mimeType.equals(AUDIO_MIME_TYPE)) {
-          if (capture) {
-              // Specified 'audio/*' and requested capture. Launch the sound recorder.
-              return createSoundRecorderIntent();
-          } else {
-              // Specified just 'audio/*', and no capture. Show a traditional file picker,
-              // filtered on accept type by sending an intent for both the sound
-              // recorder and audio/* OPENABLE.
-              Intent chooser = createChooserIntent(createSoundRecorderIntent());
-              chooser.putExtra(Intent.EXTRA_INTENT, createOpenableIntent(AUDIO_MIME_TYPE));
-              return chooser;
-          }
-      }
-      return createDefaultOpenableIntent();
-  }
-
-  @Override
-  public Uri[] parseResult(int resultCode, Intent intent) {
-      if (resultCode == Activity.RESULT_CANCELED) {
-          return null;
-      }
-      Uri result = intent == null || resultCode != Activity.RESULT_OK ? null
-              : intent.getData();
-
-      // As we ask the camera to save the result of the user taking
-      // a picture, the camera application does not return anything other
-      // than RESULT_OK. So we need to check whether the file we expected
-      // was written to disk in the in the case that we
-      // did not get an intent returned but did get a RESULT_OK. If it was,
-      // we assume that this result has came back from the camera.
-      if (result == null && intent == null && resultCode == Activity.RESULT_OK
-              && mCameraFilePath != null) {
-          File cameraFile = new File(mCameraFilePath);
-          if (cameraFile.exists()) {
-              result = Uri.fromFile(cameraFile);
-              // Broadcast to the media scanner that we have a new photo
-              // so it will be added into the gallery for the user.
-              mContext.sendBroadcast(
-                    new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
-          }
-      }
-
-      Uri[] uris = null;
-      if (result != null) {
-          uris = new Uri[1];
-          uris[0] = result;
-      }
-      return uris;
-  }
-
-  private Intent createDefaultOpenableIntent() {
-      // Create and return a chooser with the default OPENABLE
-      // actions including the camera, camcorder and sound
-      // recorder where available.
-      Intent i = new Intent(Intent.ACTION_GET_CONTENT);
-      i.addCategory(Intent.CATEGORY_OPENABLE);
-      i.setType("*/*");
-
-      Intent chooser = createChooserIntent(createCameraIntent(), createCamcorderIntent(),
-          createSoundRecorderIntent());
-      chooser.putExtra(Intent.EXTRA_INTENT, i);
-      return chooser;
-  }
-
-  private Intent createChooserIntent(Intent... intents) {
-      Intent chooser = new Intent(Intent.ACTION_CHOOSER);
-      chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);
-      return chooser;
-  }
-
-  private Intent createOpenableIntent(String type) {
-      Intent i = new Intent(Intent.ACTION_GET_CONTENT);
-      i.addCategory(Intent.CATEGORY_OPENABLE);
-      i.setType(type);
-      return i;
-  }
-
-  private Intent createCameraIntent() {
-      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
-      File cameraDataDir = Environment.getExternalStoragePublicDirectory(
-              Environment.DIRECTORY_DCIM);
-      mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
-                System.currentTimeMillis() + ".jpg";
-      intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));
-      return intent;
-  }
-
-  private Intent createCamcorderIntent() {
-      return new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
-  }
-
-  private Intent createSoundRecorderIntent() {
-      return new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
-  }
-}
diff --git a/chromium/java/com/android/webview/chromium/WebViewChromiumFactoryProvider.java b/chromium/java/com/android/webview/chromium/WebViewChromiumFactoryProvider.java
index 4ad9c04..c1089c0 100644
--- a/chromium/java/com/android/webview/chromium/WebViewChromiumFactoryProvider.java
+++ b/chromium/java/com/android/webview/chromium/WebViewChromiumFactoryProvider.java
@@ -22,7 +22,9 @@
 import android.app.ActivityThread;
 import android.content.ComponentCallbacks2;
 import android.content.Context;
+import android.content.Intent;
 import android.content.SharedPreferences;
+import android.net.Uri;
 import android.os.Build;
 import android.os.FileUtils;
 import android.os.Looper;
@@ -365,6 +367,11 @@
                     public void enableSlowWholeDocumentDraw() {
                         WebViewChromium.enableSlowWholeDocumentDraw();
                     }
+
+                    @Override
+                    public Uri[] parseFileChooserResult(int resultCode, Intent intent) {
+                        return FileChooserParamsAdapter.parseFileChooserResult(resultCode, intent);
+                    }
                 };
             }
         }