Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim:expandtab:shiftwidth=2:tabstop=2:cin: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsLocalHandlerApp.h" |
michael@0 | 8 | #include "nsIURI.h" |
michael@0 | 9 | #include "nsIProcess.h" |
michael@0 | 10 | |
michael@0 | 11 | // XXX why does nsMIMEInfoImpl have a threadsafe nsISupports? do we need one |
michael@0 | 12 | // here too? |
michael@0 | 13 | NS_IMPL_ISUPPORTS(nsLocalHandlerApp, nsILocalHandlerApp, nsIHandlerApp) |
michael@0 | 14 | |
michael@0 | 15 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 16 | //// nsIHandlerApp |
michael@0 | 17 | |
michael@0 | 18 | NS_IMETHODIMP nsLocalHandlerApp::GetName(nsAString& aName) |
michael@0 | 19 | { |
michael@0 | 20 | if (mName.IsEmpty() && mExecutable) { |
michael@0 | 21 | // Don't want to cache this, just in case someone resets the app |
michael@0 | 22 | // without changing the description.... |
michael@0 | 23 | mExecutable->GetLeafName(aName); |
michael@0 | 24 | } else { |
michael@0 | 25 | aName.Assign(mName); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | return NS_OK; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | NS_IMETHODIMP nsLocalHandlerApp::SetName(const nsAString & aName) |
michael@0 | 32 | { |
michael@0 | 33 | mName.Assign(aName); |
michael@0 | 34 | |
michael@0 | 35 | return NS_OK; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | NS_IMETHODIMP |
michael@0 | 39 | nsLocalHandlerApp::SetDetailedDescription(const nsAString & aDescription) |
michael@0 | 40 | { |
michael@0 | 41 | mDetailedDescription.Assign(aDescription); |
michael@0 | 42 | |
michael@0 | 43 | return NS_OK; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | NS_IMETHODIMP |
michael@0 | 47 | nsLocalHandlerApp::GetDetailedDescription(nsAString& aDescription) |
michael@0 | 48 | { |
michael@0 | 49 | aDescription.Assign(mDetailedDescription); |
michael@0 | 50 | |
michael@0 | 51 | return NS_OK; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | NS_IMETHODIMP |
michael@0 | 55 | nsLocalHandlerApp::Equals(nsIHandlerApp *aHandlerApp, bool *_retval) |
michael@0 | 56 | { |
michael@0 | 57 | NS_ENSURE_ARG_POINTER(aHandlerApp); |
michael@0 | 58 | |
michael@0 | 59 | *_retval = false; |
michael@0 | 60 | |
michael@0 | 61 | // If the handler app isn't a local handler app, then it's not the same app. |
michael@0 | 62 | nsCOMPtr <nsILocalHandlerApp> localHandlerApp = do_QueryInterface(aHandlerApp); |
michael@0 | 63 | if (!localHandlerApp) |
michael@0 | 64 | return NS_OK; |
michael@0 | 65 | |
michael@0 | 66 | // If either handler app doesn't have an executable, then they aren't |
michael@0 | 67 | // the same app. |
michael@0 | 68 | nsCOMPtr<nsIFile> executable; |
michael@0 | 69 | nsresult rv = localHandlerApp->GetExecutable(getter_AddRefs(executable)); |
michael@0 | 70 | if (NS_FAILED(rv)) |
michael@0 | 71 | return rv; |
michael@0 | 72 | |
michael@0 | 73 | // Equality for two empty nsIHandlerApp |
michael@0 | 74 | if (!executable && !mExecutable) { |
michael@0 | 75 | *_retval = true; |
michael@0 | 76 | return NS_OK; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | // At least one is set so they are not equal |
michael@0 | 80 | if (!mExecutable || !executable) |
michael@0 | 81 | return NS_OK; |
michael@0 | 82 | |
michael@0 | 83 | // Check the command line parameter list lengths |
michael@0 | 84 | uint32_t len; |
michael@0 | 85 | localHandlerApp->GetParameterCount(&len); |
michael@0 | 86 | if (mParameters.Length() != len) |
michael@0 | 87 | return NS_OK; |
michael@0 | 88 | |
michael@0 | 89 | // Check the command line params lists |
michael@0 | 90 | for (uint32_t idx = 0; idx < mParameters.Length(); idx++) { |
michael@0 | 91 | nsAutoString param; |
michael@0 | 92 | if (NS_FAILED(localHandlerApp->GetParameter(idx, param)) || |
michael@0 | 93 | !param.Equals(mParameters[idx])) |
michael@0 | 94 | return NS_OK; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | return executable->Equals(mExecutable, _retval); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | NS_IMETHODIMP |
michael@0 | 101 | nsLocalHandlerApp::LaunchWithURI(nsIURI *aURI, |
michael@0 | 102 | nsIInterfaceRequestor *aWindowContext) |
michael@0 | 103 | { |
michael@0 | 104 | // pass the entire URI to the handler. |
michael@0 | 105 | nsAutoCString spec; |
michael@0 | 106 | aURI->GetAsciiSpec(spec); |
michael@0 | 107 | return LaunchWithIProcess(spec); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | nsresult |
michael@0 | 111 | nsLocalHandlerApp::LaunchWithIProcess(const nsCString& aArg) |
michael@0 | 112 | { |
michael@0 | 113 | nsresult rv; |
michael@0 | 114 | nsCOMPtr<nsIProcess> process = do_CreateInstance(NS_PROCESS_CONTRACTID, &rv); |
michael@0 | 115 | if (NS_FAILED(rv)) |
michael@0 | 116 | return rv; |
michael@0 | 117 | |
michael@0 | 118 | if (NS_FAILED(rv = process->Init(mExecutable))) |
michael@0 | 119 | return rv; |
michael@0 | 120 | |
michael@0 | 121 | const char *string = aArg.get(); |
michael@0 | 122 | |
michael@0 | 123 | return process->Run(false, &string, 1); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 127 | //// nsILocalHandlerApp |
michael@0 | 128 | |
michael@0 | 129 | /* attribute nsIFile executable; */ |
michael@0 | 130 | NS_IMETHODIMP |
michael@0 | 131 | nsLocalHandlerApp::GetExecutable(nsIFile **aExecutable) |
michael@0 | 132 | { |
michael@0 | 133 | NS_IF_ADDREF(*aExecutable = mExecutable); |
michael@0 | 134 | return NS_OK; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | NS_IMETHODIMP |
michael@0 | 138 | nsLocalHandlerApp::SetExecutable(nsIFile *aExecutable) |
michael@0 | 139 | { |
michael@0 | 140 | mExecutable = aExecutable; |
michael@0 | 141 | return NS_OK; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | /* readonly attribute unsigned long parameterCount; */ |
michael@0 | 145 | NS_IMETHODIMP |
michael@0 | 146 | nsLocalHandlerApp::GetParameterCount(uint32_t *aParameterCount) |
michael@0 | 147 | { |
michael@0 | 148 | *aParameterCount = mParameters.Length(); |
michael@0 | 149 | return NS_OK; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | /* void clearParameters (); */ |
michael@0 | 153 | NS_IMETHODIMP |
michael@0 | 154 | nsLocalHandlerApp::ClearParameters() |
michael@0 | 155 | { |
michael@0 | 156 | mParameters.Clear(); |
michael@0 | 157 | return NS_OK; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | /* void appendParameter (in AString param); */ |
michael@0 | 161 | NS_IMETHODIMP |
michael@0 | 162 | nsLocalHandlerApp::AppendParameter(const nsAString & aParam) |
michael@0 | 163 | { |
michael@0 | 164 | mParameters.AppendElement(aParam); |
michael@0 | 165 | return NS_OK; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | /* AString getParameter (in unsigned long parameterIndex); */ |
michael@0 | 169 | NS_IMETHODIMP |
michael@0 | 170 | nsLocalHandlerApp::GetParameter(uint32_t parameterIndex, nsAString & _retval) |
michael@0 | 171 | { |
michael@0 | 172 | if (mParameters.Length() <= parameterIndex) |
michael@0 | 173 | return NS_ERROR_INVALID_ARG; |
michael@0 | 174 | |
michael@0 | 175 | _retval.Assign(mParameters[parameterIndex]); |
michael@0 | 176 | return NS_OK; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | /* boolean parameterExists (in AString param); */ |
michael@0 | 180 | NS_IMETHODIMP |
michael@0 | 181 | nsLocalHandlerApp::ParameterExists(const nsAString & aParam, bool *_retval) |
michael@0 | 182 | { |
michael@0 | 183 | *_retval = mParameters.Contains(aParam); |
michael@0 | 184 | return NS_OK; |
michael@0 | 185 | } |