uriloader/exthandler/nsLocalHandlerApp.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/uriloader/exthandler/nsLocalHandlerApp.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,185 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim:expandtab:shiftwidth=2:tabstop=2:cin:
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nsLocalHandlerApp.h"
    1.11 +#include "nsIURI.h"
    1.12 +#include "nsIProcess.h"
    1.13 +
    1.14 +// XXX why does nsMIMEInfoImpl have a threadsafe nsISupports?  do we need one 
    1.15 +// here too?
    1.16 +NS_IMPL_ISUPPORTS(nsLocalHandlerApp, nsILocalHandlerApp, nsIHandlerApp)
    1.17 +
    1.18 +////////////////////////////////////////////////////////////////////////////////
    1.19 +//// nsIHandlerApp
    1.20 +
    1.21 +NS_IMETHODIMP nsLocalHandlerApp::GetName(nsAString& aName)
    1.22 +{
    1.23 +  if (mName.IsEmpty() && mExecutable) {
    1.24 +    // Don't want to cache this, just in case someone resets the app
    1.25 +    // without changing the description....
    1.26 +    mExecutable->GetLeafName(aName);
    1.27 +  } else {
    1.28 +    aName.Assign(mName);
    1.29 +  }
    1.30 +  
    1.31 +  return NS_OK;
    1.32 +}
    1.33 +
    1.34 +NS_IMETHODIMP nsLocalHandlerApp::SetName(const nsAString & aName)
    1.35 +{
    1.36 +  mName.Assign(aName);
    1.37 +
    1.38 +  return NS_OK;
    1.39 +}
    1.40 +
    1.41 +NS_IMETHODIMP
    1.42 +nsLocalHandlerApp::SetDetailedDescription(const nsAString & aDescription)
    1.43 +{
    1.44 +  mDetailedDescription.Assign(aDescription);
    1.45 +
    1.46 +  return NS_OK;
    1.47 +}
    1.48 +
    1.49 +NS_IMETHODIMP
    1.50 +nsLocalHandlerApp::GetDetailedDescription(nsAString& aDescription)
    1.51 +{
    1.52 +  aDescription.Assign(mDetailedDescription);
    1.53 +  
    1.54 +  return NS_OK;
    1.55 +}
    1.56 +
    1.57 +NS_IMETHODIMP
    1.58 +nsLocalHandlerApp::Equals(nsIHandlerApp *aHandlerApp, bool *_retval)
    1.59 +{
    1.60 +  NS_ENSURE_ARG_POINTER(aHandlerApp);
    1.61 +
    1.62 +  *_retval = false;
    1.63 +
    1.64 +  // If the handler app isn't a local handler app, then it's not the same app.
    1.65 +  nsCOMPtr <nsILocalHandlerApp> localHandlerApp = do_QueryInterface(aHandlerApp);
    1.66 +  if (!localHandlerApp)
    1.67 +    return NS_OK;
    1.68 +
    1.69 +  // If either handler app doesn't have an executable, then they aren't
    1.70 +  // the same app.
    1.71 +  nsCOMPtr<nsIFile> executable;
    1.72 +  nsresult rv = localHandlerApp->GetExecutable(getter_AddRefs(executable));
    1.73 +  if (NS_FAILED(rv))
    1.74 +    return rv;
    1.75 +
    1.76 +  // Equality for two empty nsIHandlerApp
    1.77 +  if (!executable && !mExecutable) {
    1.78 +    *_retval = true;
    1.79 +    return NS_OK;
    1.80 +  }
    1.81 +
    1.82 +  // At least one is set so they are not equal
    1.83 +  if (!mExecutable || !executable)
    1.84 +    return NS_OK;
    1.85 +
    1.86 +  // Check the command line parameter list lengths
    1.87 +  uint32_t len;
    1.88 +  localHandlerApp->GetParameterCount(&len);
    1.89 +  if (mParameters.Length() != len)
    1.90 +    return NS_OK;
    1.91 +
    1.92 +  // Check the command line params lists
    1.93 +  for (uint32_t idx = 0; idx < mParameters.Length(); idx++) {
    1.94 +    nsAutoString param;
    1.95 +    if (NS_FAILED(localHandlerApp->GetParameter(idx, param)) ||
    1.96 +        !param.Equals(mParameters[idx]))
    1.97 +      return NS_OK;
    1.98 +  }
    1.99 +
   1.100 +  return executable->Equals(mExecutable, _retval);
   1.101 +}
   1.102 +
   1.103 +NS_IMETHODIMP
   1.104 +nsLocalHandlerApp::LaunchWithURI(nsIURI *aURI,
   1.105 +                                 nsIInterfaceRequestor *aWindowContext)
   1.106 +{
   1.107 +  // pass the entire URI to the handler.
   1.108 +  nsAutoCString spec;
   1.109 +  aURI->GetAsciiSpec(spec);
   1.110 +  return LaunchWithIProcess(spec);
   1.111 +}
   1.112 +
   1.113 +nsresult
   1.114 +nsLocalHandlerApp::LaunchWithIProcess(const nsCString& aArg)
   1.115 +{
   1.116 +  nsresult rv;
   1.117 +  nsCOMPtr<nsIProcess> process = do_CreateInstance(NS_PROCESS_CONTRACTID, &rv);
   1.118 +  if (NS_FAILED(rv))
   1.119 +    return rv;
   1.120 +
   1.121 +  if (NS_FAILED(rv = process->Init(mExecutable)))
   1.122 +    return rv;
   1.123 +
   1.124 +  const char *string = aArg.get();
   1.125 +
   1.126 +  return process->Run(false, &string, 1);
   1.127 +}
   1.128 +
   1.129 +////////////////////////////////////////////////////////////////////////////////
   1.130 +//// nsILocalHandlerApp
   1.131 +
   1.132 +/* attribute nsIFile executable; */
   1.133 +NS_IMETHODIMP
   1.134 +nsLocalHandlerApp::GetExecutable(nsIFile **aExecutable)
   1.135 +{
   1.136 +  NS_IF_ADDREF(*aExecutable = mExecutable);
   1.137 +  return NS_OK;
   1.138 +}
   1.139 +
   1.140 +NS_IMETHODIMP
   1.141 +nsLocalHandlerApp::SetExecutable(nsIFile *aExecutable)
   1.142 +{
   1.143 +  mExecutable = aExecutable;
   1.144 +  return NS_OK;
   1.145 +}
   1.146 +
   1.147 +/* readonly attribute unsigned long parameterCount; */
   1.148 +NS_IMETHODIMP
   1.149 +nsLocalHandlerApp::GetParameterCount(uint32_t *aParameterCount)
   1.150 +{
   1.151 +  *aParameterCount = mParameters.Length();
   1.152 +  return NS_OK;
   1.153 +}
   1.154 +
   1.155 +/* void clearParameters (); */
   1.156 +NS_IMETHODIMP
   1.157 +nsLocalHandlerApp::ClearParameters()
   1.158 +{
   1.159 +  mParameters.Clear();
   1.160 +  return NS_OK;
   1.161 +}
   1.162 +
   1.163 +/* void appendParameter (in AString param); */
   1.164 +NS_IMETHODIMP
   1.165 +nsLocalHandlerApp::AppendParameter(const nsAString & aParam)
   1.166 +{
   1.167 +  mParameters.AppendElement(aParam);
   1.168 +  return NS_OK;
   1.169 +}
   1.170 +
   1.171 +/* AString getParameter (in unsigned long parameterIndex); */
   1.172 +NS_IMETHODIMP
   1.173 +nsLocalHandlerApp::GetParameter(uint32_t parameterIndex, nsAString & _retval)
   1.174 +{
   1.175 +  if (mParameters.Length() <= parameterIndex)
   1.176 +    return NS_ERROR_INVALID_ARG;
   1.177 +
   1.178 +  _retval.Assign(mParameters[parameterIndex]);
   1.179 +  return NS_OK;
   1.180 +}
   1.181 +
   1.182 +/* boolean parameterExists (in AString param); */
   1.183 +NS_IMETHODIMP
   1.184 +nsLocalHandlerApp::ParameterExists(const nsAString & aParam, bool *_retval)
   1.185 +{
   1.186 +  *_retval = mParameters.Contains(aParam);
   1.187 +  return NS_OK;
   1.188 +}

mercurial