1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import <Cocoa/Cocoa.h>
6
7#import "content/browser/accessibility/browser_accessibility_mac.h"
8
9#import "content/browser/accessibility/browser_accessibility_cocoa.h"
10#include "content/browser/accessibility/browser_accessibility_manager_mac.h"
11
12namespace content {
13
14// Static.
15BrowserAccessibility* BrowserAccessibility::Create() {
16  return new BrowserAccessibilityMac();
17}
18
19BrowserAccessibilityMac::BrowserAccessibilityMac()
20    : browser_accessibility_cocoa_(NULL) {
21}
22
23void BrowserAccessibilityMac::OnDataChanged() {
24  BrowserAccessibility::OnDataChanged();
25
26  if (browser_accessibility_cocoa_) {
27    [browser_accessibility_cocoa_ childrenChanged];
28    return;
29  }
30
31  // We take ownership of the cocoa obj here.
32  browser_accessibility_cocoa_ = [[BrowserAccessibilityCocoa alloc]
33      initWithObject:this];
34}
35
36void BrowserAccessibilityMac::NativeReleaseReference() {
37  // Detach this object from |browser_accessibility_cocoa_| so it
38  // no longer has a pointer to this object.
39  [browser_accessibility_cocoa_ detach];
40  // Now, release it - but at this point, other processes may have a
41  // reference to the cocoa object.
42  [browser_accessibility_cocoa_ release];
43  // Finally, it's safe to delete this since we've detached.
44  delete this;
45}
46
47bool BrowserAccessibilityMac::IsNative() const {
48  return true;
49}
50
51void BrowserAccessibilityMac::RecreateNativeObject() {
52  if (!browser_accessibility_cocoa_)
53    return;
54
55  // Preserve the children so that recreating the native object doesn't
56  // end up recreating the whole subtree.
57  base::scoped_nsobject<NSMutableArray> children;
58  [browser_accessibility_cocoa_ swapChildren:&children];
59  [browser_accessibility_cocoa_ detach];
60  [browser_accessibility_cocoa_ release];
61  browser_accessibility_cocoa_ = [[BrowserAccessibilityCocoa alloc]
62      initWithObject:this];
63  [browser_accessibility_cocoa_ swapChildren:&children];
64}
65
66BrowserAccessibilityCocoa* BrowserAccessibility::ToBrowserAccessibilityCocoa() {
67  return static_cast<BrowserAccessibilityMac*>(this)->
68      native_view();
69}
70
71}  // namespace content
72