Hello noel, playmobil,

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

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

to review the following code:

Change 9262838 by [EMAIL PROTECTED] on 2008/12/04 20:44:43 *pending*

        Provided aggregate metadata, for file drag and drop, on Safari.
        This is the Safari analogue of Firefox's CL 9228479.
        
        PRESUBMIT=passed
        R=noel,playmobil
        [EMAIL PROTECTED]
        DELTA=207  (165 added, 21 deleted, 21 changed)
        OCL=9262838

Affected files ...

... //depot/googleclient/gears/opensource/gears/Makefile#216 edit
... //depot/googleclient/gears/opensource/gears/base/common/mime_detect.cc#2 
edit
... 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_common.cc#1
 add
... 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_common.h#1
 add
... 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_sf.h#4 
edit
... 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_sf.mm#5 
edit
... //depot/googleclient/gears/opensource/gears/desktop/drop_target_sf.cc#4 edit

207 delta lines: 165 added, 21 deleted, 21 changed

Also consider running:
        g4 lint -c 9262838

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 9262838 by [EMAIL PROTECTED] on 2008/12/04 20:44:43 *pending*

        Provided aggregate metadata, for file drag and drop, on Safari.
        This is the Safari analogue of Firefox's CL 9228479.
        
        OCL=9262838

Affected files ...

... //depot/googleclient/gears/opensource/gears/Makefile#216 edit
... //depot/googleclient/gears/opensource/gears/base/common/mime_detect.cc#2 
edit
... 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_common.cc#1
 add
... 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_common.h#1
 add
... 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_sf.h#4 
edit
... 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_sf.mm#5 
edit
... //depot/googleclient/gears/opensource/gears/desktop/drop_target_sf.cc#4 edit

==== //depot/googleclient/gears/opensource/gears/Makefile#216 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/Makefile 
====
# action=edit type=text
--- googleclient/gears/opensource/gears/Makefile        2008-12-04 
18:16:18.000000000 +1100
+++ googleclient/gears/opensource/gears/Makefile        2008-12-04 
16:04:27.000000000 +1100
@@ -1481,6 +1481,17 @@
                notification.cc \
                $(NULL)
 
+CHROME_CPPSRCS += \
+               desktop_cr.cc \
+               $(NULL)
+
+ifeq ($(OFFICIAL_BUILD),1)
+# The Drag-and-Drop API has not been finalized for official builds.
+else
+$(BROWSER)_CPPSRCS += \
+               drag_and_drop_utils_common.cc \
+               $(NULL)
+
 FF3_CPPSRCS += \
                drag_and_drop_utils_ff.cc \
                drop_target_ff.cc \
@@ -1491,13 +1502,6 @@
                drop_target_ie.cc \
                $(NULL)
 
-CHROME_CPPSRCS += \
-               desktop_cr.cc \
-               $(NULL)
-
-ifeq ($(OFFICIAL_BUILD),1)
-# The Drag-and-Drop API has not been finalized for official builds.
-else
 SF_CPPSRCS += \
                drag_and_drop_utils_sf.mm \
                drop_target_sf.cc \
==== //depot/googleclient/gears/opensource/gears/base/common/mime_detect.cc#2 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/base/common/mime_detect.cc
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/base/common/mime_detect.cc      
2008-12-04 15:19:07.000000000 +1100
+++ googleclient/gears/opensource/gears/base/common/mime_detect.cc      
2008-12-04 16:54:29.000000000 +1100
@@ -39,6 +39,10 @@
 #elif defined(WIN32) && !defined(OS_WINCE)
 #include <shlwapi.h>
 #include <windows.h>
+
+#elif defined(OS_MACOSX)
+#include "gears/base/common/string_utils_osx.h"
+#include "gears/base/safari/scoped_cf.h"
 #endif
 
 
@@ -93,6 +97,24 @@
   CoTaskMemFree(mime_type);
   return result;
 
+#elif defined(OS_MACOSX)
+  const char16 *file_extension_ptr = File::GetFileExtension(filename.c_str());
+  if (*file_extension_ptr == static_cast<char16>(0)) {
+    return kDefaultMimeType;
+  }
+  // The +1 is to strip the leading ".", since the UTxxx API expects the
+  // extension to be something like "jpeg" instead of ".jpeg".
+  std::string16 file_extension(file_extension_ptr + 1);
+  scoped_cftype<CFStringRef> file_extension_cfstr(CFStringCreateWithCharacters(
+      NULL, file_extension.c_str(), file_extension.length()));
+  scoped_cftype<CFStringRef> uti(UTTypeCreatePreferredIdentifierForTag(
+      kUTTagClassFilenameExtension, file_extension_cfstr.get(), NULL));
+  scoped_cftype<CFStringRef> mime_cfstr(UTTypeCopyPreferredTagWithClass(
+      uti.get(), kUTTagClassMIMEType));
+  std::string16 result;
+  CFStringRefToString16(mime_cfstr.get(), &result);
+  return result.empty() ? kDefaultMimeType : result;
+
 #else
   return kDefaultMimeType;
 #endif
==== 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_common.cc#1
 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_common.cc
 ====
# action=add type=text
--- /dev/null   1970-01-01 10:00:00.000000000 +1000
+++ googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_common.cc   
2008-12-04 16:59:11.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.
+
+#include "gears/desktop/drag_and_drop_utils_common.h"
+
+bool AppendStringsToJsArray(const std::set<std::string16> &strings,
+                            JsArray *array) {
+  int length = 0;
+  if (!array || !array->GetLength(&length)) {
+    return false;
+  }
+  for (std::set<std::string16>::const_iterator i = strings.begin();
+       i != strings.end(); ++i) {
+    if (!array->SetElementString(length++, *i)) {
+      return false;
+    }
+  }
+  return true;
+}
==== 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_common.h#1
 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_common.h
 ====
# action=add type=text
--- /dev/null   1970-01-01 10:00:00.000000000 +1000
+++ googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_common.h    
2008-12-04 16:08:47.000000000 +1100
@@ -0,0 +1,35 @@
+// 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_COMMON_H__
+#define GEARS_DESKTOP_DRAG_AND_DROP_UTILS_COMMON_H__
+
+#include <set>
+#include "gears/base/common/js_types.h"
+
+bool AppendStringsToJsArray(const std::set<std::string16> &strings,
+                            JsArray *array);
+
+#endif  // GEARS_DESKTOP_DRAG_AND_DROP_UTILS_COMMON_H__
==== 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_sf.h#4 
- 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_sf.h
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_sf.h        
2008-12-01 13:36:46.000000000 +1100
+++ googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_sf.h        
2008-12-04 15:50:58.000000000 +1100
@@ -49,9 +49,9 @@
 // actually swizzle.
 bool SwizzleWebViewMethods();
 
-bool GetDroppedFiles(ModuleEnvironment *module_environment,
-                     JsArray *files_out,
-                     std::string16 *error_out);
+bool AddFileDragAndDropData(ModuleEnvironment *module_environment,
+                            JsObject *data_out,
+                            std::string16 *error_out);
 
 // These two are mutually exclusive (although they may be both false) -
 // "in a drag" means dragenter, dragover or dragleave, but not drop.
==== 
//depot/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_sf.mm#5 
- 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_sf.mm
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_sf.mm       
2008-12-02 16:28:48.000000000 +1100
+++ googleclient/gears/opensource/gears/desktop/drag_and_drop_utils_sf.mm       
2008-12-04 18:03:37.000000000 +1100
@@ -27,17 +27,31 @@
 #import <WebKit/DOM.h>
 #import <WebKit/WebView.h>
 #import <objc/objc-class.h>
+#import <set>
 #import <vector>
 
 #import "gears/desktop/drag_and_drop_utils_sf.h"
 
 #import "gears/base/common/common.h"
+#import "gears/base/common/file.h"
+#import "gears/base/common/mime_detect.h"
 #import "gears/base/safari/nsstring_utils.h"
+#import "gears/desktop/drag_and_drop_utils_common.h"
 #import "gears/desktop/file_dialog.h"
 
 static std::vector<std::string16> g_dragging_pasteboard_filenames;
+static std::set<std::string16> g_dragging_pasteboard_file_extensions;
+static std::set<std::string16> g_dragging_pasteboard_file_mime_types;
+static int g_dragging_pasteboard_file_total_bytes = 0;
 static bool g_is_in_a_drag_operation = false;
 static bool g_is_in_a_drop_operation = false;
+
+static void ResetGlobalDraggingPasteboardState() {
+  g_dragging_pasteboard_filenames.clear();
+  g_dragging_pasteboard_file_extensions.clear();
+  g_dragging_pasteboard_file_mime_types.clear();
+  g_dragging_pasteboard_file_total_bytes = 0;
+}
 
 bool IsInADragOperation() {
   return g_is_in_a_drag_operation;
@@ -143,6 +157,16 @@
         std::string16 std_string;
         ResolveAlias(ns_string, &std_string);
         g_dragging_pasteboard_filenames.push_back(std_string);
+        g_dragging_pasteboard_file_mime_types.insert(
+            DetectMimeTypeOfFile(std_string));
+        std::string16 extension(File::GetFileExtension(std_string.c_str()));
+        if (!extension.empty()) {
+          g_dragging_pasteboard_file_extensions.insert(extension);
+        }
+        int64 file_size = File::GetFileSize(std_string.c_str());
+        if (file_size != File::kInvalidSize) {
+          g_dragging_pasteboard_file_total_bytes += file_size;
+        }
       }
     }
   }
@@ -158,14 +182,14 @@
 }
 
 - (NSDragOperation)swizzledDraggingExited:(id <NSDraggingInfo>)draggingInfo {
-  g_dragging_pasteboard_filenames.clear();
+  ResetGlobalDraggingPasteboardState();
   return [self swizzledDraggingExited:draggingInfo];
 }
 
 - (BOOL)swizzledPerformDragOperation:(id <NSDraggingInfo>)draggingInfo {
   g_is_in_a_drop_operation = true;
   BOOL result = [self swizzledPerformDragOperation:draggingInfo];
-  g_dragging_pasteboard_filenames.clear();
+  ResetGlobalDraggingPasteboardState();
   g_is_in_a_drop_operation = false;
   return result;
 }
@@ -191,14 +215,46 @@
   return true;
 }
 
-bool GetDroppedFiles(ModuleEnvironment *module_environment,
-                     JsArray *files_out,
-                     std::string16 *error_out) {
-  return FileDialog::FilesToJsObjectArray(
-      g_dragging_pasteboard_filenames,
-      module_environment,
-      files_out,
-      error_out);
+bool AddFileDragAndDropData(ModuleEnvironment *module_environment,
+                            JsObject *data_out,
+                            std::string16 *error_out) {
+  // TODO(nigeltao): Error checking. We should return emtpy (or 0) instead of
+  // partial results, in case of failure.
+  data_out->SetPropertyInt(
+      STRING16(L"fileCount"),
+      g_dragging_pasteboard_filenames.size());
+  data_out->SetPropertyDouble(
+      STRING16(L"fileTotalBytes"),
+      static_cast<double>(g_dragging_pasteboard_file_total_bytes));
+
+  scoped_ptr<JsArray> extensions_array(
+      module_environment->js_runner_->NewArray());
+  AppendStringsToJsArray(g_dragging_pasteboard_file_extensions,
+      extensions_array.get());
+  data_out->SetPropertyArray(STRING16(L"fileExtensions"),
+      extensions_array.get());
+
+  scoped_ptr<JsArray> mime_types_array(
+      module_environment->js_runner_->NewArray());
+  AppendStringsToJsArray(g_dragging_pasteboard_file_mime_types,
+      mime_types_array.get());
+  data_out->SetPropertyArray(STRING16(L"fileMimeTypes"),
+      mime_types_array.get());
+
+  if (IsInADropOperation()) {
+    scoped_ptr<JsArray> file_array(
+        module_environment->js_runner_->NewArray());
+    if (!FileDialog::FilesToJsObjectArray(
+            g_dragging_pasteboard_filenames,
+            module_environment,
+            file_array.get(),
+            error_out)) {
+      assert(!error_out->empty());
+      return false;
+    }
+    data_out->SetPropertyArray(STRING16(L"files"), file_array.get());
+  }
+  return true;
 };
 
 void AcceptDrag(ModuleEnvironment *module_environment,
@@ -222,17 +278,7 @@
     *error_out = STRING16(L"The drag-and-drop event is invalid.");
     return;
   }
-
-  if (IsInADropOperation()) {
-    scoped_ptr<JsArray> file_array(
-        module_environment->js_runner_->NewArray());
-    if (!GetDroppedFiles(module_environment, file_array.get(), error_out)) {
-      return;
-    }
-    data_out->SetPropertyArray(STRING16(L"files"), file_array.get());
-  }
-
-  // TODO(nigeltao): For all DnD events (including the non-drop events
-  // dragenter and dragover), we still should provide (1) MIME types,
-  // (2) file extensions, (3) total file size, and (4) file count.
-}
+  if (!AddFileDragAndDropData(module_environment, data_out, error_out)) {
+    assert(!error_out->empty());
+  }
+}
==== //depot/googleclient/gears/opensource/gears/desktop/drop_target_sf.cc#4 - 
/Users/nigeltao/devel/srcmacgears1/googleclient/gears/opensource/gears/desktop/drop_target_sf.cc
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/desktop/drop_target_sf.cc       
2008-12-01 13:36:47.000000000 +1100
+++ googleclient/gears/opensource/gears/desktop/drop_target_sf.cc       
2008-12-04 20:41:16.000000000 +1100
@@ -220,14 +220,10 @@
   scoped_ptr<JsObject> context_object(
       module_environment_->js_runner_->NewObject());
   context_object->SetPropertyObject(STRING16(L"event"), event);
-  if (type == HTML_EVENT_TYPE_DROP) {
-    scoped_ptr<JsArray> file_array(
-        module_environment_->js_runner_->NewArray());
-    std::string16 ignored;
-    if (GetDroppedFiles(module_environment_.get(), file_array.get(),
-                        &ignored)) {
-      context_object->SetPropertyArray(STRING16(L"files"), file_array.get());
-    }
+  std::string16 ignored;
+  if (!AddFileDragAndDropData(
+          module_environment_.get(), context_object.get(), &ignored)) {
+    return false;
   }
   InvokeCallback(callback, context_object.get());
   return true;

Reply via email to