Make JSGetObject<C>() return UnownedPtr<C>.

This a convenient place to assert that the callback that is about
to be invoked on the object doesn't destroy the object at any point
during its execution.

Change-Id: Iacb9d4e01603cc6bf316b00fdd062955c903ca5c
Reviewed-on: https://pdfium-review.googlesource.com/37970
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fxjs/cjs_app.cpp b/fxjs/cjs_app.cpp
index fd98670..485184b 100644
--- a/fxjs/cjs_app.cpp
+++ b/fxjs/cjs_app.cpp
@@ -103,7 +103,7 @@
 
 CJS_Return CJS_App::get_active_docs(CJS_Runtime* pRuntime) {
   v8::Local<v8::Object> pObj = pRuntime->GetThisObj();
-  CJS_Document* pJSDocument = JSGetObject<CJS_Document>(pObj);
+  auto pJSDocument = JSGetObject<CJS_Document>(pObj);
   v8::Local<v8::Array> aDocs = pRuntime->NewArray();
   pRuntime->PutArrayElement(
       aDocs, 0,
@@ -394,7 +394,7 @@
     return;
 
   v8::Local<v8::Object> pObj = pRuntime->ToObject(param);
-  CJS_TimerObj* pTimer = JSGetObject<CJS_TimerObj>(pObj);
+  auto pTimer = JSGetObject<CJS_TimerObj>(pObj);
   if (!pTimer)
     return;
 
diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp
index f5b0af4..51e189e 100644
--- a/fxjs/cjs_document.cpp
+++ b/fxjs/cjs_document.cpp
@@ -354,7 +354,7 @@
   if (nLength == 9) {
     if (params[8]->IsObject()) {
       v8::Local<v8::Object> pObj = pRuntime->ToObject(params[8]);
-      CJS_PrintParamsObj* pPrintObj = JSGetObject<CJS_PrintParamsObj>(pObj);
+      auto pPrintObj = JSGetObject<CJS_PrintParamsObj>(pObj);
       if (pPrintObj) {
         bUI = pPrintObj->GetUI();
         nStart = pPrintObj->GetStart();
diff --git a/fxjs/cjs_global.cpp b/fxjs/cjs_global.cpp
index c9afdcb..efeef56 100644
--- a/fxjs/cjs_global.cpp
+++ b/fxjs/cjs_global.cpp
@@ -32,7 +32,7 @@
 void JSSpecialPropQuery(const char*,
                         v8::Local<v8::String> property,
                         const v8::PropertyCallbackInfo<v8::Integer>& info) {
-  Alt* pObj = JSGetObject<Alt>(info.Holder());
+  auto pObj = JSGetObject<Alt>(info.Holder());
   if (!pObj)
     return;
 
@@ -50,7 +50,7 @@
 void JSSpecialPropGet(const char* class_name,
                       v8::Local<v8::String> property,
                       const v8::PropertyCallbackInfo<v8::Value>& info) {
-  Alt* pObj = JSGetObject<Alt>(info.Holder());
+  auto pObj = JSGetObject<Alt>(info.Holder());
   if (!pObj)
     return;
 
@@ -75,7 +75,7 @@
                       v8::Local<v8::String> property,
                       v8::Local<v8::Value> value,
                       const v8::PropertyCallbackInfo<v8::Value>& info) {
-  Alt* pObj = JSGetObject<Alt>(info.Holder());
+  auto pObj = JSGetObject<Alt>(info.Holder());
   if (!pObj)
     return;
 
@@ -96,7 +96,7 @@
 void JSSpecialPropDel(const char* class_name,
                       v8::Local<v8::String> property,
                       const v8::PropertyCallbackInfo<v8::Boolean>& info) {
-  Alt* pObj = JSGetObject<Alt>(info.Holder());
+  auto pObj = JSGetObject<Alt>(info.Holder());
   if (!pObj)
     return;
 
diff --git a/fxjs/cjs_runtime.cpp b/fxjs/cjs_runtime.cpp
index 634c48b..9329a48 100644
--- a/fxjs/cjs_runtime.cpp
+++ b/fxjs/cjs_runtime.cpp
@@ -162,7 +162,7 @@
   if (pThis.IsEmpty())
     return;
 
-  CJS_Document* pJSDocument = JSGetObject<CJS_Document>(pThis);
+  auto pJSDocument = JSGetObject<CJS_Document>(pThis);
   if (!pJSDocument)
     return;
 
diff --git a/fxjs/js_define.h b/fxjs/js_define.h
index 629cf1a..ae1557e 100644
--- a/fxjs/js_define.h
+++ b/fxjs/js_define.h
@@ -10,6 +10,7 @@
 #include <utility>
 #include <vector>
 
+#include "core/fxcrt/unowned_ptr.h"
 #include "fxjs/cfxjs_engine.h"
 #include "fxjs/cjs_object.h"
 #include "fxjs/cjs_return.h"
@@ -58,7 +59,7 @@
 void JSDestructor(v8::Local<v8::Object> obj);
 
 template <class C>
-C* JSGetObject(v8::Local<v8::Object> obj) {
+UnownedPtr<C> JSGetObject(v8::Local<v8::Object> obj) {
   if (CFXJS_Engine::GetObjDefnID(obj) != C::GetObjDefnID())
     return nullptr;
 
@@ -66,7 +67,7 @@
   if (!pJSObj)
     return nullptr;
 
-  return static_cast<C*>(pJSObj);
+  return UnownedPtr<C>(static_cast<C*>(pJSObj));
 }
 
 template <class C, CJS_Return (C::*M)(CJS_Runtime*)>
@@ -74,7 +75,7 @@
                   const char* class_name_string,
                   v8::Local<v8::String> property,
                   const v8::PropertyCallbackInfo<v8::Value>& info) {
-  C* pObj = JSGetObject<C>(info.Holder());
+  auto pObj = JSGetObject<C>(info.Holder());
   if (!pObj)
     return;
 
@@ -82,7 +83,7 @@
   if (!pRuntime)
     return;
 
-  CJS_Return result = (pObj->*M)(pRuntime);
+  CJS_Return result = (pObj.Get()->*M)(pRuntime);
   if (result.HasError()) {
     pRuntime->Error(JSFormatErrorString(class_name_string, prop_name_string,
                                         result.Error()));
@@ -99,7 +100,7 @@
                   v8::Local<v8::String> property,
                   v8::Local<v8::Value> value,
                   const v8::PropertyCallbackInfo<void>& info) {
-  C* pObj = JSGetObject<C>(info.Holder());
+  auto pObj = JSGetObject<C>(info.Holder());
   if (!pObj)
     return;
 
@@ -107,7 +108,7 @@
   if (!pRuntime)
     return;
 
-  CJS_Return result = (pObj->*M)(pRuntime, value);
+  CJS_Return result = (pObj.Get()->*M)(pRuntime, value);
   if (result.HasError()) {
     pRuntime->Error(JSFormatErrorString(class_name_string, prop_name_string,
                                         result.Error()));
@@ -120,7 +121,7 @@
 void JSMethod(const char* method_name_string,
               const char* class_name_string,
               const v8::FunctionCallbackInfo<v8::Value>& info) {
-  C* pObj = JSGetObject<C>(info.Holder());
+  auto pObj = JSGetObject<C>(info.Holder());
   if (!pObj)
     return;
 
@@ -132,7 +133,7 @@
   for (unsigned int i = 0; i < (unsigned int)info.Length(); i++)
     parameters.push_back(info[i]);
 
-  CJS_Return result = (pObj->*M)(pRuntime, parameters);
+  CJS_Return result = (pObj.Get()->*M)(pRuntime, parameters);
   if (result.HasError()) {
     pRuntime->Error(JSFormatErrorString(class_name_string, method_name_string,
                                         result.Error()));