Commit 76b09132 authored by Cosmin Sabou's avatar Cosmin Sabou
Browse files

Merge autoland to mozilla-central. a=merge

parents aa2a0cc3 94afea82
Loading
Loading
Loading
Loading
+37 −11
Original line number Diff line number Diff line
@@ -12,12 +12,14 @@ ChromeUtils.defineModuleGetter(this, "OS",
  "resource://gre/modules/osfile.jsm");
ChromeUtils.defineModuleGetter(this, "Services",
  "resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyGlobalGetters(this, ["URL"]);

const ATTR_CODE_MAX_LENGTH = 200;
const ATTR_CODE_KEYS_REGEX = /^source|medium|campaign|content$/;
const ATTR_CODE_VALUE_REGEX = /[a-zA-Z0-9_%\\-\\.\\(\\)]*/;
const ATTR_CODE_FIELD_SEPARATOR = "%26"; // URL-encoded &
const ATTR_CODE_KEY_VALUE_SEPARATOR = "%3D"; // URL-encoded =
const ATTR_CODE_KEYS = ["source", "medium", "campaign", "content"];

let gCachedAttrData = null;

@@ -65,6 +67,10 @@ var AttributionCode = {
   * Returns a promise that fulfills with an object containing the parsed
   * attribution data if the code could be read and is valid,
   * or an empty object otherwise.
   *
   * On windows the attribution service converts utm_* keys, removing "utm_".
   * On OSX the attributions are set directly on download and retain "utm_".  We
   * strip "utm_" while retrieving the params.
   */
  getAttrDataAsync() {
    return (async function() {
@@ -72,18 +78,38 @@ var AttributionCode = {
        return gCachedAttrData;
      }

      let code = "";
      gCachedAttrData = {};
      if (AppConstants.platform == "win") {
        try {
          let bytes = await OS.File.read(getAttributionFile().path);
          let decoder = new TextDecoder();
        code = decoder.decode(bytes);
          let code = decoder.decode(bytes);
          gCachedAttrData = parseAttributionCode(code);
        } catch (ex) {
          // The attribution file may already have been deleted,
          // or it may have never been installed at all;
          // failure to open or read it isn't an error.
        }

      gCachedAttrData = parseAttributionCode(code);
      } else if (AppConstants.platform == "macosx") {
        try {
          let appPath = Services.dirsvc.get("GreD", Ci.nsIFile).parent.parent.path;
          let attributionSvc = Cc["@mozilla.org/mac-attribution;1"]
                                  .getService(Ci.nsIMacAttributionService);
          let referrer = attributionSvc.getReferrerUrl(appPath);
          let params = new URL(referrer).searchParams;
          for (let key of ATTR_CODE_KEYS) {
            let utm_key = `utm_${key}`;
            if (params.has(utm_key)) {
              let value = params.get(utm_key);
              if (value && ATTR_CODE_VALUE_REGEX.test(value)) {
                gCachedAttrData[key] = value;
              }
            }
          }
        } catch (ex) {
          // No attributions
        }
      }
      return gCachedAttrData;
    })();
  },
+31 −0
Original line number Diff line number Diff line
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

with Files("**"):
    BUG_COMPONENT = ("Toolkit", "Telemetry")

XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell/xpcshell.ini']

EXTRA_JS_MODULES += [
    'AttributionCode.jsm',
]

if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'cocoa':
    XPIDL_SOURCES += [
        'nsIMacAttribution.idl',
    ]

    XPIDL_MODULE = 'attribution'

    EXPORTS += [
        'nsMacAttribution.h',
    ]

    SOURCES += [
        'nsMacAttribution.cpp',
    ]

    FINAL_LIBRARY = 'browsercomps'
+31 −0
Original line number Diff line number Diff line
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsISupports.idl"

[scriptable, uuid(6FC66A78-6CBC-4B3F-B7BA-379289B29276)]
interface nsIMacAttributionService : nsISupports
{
  /**
   * Used by the Attributions system to get the download referrer.
   *
   * @param aFilePath A path to the file to get the quarantine data from.
   * @returns referrerUrl
   */
  AString getReferrerUrl(in ACString aFilePath);

  /**
   * Used by the tests.
   *
   * @param aFilePath A path to the file to set the quarantine data on.
   * @param aReferrer A url to set as the referrer for the download.
   * @param aCreate   If true, creates new quarantine properties, overwriting
   *                  any existing properties.  If false, the referrer is only
   *                  set if quarantine properties already exist on the file.
   */
  void setReferrerUrl(in ACString aFilePath,
                      in ACString aReferrer,
                      in boolean aCreate);
};
+70 −0
Original line number Diff line number Diff line
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsMacAttribution.h"

#include <CoreFoundation/CoreFoundation.h>
#include <ApplicationServices/ApplicationServices.h>

#include "../../../xpcom/io/CocoaFileUtils.h"
#include "nsCocoaFeatures.h"
#include "nsString.h"

using namespace mozilla;

NS_IMPL_ISUPPORTS(nsMacAttributionService, nsIMacAttributionService)

NS_IMETHODIMP
nsMacAttributionService::GetReferrerUrl(const nsACString& aFilePath,
                                              nsAString& aReferrer)
{
  const nsCString& flat = PromiseFlatCString(aFilePath);
  CFStringRef filePath = ::CFStringCreateWithCString(kCFAllocatorDefault,
                                                     flat.get(),
                                                     kCFStringEncodingUTF8);

  CocoaFileUtils::CopyQuarantineReferrerUrl(filePath, aReferrer);

  ::CFRelease(filePath);
  return NS_OK;
}

NS_IMETHODIMP
nsMacAttributionService::SetReferrerUrl(const nsACString& aFilePath,
                                        const nsACString& aReferrerUrl,
                                        const bool aCreate)
{
  const nsCString& flat = PromiseFlatCString(aFilePath);
  CFStringRef filePath = ::CFStringCreateWithCString(kCFAllocatorDefault,
                                                     flat.get(),
                                                     kCFStringEncodingUTF8);

  if (!filePath) {
    return NS_ERROR_UNEXPECTED;
  }

  const nsCString& flatReferrer = PromiseFlatCString(aReferrerUrl);
  CFStringRef referrer = ::CFStringCreateWithCString(kCFAllocatorDefault,
                                                     flatReferrer.get(),
                                                     kCFStringEncodingUTF8);
  if (!referrer) {
    ::CFRelease(filePath);
    return NS_ERROR_UNEXPECTED;
  }
  CFURLRef referrerURL = ::CFURLCreateWithString(kCFAllocatorDefault,
                                                 referrer, nullptr);

  CocoaFileUtils::AddQuarantineMetadataToFile(filePath,
                                              NULL,
                                              referrerURL,
                                              true,
                                              aCreate);

  ::CFRelease(filePath);
  ::CFRelease(referrer);
  ::CFRelease(referrerURL);

  return NS_OK;
}
+23 −0
Original line number Diff line number Diff line
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsmacattribution_h____
#define nsmacattribution_h____

#include "nsIMacAttribution.h"

class nsMacAttributionService : public nsIMacAttributionService
{
public:
  nsMacAttributionService() {};

  NS_DECL_ISUPPORTS
  NS_DECL_NSIMACATTRIBUTIONSERVICE

protected:
  virtual ~nsMacAttributionService() {};
};

#endif // nsmacattribution_h____
Loading