Hello noel, playmobil,


Jeremy - can you focus on the Cocoa / Objective-C stuff?
Noel - can you look at the other stuff?

The drop_target_sf code has a few TODOs and workarounds, and is
not necessarily as complete, feature-wise, as the IE and FF
implementations, but I was hoping to get something basic (and
working) checked in and iterate from there.



I'd like you to do a code review.  Please execute
        g4 diff -c 8931781

or point your web browser to
        http://mondrian/8931781

to review the following code:

Change 8931781 by [EMAIL PROTECTED] on 2008/11/10 19:05:03 *pending*

        Introducing the Safari Drag and Drop implementation.
        
        PRESUBMIT=passed
        R=noel,playmobil
        [EMAIL PROTECTED]
        DELTA=652  (640 added, 3 deleted, 9 changed)
        OCL=8931781

Affected files ...

... //depot/googleclient/gears/opensource/gears/Makefile#204 edit
... 
//depot/googleclient/gears/opensource/gears/base/safari/browser_load_hook.mm#12 
edit
... //depot/googleclient/gears/opensource/gears/desktop/desktop.h#21 edit
... 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_osx.h#1 
add
... 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_osx.mm#1
 add
... //depot/googleclient/gears/opensource/gears/desktop/drop_target_base.h#1 
edit
... 
//depot/googleclient/gears/opensource/gears/desktop/drop_target_registration.cc#1
 edit
... 
//depot/googleclient/gears/opensource/gears/desktop/drop_target_registration.h#1
 edit
... //depot/googleclient/gears/opensource/gears/desktop/drop_target_sf.cc#1 add
... //depot/googleclient/gears/opensource/gears/desktop/drop_target_sf.h#1 add

652 delta lines: 640 added, 3 deleted, 9 changed

Also consider running:
        g4 lint -c 8931781

which verifies that the changelist doesn't introduce new style violations.

If you can't do the review, please let me know as soon as possible.  During
your review, please ensure that all new code has corresponding unit tests and
that existing unit tests are updated appropriately.  Visit
http://www/eng/code_review.html for more information.

This is a semiautomated message from "g4 mail".  Complaints or suggestions?
Mail [EMAIL PROTECTED]
Change 8931781 by [EMAIL PROTECTED] on 2008/11/10 19:05:03 *pending*

        Introducing the Safari Drag and Drop implementation.
        
        OCL=8931781

Affected files ...

... //depot/googleclient/gears/opensource/gears/Makefile#204 edit
... 
//depot/googleclient/gears/opensource/gears/base/safari/browser_load_hook.mm#12 
edit
... //depot/googleclient/gears/opensource/gears/desktop/desktop.h#21 edit
... 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_osx.h#1 
add
... 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_osx.mm#1
 add
... //depot/googleclient/gears/opensource/gears/desktop/drop_target_base.h#1 
edit
... 
//depot/googleclient/gears/opensource/gears/desktop/drop_target_registration.cc#1
 edit
... 
//depot/googleclient/gears/opensource/gears/desktop/drop_target_registration.h#1
 edit
... //depot/googleclient/gears/opensource/gears/desktop/drop_target_sf.cc#1 add
... //depot/googleclient/gears/opensource/gears/desktop/drop_target_sf.h#1 add

==== //depot/googleclient/gears/opensource/gears/Makefile#204 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/Makefile 
====
# action=edit type=text
--- googleclient/gears/opensource/gears/Makefile        2008-11-10 
18:53:34.000000000 +1100
+++ googleclient/gears/opensource/gears/Makefile        2008-11-10 
13:30:41.000000000 +1100
@@ -1429,6 +1429,11 @@
                desktop_cr.cc \
                $(NULL)
 
+SF_CPPSRCS += \
+               drag_and_drop_utils_osx.mm \
+               drop_target_sf.cc \
+               $(NULL)
+
 #-----------------------------------------------------------------------------
 # gtest
 
==== 
//depot/googleclient/gears/opensource/gears/base/safari/browser_load_hook.mm#12 
- 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/base/safari/browser_load_hook.mm
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/base/safari/browser_load_hook.mm        
2008-11-10 13:09:42.000000000 +1100
+++ googleclient/gears/opensource/gears/base/safari/browser_load_hook.mm        
2008-11-06 16:27:14.000000000 +1100
@@ -23,12 +23,14 @@
 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+#import "gears/base/safari/browser_load_hook.h"
+
 #import "gears/base/common/common.h"
 #import "gears/base/common/detect_version_collision.h"
 #import "gears/base/common/exception_handler.h"
 #import "gears/base/common/message_queue.h"
-#import "gears/base/safari/browser_load_hook.h"
 #import "gears/base/safari/safari_workarounds.h"
+#import "gears/desktop/drag_and_drop_utils_osx.h"
 #import "gears/localserver/safari/http_handler.h"
 #import "gears/localserver/safari/ui_thread.h"
 #import "gears/ui/safari/settings_menu.h"
@@ -107,6 +109,8 @@
   } else {
     LOG(("Gears: notification hook already installed"));
   }
+
+  SwizzleWebViewMethods();
   
   // If we got here then we loaded OK
   LOG(("Loaded Gears version: " PRODUCT_VERSION_STRING_ASCII "\n" ));
==== //depot/googleclient/gears/opensource/gears/desktop/desktop.h#21 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/desktop.h
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/desktop/desktop.h       2008-11-10 
13:09:43.000000000 +1100
+++ googleclient/gears/opensource/gears/desktop/desktop.h       2008-11-10 
13:47:41.000000000 +1100
@@ -205,7 +205,7 @@
 #if defined(OFFICIAL_BUILD) || defined(OS_ANDROID)
 // The Drag-and-Drop API has not been finalized for official builds.
 #else
-  // IN: DomElement div, object callbacks
+  // IN: DomElement div, object options
   // OUT: -
   void RegisterDropTarget(JsCallContext *context);
 #endif
==== 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_osx.h#1 
- 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_osx.h
 ====
# action=add type=text
--- /dev/null   1970-01-01 10:00:00.000000000 +1000
+++ googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_osx.h       
2008-11-10 12:43:20.000000000 +1100
@@ -0,0 +1,41 @@
+// Copyright 2008, Google Inc.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//  1. Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//  2. Redistributions in binary form must reproduce the above copyright 
notice,
+//     this list of conditions and the following disclaimer in the 
documentation
+//     and/or other materials provided with the distribution.
+//  3. Neither the name of Google Inc. nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef GEARS_DESKTOP_DRAG_AND_DROP_UTILS_OSX_H__
+#define GEARS_DESKTOP_DRAG_AND_DROP_UTILS_OSX_H__
+
+#include "gears/base/common/base_class.h"
+#include "gears/base/common/string16.h"
+
+bool GetDroppedFiles(ModuleEnvironment *module_environment,
+                     JsArray *files_out,
+                     bool reset);
+
+bool IsInADragOperation();
+bool IsInADropOperation();
+
+void SwizzleWebViewMethods();
+
+#endif  // GEARS_DESKTOP_DRAG_AND_DROP_UTILS_OSX_H__
==== 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_osx.mm#1
 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_osx.mm
 ====
# action=add type=text
--- /dev/null   1970-01-01 10:00:00.000000000 +1000
+++ googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_osx.mm      
2008-11-10 19:09:44.000000000 +1100
@@ -0,0 +1,145 @@
+// Copyright 2008, Google Inc.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//  1. Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//  2. Redistributions in binary form must reproduce the above copyright 
notice,
+//     this list of conditions and the following disclaimer in the 
documentation
+//     and/or other materials provided with the distribution.
+//  3. Neither the name of Google Inc. nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#import <Cocoa/Cocoa.h>
+#import <WebKit/DOM.h>
+#import <WebKit/WebView.h>
+#import <objc/objc-class.h>
+#import <vector>
+
+#import "gears/desktop/drag_and_drop_utils_osx.h"
+
+#import "gears/base/common/common.h"
+#import "gears/base/safari/nsstring_utils.h"
+#import "gears/desktop/file_dialog.h"
+
+static std::vector<std::string16> g_dragging_pasteboard_filenames_;
+static bool g_is_in_a_drag_operation_ = false;
+static bool g_is_in_a_drop_operation_ = false;
+
+std::vector<std::string16> &GetDraggingPasteboardFilenames() {
+  return g_dragging_pasteboard_filenames_;
+}
+
+bool IsInADragOperation() {
+  return g_is_in_a_drag_operation_;
+}
+
+bool IsInADropOperation() {
+  return g_is_in_a_drop_operation_;
+}
+
+// We "swizzle" some Cocoa method implementations to insert a little code
+// before and after certain events get processed. For more, see
+// http://www.cocoadev.com/index.pl?MethodSwizzling
+void MethodSwizzle(Class klass, SEL old_selector, SEL new_selector) {
+  Method old_method = class_getInstanceMethod(klass, old_selector);
+  Method new_method = class_getInstanceMethod(klass, new_selector);
+  if (old_method && new_method) {
+    char *temp1 = old_method->method_types;
+    old_method->method_types = new_method->method_types;
+    new_method->method_types = temp1;
+
+    IMP temp2 = old_method->method_imp;
+    old_method->method_imp = new_method->method_imp;
+    new_method->method_imp = temp2;
+  }
+}
+
[EMAIL PROTECTED] WebView (SwizzledMethods)
+- (NSDragOperation)swizzledDraggingEntered:(id <NSDraggingInfo>) draggingInfo;
+- (NSDragOperation)swizzledDraggingUpdated:(id <NSDraggingInfo>) draggingInfo;
+- (NSDragOperation)swizzledDraggingExited:(id <NSDraggingInfo>) draggingInfo;
+- (BOOL)swizzledPerformDragOperation:(id <NSDraggingInfo>) draggingInfo;
[EMAIL PROTECTED]
+
[EMAIL PROTECTED] WebView (SwizzledMethods)
+- (NSDragOperation)swizzledDraggingEntered:(id <NSDraggingInfo>) draggingInfo {
+  g_dragging_pasteboard_filenames_.clear();
+
+  NSPasteboard *pboard = [draggingInfo draggingPasteboard];
+  if ([[pboard types] containsObject:NSFilenamesPboardType]) {
+    NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
+    int numberOfFiles = [files count];
+    for (int i = 0; i < numberOfFiles; ++i) {
+      NSString *ns_string = [files objectAtIndex:i];
+      std::string16 std_string;
+      [ns_string string16:&std_string];
+      g_dragging_pasteboard_filenames_.push_back(std_string);
+    }
+  }
+
+  return [self swizzledDraggingEntered:draggingInfo];
+}
+
+- (NSDragOperation)swizzledDraggingUpdated:(id <NSDraggingInfo>) draggingInfo {
+  g_is_in_a_drag_operation_ = true;
+  NSDragOperation result = [self swizzledDraggingUpdated:draggingInfo];
+  g_is_in_a_drag_operation_ = false;
+  return result;
+}
+
+- (NSDragOperation)swizzledDraggingExited:(id <NSDraggingInfo>) draggingInfo {
+  g_dragging_pasteboard_filenames_.clear();
+  return [self swizzledDraggingExited:draggingInfo];
+}
+
+- (BOOL)swizzledPerformDragOperation:(id <NSDraggingInfo>) draggingInfo {
+  g_is_in_a_drop_operation_ = true;
+  BOOL result = [self swizzledPerformDragOperation:draggingInfo];
+  g_is_in_a_drop_operation_ = false;
+  return result;
+}
[EMAIL PROTECTED]
+
+void SwizzleWebViewMethods() {
+  MethodSwizzle([WebView class],
+                @selector(draggingEntered:),
+                @selector(swizzledDraggingEntered:));
+  MethodSwizzle([WebView class],
+                @selector(draggingUpdated:),
+                @selector(swizzledDraggingUpdated:));
+  MethodSwizzle([WebView class],
+                @selector(draggingExited:),
+                @selector(swizzledDraggingExited:));
+  MethodSwizzle([WebView class],
+                @selector(performDragOperation:),
+                @selector(swizzledPerformDragOperation:));
+}
+
+bool GetDroppedFiles(ModuleEnvironment *module_environment,
+                     JsArray *files_out,
+                     bool reset) {
+  std::string16 ignored;
+  bool result = FileDialog::FilesToJsObjectArray(
+      g_dragging_pasteboard_filenames_,
+      module_environment,
+      files_out,
+      &ignored);
+  if (reset) {
+    g_dragging_pasteboard_filenames_.clear();
+  }
+  return result;
+};
==== //depot/googleclient/gears/opensource/gears/desktop/drop_target_base.h#1 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/drop_target_base.h
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/desktop/drop_target_base.h      
2008-11-10 18:53:35.000000000 +1100
+++ googleclient/gears/opensource/gears/desktop/drop_target_base.h      
2008-11-10 13:39:23.000000000 +1100
@@ -29,10 +29,11 @@
 // The Drag-and-Drop API has not been finalized for official builds.
 #else
 
-#if BROWSER_FF || (BROWSER_IE && !defined(OS_WINCE))
+#if BROWSER_FF || (BROWSER_IE && !defined(OS_WINCE)) || \
+    (BROWSER_WEBKIT && defined(OS_MACOSX))
 
 // TODO(nigeltao): Before Drag and Drop is made an official Gears API, we have
-// to (1) implement DnD on Chrome and Safari, and (2) decide what to do about
+// to (1) implement DnD on Chrome, and (2) decide what to do about
 // mobile platforms like Android and WinCE.
 #define GEARS_DRAG_AND_DROP_API_IS_SUPPORTED_FOR_THIS_PLATFORM 1
 
==== 
//depot/googleclient/gears/opensource/gears/desktop/drop_target_registration.cc#1
 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/drop_target_registration.cc
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/desktop/drop_target_registration.cc     
2008-11-10 13:09:43.000000000 +1100
+++ googleclient/gears/opensource/gears/desktop/drop_target_registration.cc     
2008-10-29 14:04:38.000000000 +1100
@@ -26,9 +26,8 @@
 #ifdef OFFICIAL_BUILD
   // The Drag-and-Drop API has not been finalized for official builds.
 #else
-#if defined(BROWSER_CHROME) || defined(BROWSER_WEBKIT) || \
-    defined(OS_WINCE) || defined(OS_ANDROID)
-  // The Drag-and-Drop API has not been implemented for Chrome, Safari,
+#if defined(BROWSER_CHROME) || defined(OS_WINCE) || defined(OS_ANDROID)
+  // The Drag-and-Drop API has not been implemented for Chrome,
   // PocketIE, and Android.
 #else
 
@@ -65,5 +64,5 @@
 }
 
 
-#endif  // BROWSER_CHROME, BROWSER_WEBKIT, OS_WINCE, OS_ANDROID
+#endif  // BROWSER_CHROME, OS_WINCE, OS_ANDROID
 #endif  // OFFICIAL_BUILD
==== 
//depot/googleclient/gears/opensource/gears/desktop/drop_target_registration.h#1
 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/drop_target_registration.h
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/desktop/drop_target_registration.h      
2008-11-10 13:09:43.000000000 +1100
+++ googleclient/gears/opensource/gears/desktop/drop_target_registration.h      
2008-10-29 14:04:08.000000000 +1100
@@ -28,9 +28,8 @@
 #ifdef OFFICIAL_BUILD
 // The Drag-and-Drop API has not been finalized for official builds.
 #else
-#if defined(BROWSER_CHROME) || defined(BROWSER_WEBKIT) || \
-    defined(OS_WINCE) || defined(OS_ANDROID)
-  // The Drag-and-Drop API has not been implemented for Chrome, Safari,
+#if defined(BROWSER_CHROME) ||  defined(OS_WINCE) || defined(OS_ANDROID)
+  // The Drag-and-Drop API has not been implemented for Chrome,
   // PocketIE, and Android.
 #else
 
@@ -41,6 +40,8 @@
 #include "gears/desktop/drop_target_ff.h"
 #elif BROWSER_IE && !defined(OS_WINCE)
 #include "gears/desktop/drop_target_ie.h"
+#elif BROWSER_SAFARI
+#include "gears/desktop/drop_target_sf.h"
 #endif
 
 class GearsDropTargetRegistration : public ModuleImplBaseClass {
@@ -61,6 +62,6 @@
   DISALLOW_EVIL_CONSTRUCTORS(GearsDropTargetRegistration);
 };
 
-#endif  // BROWSER_CHROME, BROWSER_WEBKIT, OS_WINCE, OS_ANDROID
+#endif  // BROWSER_CHROME, OS_WINCE, OS_ANDROID
 #endif  // OFFICIAL_BUILD
 #endif  // GEARS_DESKTOP_DROP_TARGET_REGISTRATION_H__
==== //depot/googleclient/gears/opensource/gears/desktop/drop_target_sf.cc#1 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/drop_target_sf.cc
 ====
# action=add type=text
--- /dev/null   1970-01-01 10:00:00.000000000 +1000
+++ googleclient/gears/opensource/gears/desktop/drop_target_sf.cc       
2008-11-10 18:59:19.000000000 +1100
@@ -0,0 +1,344 @@
+// Copyright 2008, Google Inc.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//  1. Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//  2. Redistributions in binary form must reproduce the above copyright 
notice,
+//     this list of conditions and the following disclaimer in the 
documentation
+//     and/or other materials provided with the distribution.
+//  3. Neither the name of Google Inc. nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifdef OFFICIAL_BUILD
+  // The Drag-and-Drop API has not been finalized for official builds.
+#else
+#include "gears/desktop/drop_target_sf.h"
+
+#include "gears/base/common/leak_counter.h"
+#include "gears/base/npapi/scoped_npapi_handles.h"
+#include "gears/desktop/drag_and_drop_utils_osx.h"
+#include "gears/desktop/file_dialog.h"
+
+
+class GearsHtmlEventListener : public ModuleImplBaseClass {
+ public:
+  static const std::string kModuleName;
+
+  GearsHtmlEventListener();
+
+  void HandleEvent(JsCallContext *context);
+
+  void SetCallback(bool (*callback)(JsObject *, DropTarget *),
+                   DropTarget *callback_data);
+
+ private:
+  bool (*callback_)(JsObject *event, DropTarget *drop_target);
+  scoped_refptr<DropTarget> drop_target_;
+  ScopedNPVariant compiled_prevent_default_script_;
+
+  bool PreventDefaultEventBehavior(JsObject *event);
+
+  DISALLOW_EVIL_CONSTRUCTORS(GearsHtmlEventListener);
+};
+
+
+DECLARE_DISPATCHER(GearsHtmlEventListener);
+
+template<>
+void Dispatcher<GearsHtmlEventListener>::Init() {
+  RegisterMethod("handleEvent", &GearsHtmlEventListener::HandleEvent);
+}
+
+
+const std::string 
GearsHtmlEventListener::kModuleName("GearsHtmlEventListener");
+
+
+GearsHtmlEventListener::GearsHtmlEventListener()
+    : ModuleImplBaseClass(kModuleName),
+      callback_(NULL) {
+}
+
+
+void GearsHtmlEventListener::HandleEvent(JsCallContext *context) {
+  if (!callback_) {
+    return;
+  }
+
+  scoped_ptr<JsObject> event;
+  JsArgument argv[] = {
+    { JSPARAM_REQUIRED, JSPARAM_OBJECT, as_out_parameter(event) }
+  };
+  context->GetArguments(ARRAYSIZE(argv), argv);
+  if (context->is_exception_set()) {
+    return;
+  }
+
+  PreventDefaultEventBehavior(event.get());
+
+  bool result = callback_(event.get(), drop_target_.get());
+  context->SetReturnValue(JSPARAM_BOOL, &result);
+}
+
+
+void GearsHtmlEventListener::SetCallback(
+      bool (*callback)(JsObject *, DropTarget *),
+      DropTarget *drop_target) {
+  callback_ = callback;
+  drop_target_.reset(drop_target);
+}
+
+
+// TODO(nigeltao): It seems weird to compile a hard-coded script just to
+// execute a known method on a known object, but trying to invoke the
+// method directly via event.GetPropertyAsFunction followed by js_runner->
+// InvokeCallback leads to a mysterious TypeError, on Safari / Mac OS X.
+// Ideally we would find the cause of the error (which is possibly in Gears'
+// NPAPI binding code) and do it directly, but in the meantime, here is an
+// indirect workaround.
+bool GearsHtmlEventListener::PreventDefaultEventBehavior(JsObject *event) {
+  NPP npp = module_environment_->js_runner_->GetContext();
+  if (NPVARIANT_IS_VOID(compiled_prevent_default_script_)) {
+    NPObject *global;
+    if (NPN_GetValue(npp, NPNVWindowNPObject, &global) != NPERR_NO_ERROR) {
+      return false;
+    }
+    ScopedNPObject scoped_global(global);
+
+    static const char script[] = "(function(e) { e.preventDefault(); })";
+    NPString np_script = { script, ARRAYSIZE(script) - 1 };
+    if (!NPN_Evaluate(npp, global, &np_script,
+                      &compiled_prevent_default_script_) ||
+        !NPVARIANT_IS_OBJECT(compiled_prevent_default_script_)) {
+      return false;
+    }
+  }
+
+  ScopedNPVariant ignored_result;
+  const int argc = 1;
+  scoped_array<ScopedNPVariant> argv(new ScopedNPVariant[argc]);
+  argv[0].Reset(event->token());
+  NPN_InvokeDefault(npp, NPVARIANT_TO_OBJECT(compiled_prevent_default_script_),
+                    argv.get(), argc, &ignored_result);
+  return true;
+}
+
+
+// Similarly to GearsHtmlEventListener::PreventDefaultEventBehavior, we use
+// an indirect script to invoke our known callback, in order to workaround
+// a mysterious TypeError when trying to invoke it directly.
+bool DropTarget::InvokeCallback(JsRootedCallback *callback, JsObject *arg) {
+  NPP npp = module_environment_->js_runner_->GetContext();
+  if (NPVARIANT_IS_VOID(compiled_invoke_callback_script_)) {
+    NPObject *global;
+    if (NPN_GetValue(npp, NPNVWindowNPObject, &global) != NPERR_NO_ERROR) {
+      return false;
+    }
+    ScopedNPObject scoped_global(global);
+
+    static const char script[] = "(function(f, arg) { f(arg); })";
+    NPString np_script = { script, ARRAYSIZE(script) - 1 };
+    if (!NPN_Evaluate(npp, global, &np_script,
+                      &compiled_invoke_callback_script_) ||
+        !NPVARIANT_IS_OBJECT(compiled_invoke_callback_script_)) {
+      return false;
+    }
+  }
+
+  ScopedNPVariant ignored_result;
+  const int argc = 2;
+  scoped_array<ScopedNPVariant> argv(new ScopedNPVariant[argc]);
+  argv[0].Reset(callback->token());
+  argv[1].Reset(arg->token());
+  NPN_InvokeDefault(npp, NPVARIANT_TO_OBJECT(compiled_invoke_callback_script_),
+                    argv.get(), argc, &ignored_result);
+  return true;
+}
+
+
+DropTarget::DropTarget(ModuleEnvironment *module_environment,
+                       JsObject *options,
+                       std::string16 *error_out)
+    : DropTargetBase(module_environment, options, error_out),
+      unregister_self_has_been_called_(false) {
+  LEAK_COUNTER_INCREMENT(DropTarget);
+}
+
+
+DropTarget::~DropTarget() {
+  LEAK_COUNTER_DECREMENT(DropTarget);
+}
+
+
+bool DropTarget::OnDragEnter(JsObject *event, DropTarget *drop_target) {
+  return drop_target->HandleDragAndDropEvent(event,
+                                             drop_target->on_drag_enter_.get(),
+                                             DND_TYPE_DRAG_ENTER);
+}
+
+
+bool DropTarget::OnDragOver(JsObject *event, DropTarget *drop_target) {
+  return drop_target->HandleDragAndDropEvent(event,
+                                             drop_target->on_drag_over_.get(),
+                                             DND_TYPE_DRAG_OVER);
+}
+
+
+bool DropTarget::OnDragLeave(JsObject *event, DropTarget *drop_target) {
+  return drop_target->HandleDragAndDropEvent(event,
+                                             drop_target->on_drag_leave_.get(),
+                                             DND_TYPE_DRAG_LEAVE);
+}
+
+
+bool DropTarget::OnDrop(JsObject *event, DropTarget *drop_target) {
+  return drop_target->HandleDragAndDropEvent(event,
+                                             drop_target->on_drop_.get(),
+                                             DND_TYPE_DROP);
+}
+
+
+bool DropTarget::HandleDragAndDropEvent(JsObject *event,
+                                        JsRootedCallback *callback,
+                                        DragAndDropType type) {
+  if ((type == DND_TYPE_DROP && !IsInADropOperation()) ||
+      (type != DND_TYPE_DROP && !IsInADragOperation())) {
+    // We're in a synthetic drag and drop callback invocation (i.e. one
+    // spoofed from JavaScript, possibly via dispatchEvent()) instead of
+    // a genuine one.
+    return false;
+  }
+  if (unregister_self_has_been_called_) {
+    return false;
+  }
+  scoped_ptr<JsObject> context_object(
+      module_environment_->js_runner_->NewObject());
+  context_object->SetPropertyObject(STRING16(L"event"), event);
+  if (type == DropTarget::DND_TYPE_DROP) {
+    scoped_ptr<JsArray> file_array(
+        module_environment_->js_runner_->NewArray());
+    if (GetDroppedFiles(module_environment_.get(), file_array.get(), true)) {
+      context_object->SetPropertyArray(STRING16(L"files"), file_array.get());
+    }
+  }
+  InvokeCallback(callback, context_object.get());
+  return true;
+}
+
+
+void DropTarget::SetEventListener(
+    const char *event_name,
+    scoped_refptr<GearsHtmlEventListener> *listener,
+    bool (*callback)(JsObject *, DropTarget *)) {
+  if (callback) {
+    if (!CreateModule<GearsHtmlEventListener>(
+            module_environment_.get(), NULL, listener)) {
+      return;
+    }
+    (*listener)->SetCallback(callback, this);
+  }
+
+  NPVariant args[3];
+  STRINGZ_TO_NPVARIANT(event_name, args[0]);
+  args[1] = (*listener)->GetWrapperToken();
+  BOOLEAN_TO_NPVARIANT(false, args[2]);
+
+  // TODO(nigeltao): For some reason, the removeEventListener call isn't
+  // working, and we're relying on the if (unregister_self_has_been_called_)
+  // check during HandleDragAndDropEvent to prevent the JS callbacks from
+  // firing after an unregisterDropTarget() call has been made. But really,
+  // we ought to remove our event listener.
+  NPIdentifier method_id = NPN_GetStringIdentifier(
+      callback ? "addEventListener" : "removeEventListener");
+  NPObject *element = NPVARIANT_TO_OBJECT(element_);
+  ScopedNPVariant ignored_result;
+  NPN_Invoke(module_environment_->js_runner_->GetContext(), element,
+             method_id, args, 3, &ignored_result);
+
+  if (!callback) {
+    listener->reset(NULL);
+  }
+}
+
+
+DropTarget *DropTarget::CreateDropTarget(ModuleEnvironment *module_environment,
+                                         JsDomElement &dom_element,
+                                         JsObject *options,
+                                         std::string16 *error_out) {
+  scoped_refptr<DropTarget> drop_target(new DropTarget(
+      module_environment, options, error_out));
+  if (!error_out->empty()) {
+    return NULL;
+  }
+
+  drop_target->Ref();  // Balanced by an Unref() call during UnregisterSelf.
+  JsObject *js_object = dom_element.js_object();
+  drop_target->element_.Reset(js_object->token());
+  if (drop_target->on_drag_enter_.get()) {
+    drop_target->SetEventListener("dragenter",
+                                  &drop_target->drag_enter_listener_,
+                                  &OnDragEnter);
+  }
+  if (drop_target->on_drag_over_.get()) {
+    drop_target->SetEventListener("dragover",
+                                  &drop_target->drag_over_listener_,
+                                  &OnDragOver);
+  }
+  if (drop_target->on_drag_leave_.get()) {
+    drop_target->SetEventListener("dragleave",
+                                  &drop_target->drag_leave_listener_,
+                                  &OnDragLeave);
+  }
+  if (drop_target->on_drop_.get()) {
+    drop_target->SetEventListener("drop",
+                                  &drop_target->drop_listener_,
+                                  &OnDrop);
+  }
+  return drop_target.get();
+}
+
+
+void DropTarget::UnregisterSelf() {
+  if (unregister_self_has_been_called_) {
+    return;
+  }
+  unregister_self_has_been_called_ = true;
+
+  if (drag_enter_listener_.get()) {
+    SetEventListener("dragenter", &drag_enter_listener_, NULL);
+  }
+  if (drag_over_listener_.get()) {
+    SetEventListener("dragover", &drag_over_listener_, NULL);
+  }
+  if (drag_leave_listener_.get()) {
+    SetEventListener("dragleave", &drag_leave_listener_, NULL);
+  }
+  if (drop_listener_.get()) {
+    SetEventListener("drop", &drop_listener_, NULL);
+  }
+
+  Unref();  // Balanced by a Ref() call during CreateDropTarget.
+}
+
+
+// A DropTarget instance automatically de-registers itself, on page unload.
+void DropTarget::HandleEvent(JsEventType event_type) {
+  assert(event_type == JSEVENT_UNLOAD);
+  UnregisterSelf();
+}
+
+
+#endif  // OFFICIAL_BUILD
==== //depot/googleclient/gears/opensource/gears/desktop/drop_target_sf.h#1 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/drop_target_sf.h
 ====
# action=add type=text
--- /dev/null   1970-01-01 10:00:00.000000000 +1000
+++ googleclient/gears/opensource/gears/desktop/drop_target_sf.h        
2008-11-10 18:51:11.000000000 +1100
@@ -0,0 +1,97 @@
+// Copyright 2008, Google Inc.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//  1. Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//  2. Redistributions in binary form must reproduce the above copyright 
notice,
+//     this list of conditions and the following disclaimer in the 
documentation
+//     and/or other materials provided with the distribution.
+//  3. Neither the name of Google Inc. nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef GEARS_DESKTOP_DROP_TARGET_SF_H__
+#define GEARS_DESKTOP_DROP_TARGET_SF_H__
+#ifdef OFFICIAL_BUILD
+// The Drag-and-Drop API has not been finalized for official builds.
+#else
+
+#include "gears/base/common/base_class.h"
+#include "gears/base/common/js_dom_element.h"
+#include "gears/base/common/js_runner.h"
+#include "gears/base/common/js_types.h"
+#include "gears/base/common/scoped_refptr.h"
+#include "gears/desktop/drop_target_base.h"
+#include "third_party/scoped_ptr/scoped_ptr.h"
+
+class GearsHtmlEventListener;
+
+class DropTarget
+    : public DropTargetBase,
+      public RefCounted {
+ public:
+  virtual ~DropTarget();
+
+  // The result should be held within a scoped_refptr.
+  static DropTarget *CreateDropTarget(ModuleEnvironment *module_environment,
+                                      JsDomElement &dom_element,
+                                      JsObject *options,
+                                      std::string16 *error_out);
+
+  void UnregisterSelf();
+
+  virtual void HandleEvent(JsEventType event_type);
+
+  enum DragAndDropType {
+    DND_TYPE_DRAG_ENTER,
+    DND_TYPE_DRAG_OVER,
+    DND_TYPE_DRAG_LEAVE,
+    DND_TYPE_DROP
+  };
+  bool HandleDragAndDropEvent(JsObject *event,
+                              JsRootedCallback *callback,
+                              DragAndDropType type);
+
+ private:
+  ScopedNPVariant element_;
+  scoped_refptr<GearsHtmlEventListener> drag_enter_listener_;
+  scoped_refptr<GearsHtmlEventListener> drag_over_listener_;
+  scoped_refptr<GearsHtmlEventListener> drag_leave_listener_;
+  scoped_refptr<GearsHtmlEventListener> drop_listener_;
+  ScopedNPVariant compiled_invoke_callback_script_;
+  bool unregister_self_has_been_called_;
+
+  static bool OnDragEnter(JsObject *event, DropTarget *drop_target);
+  static bool OnDragOver(JsObject *event, DropTarget *drop_target);
+  static bool OnDragLeave(JsObject *event, DropTarget *drop_target);
+  static bool OnDrop(JsObject *event, DropTarget *drop_target);
+
+  DropTarget(ModuleEnvironment *module_environment,
+             JsObject *options,
+             std::string16 *error_out);
+
+  bool InvokeCallback(JsRootedCallback *callback, JsObject *arg);
+
+  // Pass a NULL callback to remove a previously added listener.
+  void SetEventListener(const char *event_name,
+                        scoped_refptr<GearsHtmlEventListener> *listener,
+                        bool (*callback)(JsObject *, DropTarget *));
+  
+  DISALLOW_EVIL_CONSTRUCTORS(DropTarget);
+};
+
+#endif  // OFFICIAL_BUILD
+#endif  // GEARS_DESKTOP_DROP_TARGET_SF_H__

Reply via email to