Bug 756308. Implement MacWebAppUtils to allow callers to locate and manipulate native webapps on Mac. r=josh

This commit is contained in:
Dan Walkowski 2012-06-20 17:19:13 -07:00
parent 9507cb7bae
commit ff829facbd
9 changed files with 138 additions and 0 deletions

View File

@ -143,6 +143,7 @@ endif
ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa)
XPIDLSRCS += nsIMacDockSupport.idl \
nsIStandaloneNativeMenu.idl \
nsIMacWebAppUtils.idl \
$(NULL)
endif

View File

@ -62,6 +62,7 @@ CMMSRCS = \
TextInputHandler.mm \
nsMacDockSupport.mm \
nsStandaloneNativeMenu.mm \
nsMacWebAppUtils.mm \
GfxInfo.mm \
WidgetTraceEvent.mm \
$(NULL)

View File

@ -0,0 +1,21 @@
/* 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 _MAC_WEB_APP_UTILS_H_
#define _MAC_WEB_APP_UTILS_H_
#include "nsIMacWebAppUtils.h"
#define NS_MACWEBAPPUTILS_CONTRACTID "@mozilla.org/widget/mac-web-app-utils;1"
#define NS_MACWEBAPPUTILS_COMPONENT_CLASSNAME "Mac Web Application Utils"
class nsMacWebAppUtils : public nsIMacWebAppUtils {
public:
nsMacWebAppUtils() {}
virtual ~nsMacWebAppUtils() {}
NS_DECL_ISUPPORTS
NS_DECL_NSIMACWEBAPPUTILS
};
#endif //_MAC_WEB_APP_UTILS_H_

View File

@ -0,0 +1,39 @@
/* 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/. */
#import <Cocoa/Cocoa.h>
#include "nsMacWebAppUtils.h"
#include "nsObjCExceptions.h"
#include "nsCOMPtr.h"
#include "nsCocoaUtils.h"
#include "nsString.h"
// Find the path to the app with the given bundleIdentifier, if any.
// Note that the OS will return the path to the newest binary, if there is more than one.
// The determination of 'newest' is complex and beyond the scope of this comment.
NS_IMPL_ISUPPORTS1(nsMacWebAppUtils, nsIMacWebAppUtils)
NS_IMETHODIMP nsMacWebAppUtils::PathForAppWithIdentifier(const nsAString& bundleIdentifier, nsAString& outPath) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
outPath.Truncate();
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
//note that the result of this expression might be nil, meaning no matching app was found.
NSString* temp = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:
[NSString stringWithCharacters:((nsString)bundleIdentifier).get() length:((nsString)bundleIdentifier).Length()]];
if (temp) {
// Copy out the resultant absolute path into outPath if non-nil.
nsCocoaUtils::GetStringForNSString(temp, outPath);
}
[ap release];
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}

View File

@ -62,6 +62,9 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsNativeThemeCocoa)
#include "nsMacDockSupport.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(nsMacDockSupport)
#include "nsMacWebAppUtils.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(nsMacWebAppUtils)
#include "nsStandaloneNativeMenu.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(nsStandaloneNativeMenu)
@ -95,6 +98,7 @@ NS_DEFINE_NAMED_CID(NS_PRINTDIALOGSERVICE_CID);
NS_DEFINE_NAMED_CID(NS_IDLE_SERVICE_CID);
NS_DEFINE_NAMED_CID(NS_NATIVEMENUSERVICE_CID);
NS_DEFINE_NAMED_CID(NS_MACDOCKSUPPORT_CID);
NS_DEFINE_NAMED_CID(NS_MACWEBAPPUTILS_CID);
NS_DEFINE_NAMED_CID(NS_STANDALONENATIVEMENU_CID);
NS_DEFINE_NAMED_CID(NS_GFXINFO_CID);
@ -121,6 +125,7 @@ static const mozilla::Module::CIDEntry kWidgetCIDs[] = {
{ &kNS_IDLE_SERVICE_CID, false, NULL, nsIdleServiceXConstructor },
{ &kNS_NATIVEMENUSERVICE_CID, false, NULL, nsNativeMenuServiceXConstructor },
{ &kNS_MACDOCKSUPPORT_CID, false, NULL, nsMacDockSupportConstructor },
{ &kNS_MACWEBAPPUTILS_CID, false, NULL, nsMacWebAppUtilsConstructor },
{ &kNS_STANDALONENATIVEMENU_CID, false, NULL, nsStandaloneNativeMenuConstructor },
{ &kNS_GFXINFO_CID, false, NULL, mozilla::widget::GfxInfoConstructor },
{ NULL }
@ -148,6 +153,7 @@ static const mozilla::Module::ContractIDEntry kWidgetContracts[] = {
{ "@mozilla.org/widget/idleservice;1", &kNS_IDLE_SERVICE_CID },
{ "@mozilla.org/widget/nativemenuservice;1", &kNS_NATIVEMENUSERVICE_CID },
{ "@mozilla.org/widget/macdocksupport;1", &kNS_MACDOCKSUPPORT_CID },
{ "@mozilla.org/widget/mac-web-app-utils;1", &kNS_MACWEBAPPUTILS_CID },
{ "@mozilla.org/widget/standalonenativemenu;1", &kNS_STANDALONENATIVEMENU_CID },
{ "@mozilla.org/gfx/info;1", &kNS_GFXINFO_CID },
{ NULL }

View File

@ -0,0 +1,20 @@
/* 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"
interface nsIMacWebAppUtils;
/**
* Allow MozApps API to locate and manipulate natively installed apps
*/
[scriptable, uuid(e9096367-ddd9-45e4-b762-49c0c18b7119)]
interface nsIMacWebAppUtils : nsISupports {
/**
* Find the path for an app with the given signature.
*/
AString pathForAppWithIdentifier(in AString bundleIdentifier);
};

View File

@ -129,6 +129,10 @@
#define NS_WIN_JUMPLISTSHORTCUT_CID \
{ 0xb16656b2, 0x5187, 0x498f, { 0xab, 0xf4, 0x56, 0x34, 0x61, 0x26, 0xbf, 0xdb } }
// {e9096367-ddd9-45e4-b762-49c0c18b7119}
#define NS_MACWEBAPPUTILS_CID \
{ 0xe9096367, 0xddd9, 0x45e4, { 0xb7, 0x62, 0x49, 0xc0, 0xc1, 0x8b, 0x71, 0x19 } }
//-----------------------------------------------------------
//Printing
//-----------------------------------------------------------

View File

@ -0,0 +1,44 @@
/* -*- 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/. */
//Basic tests to verify that MacWebAppUtils works
let Ci = Components.interfaces;
let Cc = Components.classes;
let Cu = Components.utils;
let Cr = Components.results;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
function test_find_app()
{
var mwaUtils = Cc["@mozilla.org/widget/mac-web-app-utils;1"].
createInstance(Ci.nsIMacWebAppUtils);
let sig = "com.apple.TextEdit";
let path;
path = mwaUtils.pathForAppWithIdentifier(sig);
do_print("TextEdit path: " + path + "\n");
do_check_neq(path, "");
}
function test_dont_find_fake_app()
{
var mwaUtils = Cc["@mozilla.org/widget/mac-web-app-utils;1"].
createInstance(Ci.nsIMacWebAppUtils);
let sig = "calliope.penitentiary.dramamine";
let path;
path = mwaUtils.pathForAppWithIdentifier(sig);
do_check_eq(path, "");
}
function run_test()
{
test_find_app();
test_dont_find_fake_app();
}

View File

@ -3,3 +3,5 @@ head =
tail =
[test_taskbar_jumplistitems.js]
[test_macwebapputils.js]
skip-if = os != "mac"