Hello noel,
I'd like you to do a code review. Please execute
g4 diff -c 10361128
or point your web browser to
http://mondrian/10361128
to review the following code:
Change 10361128 by nigel...@nigeltao-srcgears5 on 2009/03/04 17:21:34 *pending*
Introduce the desktop.extractMetaData method.
PRESUBMIT=passed
R=noel
[email protected]
DELTA=318 (317 added, 0 deleted, 1 changed)
OCL=10361128
Affected files ...
... //depot/googleclient/gears/opensource/gears/Makefile#238 edit
... //depot/googleclient/gears/opensource/gears/desktop/desktop.cc#81 edit
... //depot/googleclient/gears/opensource/gears/desktop/desktop.h#29 edit
...
//depot/googleclient/gears/opensource/gears/desktop/meta_data_extraction.cc#1
add
...
//depot/googleclient/gears/opensource/gears/desktop/meta_data_extraction.h#1 add
...
//depot/googleclient/gears/opensource/gears/test/manual/drag_and_drop.html#9
edit
318 delta lines: 317 added, 0 deleted, 1 changed
Also consider running:
g4 lint -c 10361128
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 10361128 by nigel...@nigeltao-srcgears5 on 2009/03/04 17:21:34 *pending*
Introduce the desktop.extractMetaData method.
Affected files ...
... //depot/googleclient/gears/opensource/gears/Makefile#238 edit
... //depot/googleclient/gears/opensource/gears/desktop/desktop.cc#81 edit
... //depot/googleclient/gears/opensource/gears/desktop/desktop.h#29 edit
...
//depot/googleclient/gears/opensource/gears/desktop/meta_data_extraction.cc#1
add
...
//depot/googleclient/gears/opensource/gears/desktop/meta_data_extraction.h#1 add
...
//depot/googleclient/gears/opensource/gears/test/manual/drag_and_drop.html#9
edit
==== //depot/googleclient/gears/opensource/gears/Makefile#238 -
/home/nigeltao/srcgears5/googleclient/gears/opensource/gears/Makefile ====
# action=edit type=text
--- googleclient/gears/opensource/gears/Makefile 2009-03-04
17:16:19.000000000 +1100
+++ googleclient/gears/opensource/gears/Makefile 2009-03-03
20:53:59.000000000 +1100
@@ -1387,6 +1387,7 @@
dll_data_wince.cc \
drop_target_base.cc \
drop_target_registration.cc \
+ meta_data_extraction.cc \
shortcut_utils_win32.cc \
$(NULL)
==== //depot/googleclient/gears/opensource/gears/desktop/desktop.cc#81 -
/home/nigeltao/srcgears5/googleclient/gears/opensource/gears/desktop/desktop.cc
====
# action=edit type=text
--- googleclient/gears/opensource/gears/desktop/desktop.cc 2009-03-03
17:05:38.000000000 +1100
+++ googleclient/gears/opensource/gears/desktop/desktop.cc 2009-03-04
17:16:57.000000000 +1100
@@ -43,9 +43,11 @@
#ifdef BROWSER_WEBKIT
#include "gears/base/safari/curl_downloader.h"
#endif
+#include "gears/blob/blob.h"
#include "gears/blob/blob_interface.h"
#include "gears/desktop/drop_target_registration.h"
#include "gears/desktop/file_dialog.h"
+#include "gears/desktop/meta_data_extraction.h"
#include "gears/localserver/common/http_constants.h"
#include "gears/localserver/common/http_request.h"
#include "gears/ui/common/html_dialog.h"
@@ -78,6 +80,7 @@
#if GEARS_DRAG_AND_DROP_API_IS_SUPPORTED_FOR_THIS_PLATFORM
// TODO(nigeltao): should acceptDrag be renamed finishDrag??
RegisterMethod("acceptDrag", &GearsDesktop::AcceptDrag);
+ RegisterMethod("extractMetaData", &GearsDesktop::ExtractMetaData);
RegisterMethod("getDragData", &GearsDesktop::GetDragData);
RegisterMethod("registerDropTarget", &GearsDesktop::RegisterDropTarget);
RegisterMethod("setDragCursor", &GearsDesktop::SetDragCursor);
@@ -904,6 +907,29 @@
}
+void GearsDesktop::ExtractMetaData(JsCallContext *context) {
+ ModuleImplBaseClass *other_module = NULL;
+
+ JsArgument argv[] = {
+ { JSPARAM_REQUIRED, JSPARAM_MODULE, &other_module },
+ };
+ context->GetArguments(ARRAYSIZE(argv), argv);
+ if (context->is_exception_set()) return;
+
+ if (GearsBlob::kModuleName != other_module->get_module_name()) {
+ context->SetException(STRING16(L"First argument must be a Blob."));
+ return;
+ }
+ scoped_refptr<BlobInterface> blob_contents;
+ static_cast<GearsBlob*>(other_module)->GetContents(&blob_contents);
+ assert(blob_contents.get());
+
+ scoped_ptr<JsObject> js_object(module_environment_->js_runner_->NewObject());
+ ::ExtractMetaData(blob_contents.get(), js_object.get());
+ context->SetReturnValue(JSPARAM_OBJECT, js_object.get());
+}
+
+
void GearsDesktop::GetDragData(JsCallContext *context) {
if (EnvIsWorker()) {
context->SetException(
==== //depot/googleclient/gears/opensource/gears/desktop/desktop.h#29 -
/home/nigeltao/srcgears5/googleclient/gears/opensource/gears/desktop/desktop.h
====
# action=edit type=text
--- googleclient/gears/opensource/gears/desktop/desktop.h 2009-03-03
17:05:38.000000000 +1100
+++ googleclient/gears/opensource/gears/desktop/desktop.h 2009-03-04
11:46:43.000000000 +1100
@@ -187,6 +187,10 @@
// OUT: -
void AcceptDrag(JsCallContext *context);
+ // IN: GearsBlob blob
+ // OUT: object metadata
+ void ExtractMetaData(JsCallContext *context);
+
// IN: Event event, string flavor
// OUT: object data
void GetDragData(JsCallContext *context);
====
//depot/googleclient/gears/opensource/gears/desktop/meta_data_extraction.cc#1 -
/home/nigeltao/srcgears5/googleclient/gears/opensource/gears/desktop/meta_data_extraction.cc
====
# action=add type=text
--- /dev/null 1970-01-01 10:00:00.000000000 +1000
+++ googleclient/gears/opensource/gears/desktop/meta_data_extraction.cc
2009-03-04 17:21:17.000000000 +1100
@@ -0,0 +1,246 @@
+// Copyright 2009, 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/meta_data_extraction.h"
+
+#include "gears/base/common/js_types.h"
+#include "gears/blob/blob_interface.h"
+#include "third_party/libpng/png.h"
+
+extern "C" {
+#include "third_party/libjpeg/jpeglib.h"
+}
+
+
+// Make an arbitrary limit (of 2^16 pixels wide, or 2^16 pixels high) for what
+// images we deem reasonable to deal with in Gears.
+static const unsigned int kMaxReasonableImageWidthHeight = 65536;
+
+
+//-----------------------------------------------------------------------------
+// JPEG meta-data extraction functions
+
+
+struct JpegBlobErrorMgr {
+ jpeg_error_mgr parent_implementation;
+ jmp_buf setjmp_buffer;
+};
+
+
+static void JpegBlobErrorExit(j_common_ptr c) {
+ JpegBlobErrorMgr *jbem = reinterpret_cast<JpegBlobErrorMgr*>(c->err);
+ longjmp(jbem->setjmp_buffer, 1);
+}
+
+
+struct JpegBlobReadContext {
+ BlobInterface *blob;
+ int64 offset;
+ uint8 buffer[1024];
+};
+
+
+static void JpegBlobInitSource(j_decompress_ptr jd) {
+ JpegBlobReadContext *context =
+ reinterpret_cast<JpegBlobReadContext*>(jd->client_data);
+ jd->src->next_input_byte = context->buffer;
+ jd->src->bytes_in_buffer = 0;
+}
+
+
+static boolean JpegBlobFillInputBuffer(j_decompress_ptr jd) {
+ JpegBlobReadContext *context =
+ reinterpret_cast<JpegBlobReadContext*>(jd->client_data);
+ int bytes_read = static_cast<int>(
+ context->blob->Read(context->buffer, context->offset, 1024));
+ if (bytes_read <= 0) {
+ return FALSE;
+ }
+ assert(bytes_read >= 0 && bytes_read <= 1024);
+ context->offset += bytes_read;
+ jd->src->next_input_byte = context->buffer;
+ jd->src->bytes_in_buffer = bytes_read;
+ return TRUE;
+}
+
+
+static void JpegBlobSkipInputData(j_decompress_ptr jd, long num_bytes) {
+ if (num_bytes <= 0) {
+ return;
+ }
+ JpegBlobReadContext *context =
+ reinterpret_cast<JpegBlobReadContext*>(jd->client_data);
+ if (static_cast<unsigned int>(num_bytes) < jd->src->bytes_in_buffer) {
+ jd->src->next_input_byte += num_bytes;
+ jd->src->bytes_in_buffer -= num_bytes;
+ } else {
+ context->offset += num_bytes;
+ jd->src->next_input_byte = context->buffer;
+ jd->src->bytes_in_buffer = 0;
+ }
+}
+
+
+static void JpegBlobTermSource(j_decompress_ptr jd) {
+ // No-op.
+}
+
+
+static bool ExtractMetaDataJpeg(BlobInterface *blob, JsObject *result) {
+ JpegBlobReadContext context;
+ context.blob = blob;
+ context.offset = 0;
+
+ jpeg_decompress_struct jd;
+ memset(&jd, 0, sizeof(jd));
+ jpeg_create_decompress(&jd);
+ jd.client_data = &context;
+
+ JpegBlobErrorMgr jbem;
+ memset(&jbem, 0, sizeof(jbem));
+ jd.err = jpeg_std_error(&jbem.parent_implementation);
+ jd.err->error_exit = JpegBlobErrorExit;
+ if (setjmp(jbem.setjmp_buffer)) {
+ jpeg_destroy_decompress(&jd);
+ return false;
+ }
+
+ jpeg_source_mgr jsm;
+ memset(&jsm, 0, sizeof(jsm));
+ jd.src = &jsm;
+ jsm.init_source = JpegBlobInitSource;
+ jsm.fill_input_buffer = JpegBlobFillInputBuffer;
+ jsm.skip_input_data = JpegBlobSkipInputData;
+ jsm.resync_to_restart = jpeg_resync_to_restart;
+ jsm.term_source = JpegBlobTermSource;
+
+ jpeg_read_header(&jd, TRUE);
+
+ if ((jd.image_width > kMaxReasonableImageWidthHeight) ||
+ (jd.image_height > kMaxReasonableImageWidthHeight)) {
+ jpeg_destroy_decompress(&jd);
+ return false;
+ }
+ result->SetPropertyInt(
+ std::string16(STRING16(L"imageWidth")),
+ static_cast<int>(jd.image_width));
+ result->SetPropertyInt(
+ std::string16(STRING16(L"imageHeight")),
+ static_cast<int>(jd.image_height));
+ result->SetPropertyString(
+ std::string16(STRING16(L"mimeType")),
+ std::string16(STRING16(L"image/jpeg")));
+
+ jpeg_destroy_decompress(&jd);
+ return true;
+}
+
+
+//-----------------------------------------------------------------------------
+// PNG meta-data extraction functions
+
+
+struct PngBlobReadContext {
+ BlobInterface *blob;
+ int64 offset;
+};
+
+
+static void PngBlobReadFunction(
+ png_structp png_ptr, png_bytep data, png_size_t length) {
+ PngBlobReadContext *context =
+ reinterpret_cast<PngBlobReadContext*>(png_get_io_ptr(png_ptr));
+ if (length != context->blob->Read(data, context->offset, length)) {
+ png_error(png_ptr, "Incomplete read from Blob");
+ }
+ context->offset += length;
+}
+
+
+static bool ExtractMetaDataPng(BlobInterface *blob, JsObject *result) {
+ static const int kPngMagicHeaderLength = 8;
+ if (blob->Length() < kPngMagicHeaderLength) {
+ return false;
+ }
+ uint8 magic_header[kPngMagicHeaderLength];
+ if (kPngMagicHeaderLength !=
+ blob->Read(magic_header, 0, kPngMagicHeaderLength)) {
+ return false;
+ }
+ if (png_sig_cmp(magic_header, 0, kPngMagicHeaderLength) != 0) {
+ return false;
+ }
+
+ png_structp png_ptr = png_create_read_struct(
+ PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (!png_ptr) {
+ return false;
+ }
+ png_infop info_ptr = png_create_info_struct(png_ptr);
+ if (!info_ptr) {
+ png_destroy_read_struct(&png_ptr, NULL, NULL);
+ return false;
+ }
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
+ return false;
+ }
+
+ PngBlobReadContext context;
+ context.blob = blob;
+ context.offset = kPngMagicHeaderLength;
+ png_set_sig_bytes(png_ptr, kPngMagicHeaderLength);
+ png_set_read_fn(png_ptr, &context, PngBlobReadFunction);
+ png_read_info(png_ptr, info_ptr);
+
+ png_uint_32 width, height;
+ int bit_depth, color_type;
+ png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
+ NULL, NULL, NULL);
+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
+
+ if ((width > kMaxReasonableImageWidthHeight) ||
+ (height > kMaxReasonableImageWidthHeight)) {
+ return false;
+ }
+ result->SetPropertyInt(
+ std::string16(STRING16(L"imageWidth")), static_cast<int>(width));
+ result->SetPropertyInt(
+ std::string16(STRING16(L"imageHeight")), static_cast<int>(height));
+ result->SetPropertyString(
+ std::string16(STRING16(L"mimeType")),
+ std::string16(STRING16(L"image/png")));
+ return true;
+}
+
+
+//-----------------------------------------------------------------------------
+// Public API
+
+
+void ExtractMetaData(BlobInterface *blob, JsObject *result) {
+ if (ExtractMetaDataJpeg(blob, result)) return;
+ if (ExtractMetaDataPng(blob, result)) return;
+}
====
//depot/googleclient/gears/opensource/gears/desktop/meta_data_extraction.h#1 -
/home/nigeltao/srcgears5/googleclient/gears/opensource/gears/desktop/meta_data_extraction.h
====
# action=add type=text
--- /dev/null 1970-01-01 10:00:00.000000000 +1000
+++ googleclient/gears/opensource/gears/desktop/meta_data_extraction.h
2009-03-04 16:55:35.000000000 +1100
@@ -0,0 +1,34 @@
+// Copyright 2009, 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_META_DATA_EXTRACTION_H__
+#define GEARS_DESKTOP_META_DATA_EXTRACTION_H__
+
+class BlobInterface;
+class JsObject;
+
+void ExtractMetaData(BlobInterface *blob, JsObject *result);
+
+#endif // GEARS_DESKTOP_META_DATA_EXTRACTION_H__
====
//depot/googleclient/gears/opensource/gears/test/manual/drag_and_drop.html#9 -
/home/nigeltao/srcgears5/googleclient/gears/opensource/gears/test/manual/drag_and_drop.html
====
# action=edit type=text
--- googleclient/gears/opensource/gears/test/manual/drag_and_drop.html
2009-03-04 17:16:20.000000000 +1100
+++ googleclient/gears/opensource/gears/test/manual/drag_and_drop.html
2009-03-04 17:23:49.000000000 +1100
@@ -99,7 +99,13 @@
for (i = 0; i < context.files.length; i++) {
var file = context.files[i];
s += '<b>' + file.name + '</b> has length <b>' +
- file.blob.length + '</b><br>';
+ file.blob.length + '</b>';
+ var md = desktop.extractMetaData(file.blob);
+ if (md.imageWidth && md.imageHeight) {
+ s += ' and dimensions <b>' + md.imageWidth + '</b>×<b>' +
+ md.imageHeight + '</b>';
+ }
+ s += '<br>';
}
}
s += 'count: <b>' + context.count + '</b><br>';