1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "mozilla/AppData.h"
8 #include "nsXULAppAPI.h"
9 #include "nsINIParser.h"
10 #include "nsIFile.h"
11 #include "nsCRTGlue.h"
12 #include "nsAutoPtr.h"
13 
14 namespace mozilla {
15 
16 void
SetAllocatedString(const char * & aStr,const char * aNewValue)17 SetAllocatedString(const char*& aStr, const char* aNewValue)
18 {
19   NS_Free(const_cast<char*>(aStr));
20   if (aNewValue) {
21     aStr = NS_strdup(aNewValue);
22   } else {
23     aStr = nullptr;
24   }
25 }
26 
27 void
SetAllocatedString(const char * & aStr,const nsACString & aNewValue)28 SetAllocatedString(const char*& aStr, const nsACString& aNewValue)
29 {
30   NS_Free(const_cast<char*>(aStr));
31   if (aNewValue.IsEmpty()) {
32     aStr = nullptr;
33   } else {
34     aStr = ToNewCString(aNewValue);
35   }
36 }
37 
ScopedAppData(const nsXREAppData * aAppData)38 ScopedAppData::ScopedAppData(const nsXREAppData* aAppData)
39 {
40   Zero();
41 
42   this->size = aAppData->size;
43 
44   SetAllocatedString(this->vendor, aAppData->vendor);
45   SetAllocatedString(this->name, aAppData->name);
46   SetAllocatedString(this->remotingName, aAppData->remotingName);
47   SetAllocatedString(this->version, aAppData->version);
48   SetAllocatedString(this->buildID, aAppData->buildID);
49   SetAllocatedString(this->ID, aAppData->ID);
50   SetAllocatedString(this->copyright, aAppData->copyright);
51   SetAllocatedString(this->profile, aAppData->profile);
52   SetStrongPtr(this->directory, aAppData->directory);
53   this->flags = aAppData->flags;
54 
55   if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) {
56     SetStrongPtr(this->xreDirectory, aAppData->xreDirectory);
57     SetAllocatedString(this->minVersion, aAppData->minVersion);
58     SetAllocatedString(this->maxVersion, aAppData->maxVersion);
59   }
60 
61   if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) {
62     SetAllocatedString(this->crashReporterURL, aAppData->crashReporterURL);
63   }
64 
65   if (aAppData->size > offsetof(nsXREAppData, UAName)) {
66     SetAllocatedString(this->UAName, aAppData->UAName);
67   }
68 
69 #if defined(XP_WIN) && defined(MOZ_SANDBOX)
70   sandboxBrokerServices = aAppData->sandboxBrokerServices;
71 #endif
72 }
73 
~ScopedAppData()74 ScopedAppData::~ScopedAppData()
75 {
76   SetAllocatedString(this->vendor, nullptr);
77   SetAllocatedString(this->name, nullptr);
78   SetAllocatedString(this->remotingName, nullptr);
79   SetAllocatedString(this->version, nullptr);
80   SetAllocatedString(this->buildID, nullptr);
81   SetAllocatedString(this->ID, nullptr);
82   SetAllocatedString(this->copyright, nullptr);
83   SetAllocatedString(this->profile, nullptr);
84 
85   NS_IF_RELEASE(this->directory);
86 
87   SetStrongPtr(this->xreDirectory, (nsIFile*)nullptr);
88   SetAllocatedString(this->minVersion, nullptr);
89   SetAllocatedString(this->maxVersion, nullptr);
90 
91   SetAllocatedString(this->crashReporterURL, nullptr);
92   SetAllocatedString(this->UAName, nullptr);
93 }
94 
95 } // namespace mozilla
96