1 // Copyright 2014 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.
5 #include "content/browser/cocoa/system_hotkey_helper_mac.h"
8 #include "base/files/file_path.h"
9 #include "base/mac/foundation_util.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/metrics/histogram.h"
12 #include "content/browser/cocoa/system_hotkey_map.h"
13 #include "content/public/browser/browser_thread.h"
17 NSString* kSystemHotkeyPlistExtension =
18 @"/Preferences/com.apple.symbolichotkeys.plist";
20 // Amount of time to delay loading the hotkeys in seconds.
21 const int kLoadHotkeysDelaySeconds = 10;
28 SystemHotkeyHelperMac* SystemHotkeyHelperMac::GetInstance() {
29 return base::Singleton<SystemHotkeyHelperMac>::get();
32 void SystemHotkeyHelperMac::DeferredLoadSystemHotkeys() {
33 BrowserThread::PostDelayedTask(
36 base::Bind(&SystemHotkeyHelperMac::LoadSystemHotkeys,
37 base::Unretained(this)),
38 base::TimeDelta::FromSeconds(kLoadHotkeysDelaySeconds));
41 SystemHotkeyHelperMac::SystemHotkeyHelperMac() : map_(new SystemHotkeyMap) {
44 SystemHotkeyHelperMac::~SystemHotkeyHelperMac() {
47 void SystemHotkeyHelperMac::LoadSystemHotkeys() {
48 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
50 std::string library_path(base::mac::GetUserLibraryPath().value());
51 NSString* expanded_file_path =
52 [NSString stringWithFormat:@"%s%@",
54 kSystemHotkeyPlistExtension];
56 // Loads the file into memory.
57 NSData* data = [NSData dataWithContentsOfFile:expanded_file_path];
58 // Intentionally create the object with +1 retain count, as FileDidLoad
59 // will destroy the object.
60 NSDictionary* dictionary = [SystemHotkeyMap::DictionaryFromData(data) retain];
62 BrowserThread::PostTask(BrowserThread::UI,
64 base::Bind(&SystemHotkeyHelperMac::FileDidLoad,
65 base::Unretained(this),
69 void SystemHotkeyHelperMac::FileDidLoad(NSDictionary* dictionary) {
70 DCHECK_CURRENTLY_ON(BrowserThread::UI);
72 bool success = map()->ParseDictionary(dictionary);
73 UMA_HISTOGRAM_BOOLEAN("OSX.SystemHotkeyMap.LoadSuccess", success);
77 } // namespace content