blob: 9d75502ccdfd2f69d025a3cfb5f5a9fdb34c19c7 [file] [log] [blame]
Robert Sesek03282612018-10-31 15:48:061// Copyright (c) 2013 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#include "chrome/browser/ui/browser_commands_mac.h"
6
7#include <unistd.h>
8
9#import <Cocoa/Cocoa.h>
10
11#include "base/logging.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/browser/ui/browser.h"
14#include "chrome/browser/ui/browser_commands.h"
15#include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
16#include "chrome/common/pref_names.h"
17#include "components/prefs/pref_service.h"
18
19namespace chrome {
20
21void ToggleFullscreenToolbar(Browser* browser) {
22 DCHECK(browser);
23
24 // Toggle the value of the preference.
25 PrefService* prefs = browser->profile()->GetPrefs();
26 bool show_toolbar = prefs->GetBoolean(prefs::kShowFullscreenToolbar);
27 prefs->SetBoolean(prefs::kShowFullscreenToolbar, !show_toolbar);
28}
29
30void ToggleJavaScriptFromAppleEventsAllowed(Browser* browser) {
31 CGEventRef cg_event = [[NSApp currentEvent] CGEvent];
32 if (!cg_event)
33 return;
34
35 // If the event is from another process, do not allow it to toggle this
36 // secure setting.
37 int sender_pid =
38 CGEventGetIntegerValueField(cg_event, kCGEventSourceUnixProcessID);
39 if (sender_pid != 0 && sender_pid != getpid()) {
40 DLOG(ERROR)
41 << "Dropping JS AppleScript toggle, event not from browser, from "
42 << sender_pid;
43 return;
44 }
45
46 // Only allow events generated in the HID system to toggle this setting.
47 int event_source =
48 CGEventGetIntegerValueField(cg_event, kCGEventSourceStateID);
49 if (event_source != kCGEventSourceStateHIDSystemState) {
50 DLOG(ERROR) << "Dropping JS AppleScript toggle, event source state not "
51 "from HID, from "
52 << event_source;
53 return;
54 }
55
56 PrefService* prefs = browser->profile()->GetPrefs();
57 prefs->SetBoolean(prefs::kAllowJavascriptAppleEvents,
58 !prefs->GetBoolean(prefs::kAllowJavascriptAppleEvents));
59}
60
61} // namespace chrome