OSDN Git Service

Add support for plugins to use incognito mode.
authorDerek Sollenberger <djsollen@google.com>
Fri, 25 Feb 2011 15:34:46 +0000 (10:34 -0500)
committerDerek Sollenberger <djsollen@google.com>
Fri, 25 Feb 2011 21:55:57 +0000 (16:55 -0500)
bug: 2968415

Change-Id: Ie67c9f6b28a81d4a65f39bde2896078d7b49b8ee

WebCore/plugins/android/PluginViewAndroid.cpp
WebKit/android/WebCoreSupport/WebRequestContext.cpp
WebKit/android/plugins/ANPSystemInterface.cpp
WebKit/android/plugins/ANPSystem_npapi.h
WebKit/android/plugins/PluginWidgetAndroid.cpp
WebKit/android/plugins/android_npapi.h

index b9d7a4f..6c712d0 100644 (file)
@@ -108,6 +108,7 @@ extern void ANPWindowInterfaceV0_Init(ANPInterface* value);
 extern void ANPWindowInterfaceV1_Init(ANPInterface* value);
 extern void ANPSystemInterfaceV0_Init(ANPInterface* value);
 extern void ANPSystemInterfaceV1_Init(ANPInterface* value);
+extern void ANPSystemInterfaceV2_Init(ANPInterface* value);
 extern void ANPOpenGLInterfaceV0_Init(ANPInterface* value);
 extern void ANPVideoInterfaceV0_Init(ANPInterface* value);
 
@@ -136,6 +137,7 @@ static const VarProcPair gVarProcs[] = {
     { VARPROCLINE(WindowInterfaceV1)        },
     { VARPROCLINE(SystemInterfaceV0)        },
     { VARPROCLINE(SystemInterfaceV1)        },
+    { VARPROCLINE(SystemInterfaceV2)        },
     { VARPROCLINE(OpenGLInterfaceV0)        },
     { VARPROCLINE(VideoInterfaceV0)         },
 };
index 341a23c..78c3501 100644 (file)
@@ -39,6 +39,8 @@ static WTF::Mutex acceptLanguageMutex;
 
 static int numPrivateBrowsingInstances;
 
+extern void ANPSystemInterface_CleanupIncognito();
+
 using namespace WTF;
 
 namespace android {
@@ -79,6 +81,7 @@ WebRequestContext::~WebRequestContext()
         if (!numPrivateBrowsingInstances) {
             WebCookieJar::cleanup(true);
             WebCache::cleanup(true);
+            ANPSystemInterface_CleanupIncognito();
         }
     }
 }
index 8510c2e..7199635 100644 (file)
 #include "config.h"
 
 #include "ANPSystem_npapi.h"
+#include "Frame.h"
 #include "JavaSharedClient.h"
 #include "PluginClient.h"
 #include "PluginPackage.h"
 #include "PluginView.h"
 #include "PluginWidgetAndroid.h"
+#include "Settings.h"
 #include "SkString.h"
 #include "WebViewCore.h"
 #include <wtf/text/CString.h>
 
+#include <dirent.h>
+
+//#define PLUGIN_DEBUG_LOCAL // controls the printing of log messages
+#include "PluginDebugAndroid.h"
+
 static const char* gApplicationDataDir = NULL;
+static const char* gApplicationDataDirIncognito = NULL;
 
 using namespace android;
 
+static WebCore::PluginView* pluginViewForInstance(NPP instance) {
+    if (instance && instance->ndata)
+        return static_cast<WebCore::PluginView*>(instance->ndata);
+    return WebCore::PluginView::currentPluginView();
+}
+
 static const char* anp_getApplicationDataDirectory() {
     if (NULL == gApplicationDataDir) {
         PluginClient* client = JavaSharedClient::GetPluginClient();
@@ -58,18 +72,49 @@ static const char* anp_getApplicationDataDirectory() {
         memcpy(storage, path.utf8().data(), length);
         storage[length] = '\0';
 
+        static const char incognitoPath[] = "/incognito_plugins";
+        char* incognitoStorage = (char*) malloc(length + strlen(incognitoPath) + 1);
+
+        strcpy(incognitoStorage, storage);
+        strcat(incognitoStorage, incognitoPath);
+
         // save this assignment for last, so that if multiple threads call us
         // (which should never happen), we never return an incomplete global.
         // At worst, we would allocate storage for the path twice.
         gApplicationDataDir = storage;
+        gApplicationDataDirIncognito = incognitoStorage;
     }
+
     return gApplicationDataDir;
 }
 
-static WebCore::PluginView* pluginViewForInstance(NPP instance) {
-    if (instance && instance->ndata)
-        return static_cast<WebCore::PluginView*>(instance->ndata);
-    return PluginView::currentPluginView();
+static const char* anp_getApplicationDataDirectoryV2(NPP instance) {
+    WebCore::PluginView* pluginView = pluginViewForInstance(instance);
+    PluginWidgetAndroid* pluginWidget = pluginView->platformPluginWidget();
+
+    if (NULL == gApplicationDataDir) {
+        anp_getApplicationDataDirectory();
+    }
+
+    WebCore::Settings* settings = pluginWidget->webViewCore()->mainFrame()->settings();
+    if (settings && settings->privateBrowsingEnabled()) {
+        // if this is an incognito view then check the path to see if it exists
+        // and if it is a directory, otherwise if it does not exist create it.
+        struct stat st;
+        if (stat(gApplicationDataDirIncognito, &st) == 0) {
+            if (!S_ISDIR(st.st_mode)) {
+                return NULL;
+            }
+        } else {
+            if (mkdir(gApplicationDataDirIncognito, S_IRWXU) != 0) {
+                return NULL;
+            }
+        }
+
+        return gApplicationDataDirIncognito;
+    }
+
+    return gApplicationDataDir;
 }
 
 static jclass anp_loadJavaClass(NPP instance, const char* className) {
@@ -107,3 +152,71 @@ void ANPSystemInterfaceV1_Init(ANPInterface* v) {
     ANPSystemInterfaceV1* i = reinterpret_cast<ANPSystemInterfaceV1*>(v);
     ASSIGN(i, setPowerState);
 }
+
+void ANPSystemInterfaceV2_Init(ANPInterface* v) {
+    // initialize the functions from the previous interface
+    ANPSystemInterfaceV1_Init(v);
+    // add any new functions or override existing functions
+    ANPSystemInterfaceV2* i = reinterpret_cast<ANPSystemInterfaceV2*>(v);
+    i->getApplicationDataDirectory = anp_getApplicationDataDirectoryV2;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+static bool isDirectory(const char* path) {
+    struct stat st;
+    return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
+}
+
+static void removeDirectory(const char* path) {
+    // create a pointer to a directory
+    DIR *dir = NULL;
+    dir = opendir(path);
+    if (!dir)
+        return;
+
+    struct dirent* entry = 0;
+    while ((entry = readdir(dir))) { // while there is still something in the directory to list
+        if (!entry)
+            return;
+
+        if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name)) {
+            PLUGIN_LOG(". file: %s", entry->d_name);
+            continue;
+        }
+
+        // concatenate the strings to get the complete path
+        static const char separator[] = "/";
+        char* file = (char*) malloc(strlen(path) + strlen(separator) + strlen(entry->d_name) + 1);
+        strcpy(file, path);
+        strcat(file, separator);
+        strcat(file, entry->d_name);
+
+        if (isDirectory(file) == true) {
+            PLUGIN_LOG("remove dir: %s", file);
+            removeDirectory(file);
+        } else { // it's a file, we can use remove
+            PLUGIN_LOG("remove file: %s", file);
+            remove(file);
+        }
+
+        free(file);
+    }
+
+    // clean up
+    closedir (dir); // close the directory
+    rmdir(path); // delete the directory
+}
+
+void ANPSystemInterface_CleanupIncognito() {
+    PLUGIN_LOG("cleanup incognito plugin directory");
+
+    if (gApplicationDataDirIncognito == NULL)
+        anp_getApplicationDataDirectory();
+    if (gApplicationDataDirIncognito == NULL)
+        return;
+
+    // check to see if the directory exists and if so delete it
+    if (isDirectory(gApplicationDataDirIncognito))
+        removeDirectory(gApplicationDataDirIncognito);
+}
index 5d91cfb..835bc7c 100644 (file)
@@ -57,4 +57,16 @@ struct ANPSystemInterfaceV1 : ANPSystemInterfaceV0 {
     void (*setPowerState)(NPP instance, ANPPowerState powerState);
 };
 
+struct ANPSystemInterfaceV2 : ANPInterface {
+    /** Return the path name for the current Application's plugin data directory,
+        or NULL if not supported. This directory will change depending on whether
+        or not the plugin is found within an incognito tab.
+     */
+    const char* (*getApplicationDataDirectory)(NPP instance);
+
+    // redeclaration of existing features
+    jclass (*loadJavaClass)(NPP instance, const char* className);
+    void (*setPowerState)(NPP instance, ANPPowerState powerState);
+};
+
 #endif //ANPSystem_npapi_H
index d7d4fa7..b8a10cc 100644 (file)
@@ -390,7 +390,7 @@ void PluginWidgetAndroid::sendSizeAndVisibilityEvents(const bool updateDimension
     const float zoomLevel = m_core->scale();
 
     // notify the plugin of the new size
-    if (m_drawingModel == kOpenGL_ANPDrawingModel && updateDimensions) {
+    if (m_drawingModel == kOpenGL_ANPDrawingModel && updateDimensions && m_pluginWindow) {
         PLUGIN_LOG("%s (%d,%d)[%f]", __FUNCTION__, m_pluginWindow->width,
                 m_pluginWindow->height, zoomLevel);
         ANPEvent event;
index fb13b43..b0c3765 100644 (file)
@@ -126,6 +126,8 @@ typedef uint32_t ANPMatrixFlag;
 #define kVideoInterfaceV0_ANPGetValue       ((NPNVariable)1015)
 #define kSystemInterfaceV1_ANPGetValue      ((NPNVariable)1016)
 
+#define kSystemInterfaceV2_ANPGetValue      ((NPNVariable)1017)
+
 /** queries for the drawing models supported on this device.
 
     NPN_GetValue(inst, kSupportedDrawingModel_ANPGetValue, uint32_t* bits)