Hello aa,

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

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

to review the following code:

Change 10257353 by nigel...@nigeltao-srcgears2 on 2009/02/24 18:02:03 *pending*

        Introduce the workerpool.location property.
        
        PRESUBMIT=passed
        R=aa
        [email protected]
        DELTA=306  (304 added, 1 deleted, 1 changed)
        OCL=10257353

Affected files ...

... //depot/googleclient/gears/opensource/gears/Makefile#234 edit
... //depot/googleclient/gears/opensource/gears/test/testcases/config.js#39 edit
... 
//depot/googleclient/gears/opensource/gears/test/testcases/test_worker_location.worker.js#1
 add
... 
//depot/googleclient/gears/opensource/gears/test/testcases/workerpool_createworkerfromurl_tests.js#8
 edit
... 
//depot/googleclient/gears/opensource/gears/test/testcases/workerpool_tests.js#4
 edit
... //depot/googleclient/gears/opensource/gears/workerpool/location.cc#1 add
... //depot/googleclient/gears/opensource/gears/workerpool/location.h#1 add
... //depot/googleclient/gears/opensource/gears/workerpool/workerpool.cc#9 edit
... //depot/googleclient/gears/opensource/gears/workerpool/workerpool.h#2 edit

306 delta lines: 304 added, 1 deleted, 1 changed

Also consider running:
        g4 lint -c 10257353

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 10257353 by nigel...@nigeltao-srcgears2 on 2009/02/24 18:02:03 *pending*

        Introduce the workerpool.location property.

Affected files ...

... //depot/googleclient/gears/opensource/gears/Makefile#234 edit
... //depot/googleclient/gears/opensource/gears/test/testcases/config.js#39 edit
... 
//depot/googleclient/gears/opensource/gears/test/testcases/test_worker_location.worker.js#1
 add
... 
//depot/googleclient/gears/opensource/gears/test/testcases/workerpool_createworkerfromurl_tests.js#8
 edit
... 
//depot/googleclient/gears/opensource/gears/test/testcases/workerpool_tests.js#4
 edit
... //depot/googleclient/gears/opensource/gears/workerpool/location.cc#1 add
... //depot/googleclient/gears/opensource/gears/workerpool/location.h#1 add
... //depot/googleclient/gears/opensource/gears/workerpool/workerpool.cc#9 edit
... //depot/googleclient/gears/opensource/gears/workerpool/workerpool.h#2 edit

==== //depot/googleclient/gears/opensource/gears/Makefile#234 - 
/home/nigeltao/srcgears2/googleclient/gears/opensource/gears/Makefile ====
# action=edit type=text
--- googleclient/gears/opensource/gears/Makefile        2009-02-24 
17:59:55.000000000 +1100
+++ googleclient/gears/opensource/gears/Makefile        2009-02-24 
11:13:06.000000000 +1100
@@ -1824,6 +1824,7 @@
                $(NULL)
 
 FF3_CPPSRCS    += \
+               location.cc \
                pool_threads_manager.cc \
                workerpool.cc \
                workerpool_utils.cc \
@@ -1839,6 +1840,7 @@
                $(NULL)
 
 IE_CPPSRCS     += \
+               location.cc \
                pool_threads_manager.cc \
                workerpool.cc \
                workerpool_utils.cc \
@@ -1855,6 +1857,7 @@
                $(NULL)
 
 $(BROWSER)_CPPSRCS     += \
+               location.cc \
                pool_threads_manager.cc \
                workerpool.cc \
                workerpool_utils.cc \
==== //depot/googleclient/gears/opensource/gears/test/testcases/config.js#39 - 
/home/nigeltao/srcgears2/googleclient/gears/opensource/gears/test/testcases/config.js
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/test/testcases/config.js        
2009-02-24 17:59:56.000000000 +1100
+++ googleclient/gears/opensource/gears/test/testcases/config.js        
2009-02-24 18:21:37.000000000 +1100
@@ -161,8 +161,7 @@
                         {useWorker: true, useIFrame: true});
 workerPoolSuite.addFile('../testcases/workerpool_onerror_tests.js',
                         {useWorker: true, useIFrame: true});
-workerPoolSuite.addFile(
-                        '../testcases/workerpool_createworkerfromurl_tests.js',
+workerPoolSuite.addFile('../testcases/workerpool_createworkerfromurl_tests.js',
                         {useWorker: false, useIFrame: true});
 workerPoolSuite.addFile('../testcases/workerpool_message_body_tests.js',
                         {useWorker: true, useIFrame: true});
==== 
//depot/googleclient/gears/opensource/gears/test/testcases/test_worker_location.worker.js#1
 - 
/home/nigeltao/srcgears2/googleclient/gears/opensource/gears/test/testcases/test_worker_location.worker.js
 ====
# action=add type=text
--- /dev/null   1970-01-01 10:00:00.000000000 +1000
+++ 
googleclient/gears/opensource/gears/test/testcases/test_worker_location.worker.js
   2009-02-24 18:20:15.000000000 +1100
@@ -0,0 +1,8 @@
+var wp = google.gears.workerPool;
+wp.onmessage = function(text, sender, msg) {
+  wp.sendMessage({
+    echo: msg.body,
+    masterOrigin: msg.origin,
+    workerOrigin: wp.location.protocol + '//' + wp.location.host
+  }, sender);
+};
==== 
//depot/googleclient/gears/opensource/gears/test/testcases/workerpool_createworkerfromurl_tests.js#8
 - 
/home/nigeltao/srcgears2/googleclient/gears/opensource/gears/test/testcases/workerpool_createworkerfromurl_tests.js
 ====
# action=edit type=text
--- 
googleclient/gears/opensource/gears/test/testcases/workerpool_createworkerfromurl_tests.js
  2009-02-24 17:59:56.000000000 +1100
+++ 
googleclient/gears/opensource/gears/test/testcases/workerpool_createworkerfromurl_tests.js
  2009-02-24 18:21:27.000000000 +1100
@@ -41,6 +41,9 @@
 // This worker expects to be run out-of-origin, and calls allowCrossOrigin().
 var crossOriginWorkerPath = '/testcases/test_worker_cross_origin.worker.js';
 
+// This worker replies with its workerpool.location information.
+var locationWorkerPath = '/testcases/test_worker_location.worker.js';
+
 // This worker gets served with the wrong content type for cross-origin access
 // since it doesn't end in '.worker.js'. It gets rejected by Gears before even
 // being executed.
@@ -218,3 +221,22 @@
   startAsync();
   wp.sendMessage('cross origin ping', childId);
 }
+
+function testWorkerLocation() {
+  startAsync();
+  var repliesRemaining = 2;
+  var wp = google.gears.factory.create('beta.workerpool');
+  wp.onmessage = function(text, sender, m) {
+    var expectedWorkerOrigin = (m.body.echo == 'current')
+        ? currentOrigin : differentOrigin;
+    assertEqual(currentOrigin, m.body.masterOrigin);
+    assertEqual(expectedWorkerOrigin, m.body.workerOrigin);
+    if (--repliesRemaining == 0) {
+      completeAsync();
+    }
+  };
+  var childId1 = wp.createWorkerFromUrl(currentOrigin   + locationWorkerPath);
+  var childId2 = wp.createWorkerFromUrl(differentOrigin + locationWorkerPath);
+  wp.sendMessage('current', childId1);
+  wp.sendMessage('different', childId2);
+}
==== 
//depot/googleclient/gears/opensource/gears/test/testcases/workerpool_tests.js#4
 - 
/home/nigeltao/srcgears2/googleclient/gears/opensource/gears/test/testcases/workerpool_tests.js
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/test/testcases/workerpool_tests.js      
2009-02-24 17:59:56.000000000 +1100
+++ googleclient/gears/opensource/gears/test/testcases/workerpool_tests.js      
2009-02-24 17:53:11.000000000 +1100
@@ -200,3 +200,20 @@
                                  '};');
   wp1.sendMessage('PING1', childId);
 }
+
+function testLocation() {
+  // This one cannot run in a worker.
+  if (typeof document == 'undefined') {
+    return;
+  }
+
+  var wp = google.gears.factory.create('beta.workerpool');
+  assertEqual(window.location.hash,     wp.location.hash    );
+  assertEqual(window.location.host,     wp.location.host    );
+  assertEqual(window.location.hostname, wp.location.hostname);
+  assertEqual(window.location.href,     wp.location.href    );
+  assertEqual(window.location.port,     wp.location.port    );
+  assertEqual(window.location.pathname, wp.location.pathname);
+  assertEqual(window.location.protocol, wp.location.protocol);
+  assertEqual(window.location.search,   wp.location.search  );
+}
==== //depot/googleclient/gears/opensource/gears/workerpool/location.cc#1 - 
/home/nigeltao/srcgears2/googleclient/gears/opensource/gears/workerpool/location.cc
 ====
# action=add type=text
--- /dev/null   1970-01-01 10:00:00.000000000 +1000
+++ googleclient/gears/opensource/gears/workerpool/location.cc  2009-02-24 
17:33:49.000000000 +1100
@@ -0,0 +1,153 @@
+// 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/workerpool/location.h"
+
+DECLARE_DISPATCHER(GearsLocation);
+
+template <>
+void Dispatcher<GearsLocation>::Init() {
+  RegisterProperty("hash",     &GearsLocation::GetHash,     NULL);
+  RegisterProperty("host",     &GearsLocation::GetHost,     NULL);
+  RegisterProperty("hostname", &GearsLocation::GetHostname, NULL);
+  RegisterProperty("href",     &GearsLocation::GetHref,     NULL);
+  RegisterProperty("pathname", &GearsLocation::GetPathname, NULL);
+  RegisterProperty("port",     &GearsLocation::GetPort,     NULL);
+  RegisterProperty("protocol", &GearsLocation::GetProtocol, NULL);
+  RegisterProperty("search",   &GearsLocation::GetSearch,   NULL);
+}
+
+const std::string GearsLocation::kModuleName("GearsLocation");
+
+GearsLocation::GearsLocation()
+    : ModuleImplBaseClass(kModuleName) {
+}
+
+GearsLocation::~GearsLocation() {
+}
+
+void GearsLocation::GetComponent(
+    JsCallContext *context,
+    url_parse::Parsed::ComponentType component_type,
+    bool host_port) {
+  const std::string16 &url = module_environment_->security_origin_.full_url();
+  url_parse::Parsed parsed;
+  url_parse::ParseStandardURL(url.c_str(), url.length(), &parsed);
+
+  url_parse::Component component;
+  switch (component_type) {
+    case url_parse::Parsed::SCHEME:
+      component = parsed.scheme;
+      break;
+    case url_parse::Parsed::HOST:
+      component = parsed.host;
+      break;
+    case url_parse::Parsed::PORT:
+      component = parsed.port;
+      break;
+    case url_parse::Parsed::PATH:
+      component = parsed.path;
+      break;
+    case url_parse::Parsed::QUERY:
+      component = parsed.query;
+      break;
+    case url_parse::Parsed::REF:
+      component = parsed.ref;
+      break;
+    default:
+      assert(false);
+      return;
+  }
+  if (!component.is_valid()) {
+    std::string16 empty;
+    context->SetReturnValue(JSPARAM_STRING16, &empty);
+    return;
+  }
+  int begin = component.begin;
+  int len = component.len;
+  // The HTML5 spec (*) says that we should return "http:" instead of "http",
+  // and similarly the search and hash terms should contain "?" and "#".
+  // Furthermore, host_port shouldn't add the port part if the port is the
+  // default port (i.e. 80 for HTTP, 443 for HTTPS).
+  // (*) http://www.w3.org/TR/html5/infrastructure.html and also
+  // RFC 3986 "Uniform Resource Identifier (URI): Generic Syntax".
+  if (host_port) {
+    if (parsed.scheme.is_valid() && parsed.port.is_valid()) {
+      bool is_default_port = false;
+      std::string16 scheme(url.substr(parsed.scheme.begin, parsed.scheme.len));
+      std::string16 port(url.substr(parsed.port.begin, parsed.port.len));
+      LowerString(scheme);
+      if (scheme == STRING16(L"http") && port == STRING16(L"80")) {
+        is_default_port = true;
+      } else if (scheme == STRING16(L"https") && port == STRING16(L"443")) {
+        is_default_port = true;
+      }
+      if (!is_default_port) {
+        len = parsed.port.end() - begin;
+      }
+    }
+  } else if (component_type == url_parse::Parsed::SCHEME) {
+    len++;
+  } else if (component_type == url_parse::Parsed::QUERY ||
+      component_type == url_parse::Parsed::REF) {
+    begin--;
+    len++;
+  }
+  std::string16 result(url.substr(begin, len));
+  context->SetReturnValue(JSPARAM_STRING16, &result);
+}
+
+void GearsLocation::GetHash(JsCallContext *context) {
+  GetComponent(context, url_parse::Parsed::REF, false);
+}
+
+void GearsLocation::GetHost(JsCallContext *context) {
+  GetComponent(context, url_parse::Parsed::HOST, true);
+}
+
+void GearsLocation::GetHostname(JsCallContext *context) {
+  GetComponent(context, url_parse::Parsed::HOST, false);
+}
+
+void GearsLocation::GetHref(JsCallContext *context) {
+  context->SetReturnValue(JSPARAM_STRING16,
+      &module_environment_->security_origin_.full_url());
+}
+
+void GearsLocation::GetPathname(JsCallContext *context) {
+  GetComponent(context, url_parse::Parsed::PATH, false);
+}
+
+void GearsLocation::GetPort(JsCallContext *context) {
+  GetComponent(context, url_parse::Parsed::PORT, false);
+}
+
+void GearsLocation::GetProtocol(JsCallContext *context) {
+  GetComponent(context, url_parse::Parsed::SCHEME, false);
+}
+
+void GearsLocation::GetSearch(JsCallContext *context) {
+  GetComponent(context, url_parse::Parsed::QUERY, false);
+}
==== //depot/googleclient/gears/opensource/gears/workerpool/location.h#1 - 
/home/nigeltao/srcgears2/googleclient/gears/opensource/gears/workerpool/location.h
 ====
# action=add type=text
--- /dev/null   1970-01-01 10:00:00.000000000 +1000
+++ googleclient/gears/opensource/gears/workerpool/location.h   2009-02-24 
17:04:44.000000000 +1100
@@ -0,0 +1,81 @@
+// 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_WORKERPOOL_LOCATION_H__
+#define GEARS_WORKERPOOL_LOCATION_H__
+
+#include "gears/base/common/base_class.h"
+#include "third_party/googleurl/src/url_parse.h"
+
+class GearsLocation
+    : public ModuleImplBaseClass {
+ public:
+  static const std::string kModuleName;
+
+  GearsLocation();
+  virtual ~GearsLocation();
+
+  // IN: -
+  // OUT: string hash
+  void GetHash(JsCallContext *context);
+
+  // IN: -
+  // OUT: string host
+  void GetHost(JsCallContext *context);
+
+  // IN: -
+  // OUT: string hostname
+  void GetHostname(JsCallContext *context);
+
+  // IN: -
+  // OUT: string href
+  void GetHref(JsCallContext *context);
+
+  // IN: -
+  // OUT: string pathname
+  void GetPathname(JsCallContext *context);
+
+  // IN: -
+  // OUT: string protocol
+  void GetProtocol(JsCallContext *context);
+
+  // IN: -
+  // OUT: string port
+  void GetPort(JsCallContext *context);
+
+  // IN: -
+  // OUT: string search
+  void GetSearch(JsCallContext *context);
+
+ private:
+  void GetComponent(
+      JsCallContext *context,
+      url_parse::Parsed::ComponentType component_type,
+      bool host_port);
+
+  DISALLOW_EVIL_CONSTRUCTORS(GearsLocation);
+};
+
+#endif // GEARS_WORKERPOOL_LOCATION_H__
==== //depot/googleclient/gears/opensource/gears/workerpool/workerpool.cc#9 - 
/home/nigeltao/srcgears2/googleclient/gears/opensource/gears/workerpool/workerpool.cc
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/workerpool/workerpool.cc        
2009-02-24 17:59:57.000000000 +1100
+++ googleclient/gears/opensource/gears/workerpool/workerpool.cc        
2009-02-24 17:57:31.000000000 +1100
@@ -31,6 +31,7 @@
 #include "gears/base/common/permissions_db.h"
 #include "gears/base/common/url_utils.h"
 #include "gears/localserver/common/http_request.h"
+#include "gears/workerpool/location.h"
 #if BROWSER_FF
 #include "gears/workerpool/firefox/pool_threads_manager.h"
 #elif BROWSER_IE || BROWSER_IEMOBILE
@@ -53,6 +54,7 @@
                    &GearsWorkerPool::SetOnmessage);
   RegisterProperty("onerror", &GearsWorkerPool::GetOnerror,
                    &GearsWorkerPool::SetOnerror);
+  RegisterProperty("location", &GearsWorkerPool::GetLocation, NULL);
 #ifdef DEBUG
   RegisterMethod("forceGC", &GearsWorkerPool::ForceGC);
 #endif
@@ -140,6 +142,16 @@
 void GearsWorkerPool::GetOnerror(JsCallContext *context) {
   // TODO(nigeltao): implement, a la HttpRequest::GetOnReadyStateChange.
   context->SetException(STRING16(L"Not Implemented"));
+}
+
+void GearsWorkerPool::GetLocation(JsCallContext *context) {
+  if (!location_) {
+    CreateModule<GearsLocation>(module_environment_.get(), context, 
&location_);
+    if (!location_) {
+      return;  // CreateModule will set the error message.
+    }
+  }
+  context->SetReturnValue(JSPARAM_MODULE, location_.get());
 }
 
 void GearsWorkerPool::CreateWorker(JsCallContext *context) {
@@ -318,6 +330,9 @@
 void GearsWorkerPool::HandleEvent(JsEventType event_type) {
   assert(event_type == JSEVENT_UNLOAD);
 
+  // Break the reference cycle with the GearsLocation and the 
ModuleEnvironment.
+  location_.reset(NULL);
+
   if (owns_threads_manager_ && threads_manager_) {
     // Note: the following line can cause us to be deleted
     threads_manager_->ShutDown();
==== //depot/googleclient/gears/opensource/gears/workerpool/workerpool.h#2 - 
/home/nigeltao/srcgears2/googleclient/gears/opensource/gears/workerpool/workerpool.h
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/workerpool/workerpool.h 2009-02-24 
17:59:57.000000000 +1100
+++ googleclient/gears/opensource/gears/workerpool/workerpool.h 2009-02-24 
11:33:47.000000000 +1100
@@ -74,6 +74,10 @@
   // OUT: function
   void GetOnerror(JsCallContext *context);
 
+  // IN: -
+  // OUT: location
+  void GetLocation(JsCallContext *context);
+
 #ifdef DEBUG
   // IN: -
   // OUT: -
@@ -91,6 +95,7 @@
   PoolThreadsManager *threads_manager_;
   bool owns_threads_manager_;
   scoped_ptr<JsEventMonitor> unload_monitor_;
+  scoped_refptr<ModuleImplBaseClass> location_;
 
   DISALLOW_EVIL_CONSTRUCTORS(GearsWorkerPool);
 };

Reply via email to