1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 *
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 "nsSound.h"
8#include "nsContentUtils.h"
9#include "nsObjCExceptions.h"
10#include "nsNetUtil.h"
11#include "nsCOMPtr.h"
12#include "nsIURL.h"
13#include "nsString.h"
14
15#import <Cocoa/Cocoa.h>
16
17NS_IMPL_ISUPPORTS(nsSound, nsISound, nsIStreamLoaderObserver)
18
19nsSound::nsSound() {}
20
21nsSound::~nsSound() {}
22
23NS_IMETHODIMP
24nsSound::Beep() {
25  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
26
27  NSBeep();
28  return NS_OK;
29
30  NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
31}
32
33NS_IMETHODIMP
34nsSound::OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* context, nsresult aStatus,
35                          uint32_t dataLen, const uint8_t* data) {
36  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
37
38  NSData* value = [NSData dataWithBytes:data length:dataLen];
39
40  NSSound* sound = [[NSSound alloc] initWithData:value];
41
42  [sound play];
43
44  [sound autorelease];
45
46  return NS_OK;
47
48  NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
49}
50
51NS_IMETHODIMP
52nsSound::Play(nsIURL* aURL) {
53  nsCOMPtr<nsIURI> uri(aURL);
54  nsCOMPtr<nsIStreamLoader> loader;
55  return NS_NewStreamLoader(getter_AddRefs(loader), uri,
56                            this,  // aObserver
57                            nsContentUtils::GetSystemPrincipal(),
58                            nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
59                            nsIContentPolicy::TYPE_OTHER);
60}
61
62NS_IMETHODIMP
63nsSound::Init() { return NS_OK; }
64
65NS_IMETHODIMP
66nsSound::PlayEventSound(uint32_t aEventId) {
67  // Mac doesn't have system sound settings for each user actions.
68  return NS_OK;
69}
70