summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/accessible_widget_helper_gtk.cc
blob: 029aca46ea9810f12ee4f0a9570ab390be34f952 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/gtk/accessible_widget_helper_gtk.h"

#include "app/l10n_util.h"
#include "chrome/browser/accessibility_events.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/notification_service.h"

AccessibleWidgetHelper::AccessibleWidgetHelper(
    GtkWidget* root_widget, Profile* profile)
    : accessibility_event_router_(AccessibilityEventRouterGtk::GetInstance()),
      profile_(profile),
      root_widget_(root_widget) {
  CHECK(profile_);
  accessibility_event_router_->AddRootWidget(root_widget_, profile_);
}

AccessibleWidgetHelper::~AccessibleWidgetHelper() {
  if (!window_title_.empty()) {
    AccessibilityWindowInfo info(profile_, window_title_);
    NotificationService::current()->Notify(
        NotificationType::ACCESSIBILITY_WINDOW_CLOSED,
        Source<Profile>(profile_),
        Details<AccessibilityWindowInfo>(&info));
  }

  if (root_widget_)
    accessibility_event_router_->RemoveRootWidget(root_widget_);
  for (std::set<GtkWidget*>::iterator it = managed_widgets_.begin();
       it != managed_widgets_.end();
       ++it) {
    accessibility_event_router_->RemoveWidgetNameOverride(*it);
  }
}

void AccessibleWidgetHelper::SendOpenWindowNotification(
    const std::string& window_title) {
  window_title_ = window_title;
  AccessibilityWindowInfo info(profile_, window_title);
  NotificationService::current()->Notify(
      NotificationType::ACCESSIBILITY_WINDOW_OPENED,
      Source<Profile>(profile_),
      Details<AccessibilityWindowInfo>(&info));
}

void AccessibleWidgetHelper::SetWidgetName(
    GtkWidget* widget, std::string name) {
  if (managed_widgets_.find(widget) != managed_widgets_.end()) {
    // AccessibilityEventRouterGtk reference-counts its widgets, but we
    // don't. In order to avoid a memory leak, tell the event router
    // to deref first, so the resulting refcount is unchanged after we
    // call SetWidgetName.
    accessibility_event_router_->RemoveWidgetNameOverride(widget);
  }

  accessibility_event_router_->AddWidgetNameOverride(widget, name);
  managed_widgets_.insert(widget);
}

void AccessibleWidgetHelper::SetWidgetName(
    GtkWidget* widget, int string_id) {
  SetWidgetName(widget, l10n_util::GetStringUTF8(string_id));
}