xpcom/tests/windows/TestCOM.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include <windows.h>
     7 #include <unknwn.h>
     8 #include <stdio.h>
     9 #include "nsISupports.h"
    10 #include "nsIFactory.h"
    12 // unknwn.h is needed to build with WIN32_LEAN_AND_MEAN
    13 #include <unknwn.h>
    15 // {5846BA30-B856-11d1-A98A-00805F8A7AC4}
    16 #define NS_ITEST_COM_IID \
    17 { 0x5846ba30, 0xb856, 0x11d1, \
    18   { 0xa9, 0x8a, 0x0, 0x80, 0x5f, 0x8a, 0x7a, 0xc4 } }
    20 class nsITestCom: public nsISupports
    21 {
    22 public:
    23   NS_DECLARE_STATIC_IID_ACCESSOR(NS_ITEST_COM_IID)
    24   NS_IMETHOD Test() = 0;
    25 };
    27 NS_DEFINE_STATIC_IID_ACCESSOR(nsITestCom, NS_ITEST_COM_IID)
    29 /*
    30  * nsTestCom
    31  */
    33 class nsTestCom MOZ_FINAL : public nsITestCom {
    34   NS_DECL_ISUPPORTS
    36 public:
    37   nsTestCom() {
    38   }
    40   NS_IMETHOD Test() {
    41     printf("Accessed nsITestCom::Test() from COM\n");
    42     return NS_OK;
    43   }
    45 private:
    46   ~nsTestCom() {
    47     printf("nsTestCom instance successfully deleted\n");
    48   }
    49 };
    51 NS_IMPL_QUERY_INTERFACE(nsTestCom, nsITestCom)
    53 MozExternalRefCountType nsTestCom::AddRef()
    54 {
    55   nsrefcnt res = ++mRefCnt;
    56   NS_LOG_ADDREF(this, mRefCnt, "nsTestCom", sizeof(*this));
    57   printf("nsTestCom: Adding ref = %d\n", res);
    58   return res;
    59 }
    61 MozExternalRefCountType nsTestCom::Release()
    62 {
    63   nsrefcnt res = --mRefCnt;
    64   NS_LOG_RELEASE(this, mRefCnt, "nsTestCom");
    65   printf("nsTestCom: Releasing = %d\n", res);
    66   if (res == 0) {
    67     delete this;
    68   }
    69   return res;
    70 }
    72 class nsTestComFactory MOZ_FINAL : public nsIFactory {
    73   NS_DECL_ISUPPORTS
    74 public:
    75   nsTestComFactory() {
    76   }
    78   NS_IMETHOD CreateInstance(nsISupports *aOuter,
    79                             const nsIID &aIID,
    80                             void **aResult);
    82   NS_IMETHOD LockFactory(bool aLock) {
    83     printf("nsTestComFactory: ");
    84     printf("%s", (aLock ? "Locking server" : "Unlocking server"));
    85     printf("\n");
    86     return NS_OK;
    87   }
    88 };
    90 NS_IMPL_ISUPPORTS(nsTestComFactory, nsIFactory)
    92 nsresult nsTestComFactory::CreateInstance(nsISupports *aOuter,
    93 					  const nsIID &aIID,
    94 					  void **aResult)
    95 {
    96   if (aOuter != nullptr) {
    97     return NS_ERROR_NO_AGGREGATION;
    98   }
   100   nsTestCom *t = new nsTestCom();
   102   if (t == nullptr) {
   103     return NS_ERROR_OUT_OF_MEMORY;
   104   }
   106   NS_ADDREF(t);
   107   nsresult res = t->QueryInterface(aIID, aResult);
   108   NS_RELEASE(t);
   110   if (NS_SUCCEEDED(res)) {
   111     printf("nsTestComFactory: successfully created nsTestCom instance\n");
   112   }
   114   return res;
   115 }
   117 /*
   118  * main
   119  */
   121 int main(int argc, char *argv[])
   122 {
   123   nsTestComFactory *inst = new nsTestComFactory();
   124   IClassFactory *iFactory;
   125   inst->QueryInterface(NS_GET_IID(nsIFactory), (void **) &iFactory);
   127   IUnknown *iUnknown;  
   128   nsITestCom *iTestCom;
   130   iFactory->LockServer(TRUE);
   131   iFactory->CreateInstance(nullptr, IID_IUnknown, (void **) &iUnknown);
   132   iFactory->LockServer(FALSE);
   134   GUID testGUID = NS_ITEST_COM_IID;
   135   HRESULT hres;
   136   hres= iUnknown->QueryInterface(testGUID, 
   137 				 (void **) &iTestCom);
   139   iTestCom->Test();
   141   iUnknown->Release();
   142   iTestCom->Release();
   143   iFactory->Release();
   145   return 0;
   146 }

mercurial