1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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{
21}
22
23nsSound::~nsSound()
24{
25}
26
27NS_IMETHODIMP
28nsSound::Beep()
29{
30  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
31
32  NSBeep();
33  return NS_OK;
34
35  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
36}
37
38NS_IMETHODIMP
39nsSound::OnStreamComplete(nsIStreamLoader *aLoader,
40                          nsISupports *context,
41                          nsresult aStatus,
42                          uint32_t dataLen,
43                          const uint8_t *data)
44{
45  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
46
47  NSData *value = [NSData dataWithBytes:data length:dataLen];
48
49  NSSound *sound = [[NSSound alloc] initWithData:value];
50
51  [sound play];
52
53  [sound autorelease];
54
55  return NS_OK;
56
57  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
58}
59
60NS_IMETHODIMP
61nsSound::Play(nsIURL *aURL)
62{
63  nsCOMPtr<nsIURI> uri(do_QueryInterface(aURL));
64  nsCOMPtr<nsIStreamLoader> loader;
65  return NS_NewStreamLoader(getter_AddRefs(loader),
66                            uri,
67                            this, // aObserver
68                            nsContentUtils::GetSystemPrincipal(),
69                            nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
70                            nsIContentPolicy::TYPE_OTHER);
71}
72
73NS_IMETHODIMP
74nsSound::Init()
75{
76  return NS_OK;
77}
78
79NS_IMETHODIMP
80nsSound::PlaySystemSound(const nsAString &aSoundAlias)
81{
82  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
83
84  if (NS_IsMozAliasSound(aSoundAlias)) {
85    NS_WARNING("nsISound::playSystemSound is called with \"_moz_\" events, they are obsolete, use nsISound::playEventSound instead");
86    // Mac doesn't have system sound settings for each user actions.
87    return NS_OK;
88  }
89
90  NSString *name = [NSString stringWithCharacters:reinterpret_cast<const unichar*>(aSoundAlias.BeginReading())
91                                           length:aSoundAlias.Length()];
92  NSSound *sound = [NSSound soundNamed:name];
93  if (sound) {
94    [sound stop];
95    [sound play];
96  }
97
98  return NS_OK;
99
100  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
101}
102
103NS_IMETHODIMP
104nsSound::PlayEventSound(uint32_t aEventId)
105{
106  // Mac doesn't have system sound settings for each user actions.
107  return NS_OK;
108}
109