[email protected] | 8e837ec | 2013-01-31 01:48:33 | [diff] [blame] | 1 | // 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/app_mode/app_mode_utils.h" |
| 6 | |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | 8e837ec | 2013-01-31 01:48:33 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 565f32fc0 | 2013-03-05 18:51:48 | [diff] [blame] | 10 | #include "base/logging.h" |
Toni Barzic | fc4a81a | 2018-01-31 01:48:59 | [diff] [blame] | 11 | #include "base/optional.h" |
Avi Drissman | d251e91 | 2018-12-26 15:46:37 | [diff] [blame] | 12 | #include "base/stl_util.h" |
[email protected] | 565f32fc0 | 2013-03-05 18:51:48 | [diff] [blame] | 13 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | 8e837ec | 2013-01-31 01:48:33 | [diff] [blame] | 14 | #include "chrome/common/chrome_switches.h" |
| 15 | |
| 16 | namespace chrome { |
| 17 | |
Toni Barzic | fc4a81a | 2018-01-31 01:48:59 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | // If the device is running in forced app mode, returns the ID of the app for |
| 21 | // which the device is forced in app mode. Otherwise, returns nullopt. |
| 22 | base::Optional<std::string> GetForcedAppModeApp() { |
| 23 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 24 | if (!command_line->HasSwitch(switches::kForceAppMode) || |
| 25 | !command_line->HasSwitch(switches::kAppId)) { |
| 26 | return base::nullopt; |
| 27 | } |
| 28 | |
| 29 | return command_line->GetSwitchValueASCII(switches::kAppId); |
| 30 | } |
| 31 | |
| 32 | } // namespace |
| 33 | |
[email protected] | 565f32fc0 | 2013-03-05 18:51:48 | [diff] [blame] | 34 | bool IsCommandAllowedInAppMode(int command_id) { |
| 35 | DCHECK(IsRunningInForcedAppMode()); |
| 36 | |
| 37 | const int kAllowed[] = { |
toyoshim | 7dad4b118 | 2016-04-01 14:28:05 | [diff] [blame] | 38 | IDC_BACK, |
| 39 | IDC_FORWARD, |
| 40 | IDC_RELOAD, |
Jeff Fisher | 6cc1ce7b | 2019-06-28 23:02:38 | [diff] [blame] | 41 | IDC_CLOSE_FIND_OR_STOP, |
toyoshim | 7dad4b118 | 2016-04-01 14:28:05 | [diff] [blame] | 42 | IDC_STOP, |
| 43 | IDC_RELOAD_BYPASSING_CACHE, |
| 44 | IDC_RELOAD_CLEARING_CACHE, |
| 45 | IDC_CUT, |
| 46 | IDC_COPY, |
| 47 | IDC_PASTE, |
| 48 | IDC_ZOOM_PLUS, |
| 49 | IDC_ZOOM_NORMAL, |
| 50 | IDC_ZOOM_MINUS, |
[email protected] | 565f32fc0 | 2013-03-05 18:51:48 | [diff] [blame] | 51 | }; |
| 52 | |
Avi Drissman | d251e91 | 2018-12-26 15:46:37 | [diff] [blame] | 53 | for (size_t i = 0; i < base::size(kAllowed); ++i) { |
[email protected] | 565f32fc0 | 2013-03-05 18:51:48 | [diff] [blame] | 54 | if (kAllowed[i] == command_id) |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | return false; |
| 59 | } |
| 60 | |
[email protected] | 8e837ec | 2013-01-31 01:48:33 | [diff] [blame] | 61 | bool IsRunningInAppMode() { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 62 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
[email protected] | 8e837ec | 2013-01-31 01:48:33 | [diff] [blame] | 63 | return command_line->HasSwitch(switches::kKioskMode) || |
poromov | 008f8d0 | 2016-11-17 16:34:19 | [diff] [blame] | 64 | IsRunningInForcedAppMode(); |
[email protected] | 565f32fc0 | 2013-03-05 18:51:48 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool IsRunningInForcedAppMode() { |
Anatoliy Potapchuk | ce6f880 | 2020-01-30 09:30:34 | [diff] [blame] | 68 | return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 69 | switches::kForceAppMode); |
Toni Barzic | fc4a81a | 2018-01-31 01:48:59 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | bool IsRunningInForcedAppModeForApp(const std::string& app_id) { |
| 73 | DCHECK(!app_id.empty()); |
| 74 | |
| 75 | base::Optional<std::string> forced_app_mode_app = GetForcedAppModeApp(); |
| 76 | if (!forced_app_mode_app.has_value()) |
| 77 | return false; |
| 78 | |
| 79 | return app_id == forced_app_mode_app.value(); |
[email protected] | 8e837ec | 2013-01-31 01:48:33 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | 15c7e8af | 2013-07-10 02:10:32 | [diff] [blame] | 82 | } // namespace chrome |