diff options
author | xiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-05 18:51:48 +0000 |
---|---|---|
committer | xiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-05 18:51:48 +0000 |
commit | 565f32fc0ffca7ca992aca1a5b43c35e8c96d064 (patch) | |
tree | 79b69dcdf42fc4ef890f4f4f7a67ca41f87175fd | |
parent | f8d5a5e7651d3a1abcb43ece43ef7bf6fbe49790 (diff) | |
download | chromium_src-565f32fc0ffca7ca992aca1a5b43c35e8c96d064.zip chromium_src-565f32fc0ffca7ca992aca1a5b43c35e8c96d064.tar.gz chromium_src-565f32fc0ffca7ca992aca1a5b43c35e8c96d064.tar.bz2 |
cros: Add app mode restrictions.
- White list ash accelerator actions;
- White list browser accelerators;
- Use a limited render view context menu;
BUG=178469
TEST=Verify Ctrl-N etc is disabled and no accelerator/menu to get a browser
window in app mode.
R=zelidrag@chromium.org,sky@chromium.org
Review URL: https://chromiumcodereview.appspot.com/12389083
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@186214 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | ash/accelerators/accelerator_controller.cc | 8 | ||||
-rw-r--r-- | ash/accelerators/accelerator_controller.h | 2 | ||||
-rw-r--r-- | ash/accelerators/accelerator_table.cc | 39 | ||||
-rw-r--r-- | ash/accelerators/accelerator_table.h | 6 | ||||
-rw-r--r-- | ash/shell/shell_delegate_impl.cc | 8 | ||||
-rw-r--r-- | ash/shell/shell_delegate_impl.h | 3 | ||||
-rw-r--r-- | ash/shell_delegate.h | 5 | ||||
-rw-r--r-- | ash/test/test_shell_delegate.cc | 6 | ||||
-rw-r--r-- | ash/test/test_shell_delegate.h | 4 | ||||
-rw-r--r-- | chrome/browser/app_mode/app_mode_utils.cc | 39 | ||||
-rw-r--r-- | chrome/browser/app_mode/app_mode_utils.h | 8 | ||||
-rw-r--r-- | chrome/browser/tab_contents/render_view_context_menu.cc | 47 | ||||
-rw-r--r-- | chrome/browser/tab_contents/render_view_context_menu.h | 1 | ||||
-rw-r--r-- | chrome/browser/ui/ash/chrome_shell_delegate.cc | 4 | ||||
-rw-r--r-- | chrome/browser/ui/ash/chrome_shell_delegate.h | 3 | ||||
-rw-r--r-- | chrome/browser/ui/views/frame/browser_view.cc | 6 |
16 files changed, 169 insertions, 20 deletions
diff --git a/ash/accelerators/accelerator_controller.cc b/ash/accelerators/accelerator_controller.cc index b9e12bb..6829148 100644 --- a/ash/accelerators/accelerator_controller.cc +++ b/ash/accelerators/accelerator_controller.cc @@ -337,6 +337,8 @@ void AcceleratorController::Init() { reserved_actions_.insert(kReservedActions[i]); for (size_t i = 0; i < kNonrepeatableActionsLength; ++i) nonrepeatable_actions_.insert(kNonrepeatableActions[i]); + for (size_t i = 0; i < kActionsAllowedInAppModeLength; ++i) + actions_allowed_in_app_mode_.insert(kActionsAllowedInAppMode[i]); RegisterAccelerators(kAcceleratorData, kAcceleratorDataLength); @@ -417,6 +419,12 @@ bool AcceleratorController::PerformAction(int action, // in the modal window by cycling through its window elements. return true; } + if (shell->delegate()->IsRunningInForcedAppMode() && + actions_allowed_in_app_mode_.find(action) == + actions_allowed_in_app_mode_.end()) { + return false; + } + const ui::KeyboardCode key_code = accelerator.key_code(); // PerformAction() is performed from gesture controllers and passes // empty Accelerator() instance as the second argument. Such events diff --git a/ash/accelerators/accelerator_controller.h b/ash/accelerators/accelerator_controller.h index 468a5bf..53ed723 100644 --- a/ash/accelerators/accelerator_controller.h +++ b/ash/accelerators/accelerator_controller.h @@ -156,6 +156,8 @@ class ASH_EXPORT AcceleratorController : public ui::AcceleratorTarget { std::set<int> reserved_actions_; // Actions which will not be repeated while holding the accelerator key. std::set<int> nonrepeatable_actions_; + // Actions allowed in app mode. + std::set<int> actions_allowed_in_app_mode_; DISALLOW_COPY_AND_ASSIGN(AcceleratorController); }; diff --git a/ash/accelerators/accelerator_table.cc b/ash/accelerators/accelerator_table.cc index eb97242..afb8eaa 100644 --- a/ash/accelerators/accelerator_table.cc +++ b/ash/accelerators/accelerator_table.cc @@ -297,4 +297,43 @@ const AcceleratorAction kNonrepeatableActions[] = { const size_t kNonrepeatableActionsLength = arraysize(kNonrepeatableActions); +const AcceleratorAction kActionsAllowedInAppMode[] = { + BRIGHTNESS_DOWN, + BRIGHTNESS_UP, +#if defined(OS_CHROMEOS) + CYCLE_DISPLAY_MODE, + DISABLE_GPU_WATCHDOG, +#endif // defined(OS_CHROMEOS) + DISABLE_CAPS_LOCK, + KEYBOARD_BRIGHTNESS_DOWN, + KEYBOARD_BRIGHTNESS_UP, + MAGNIFY_SCREEN_ZOOM_IN, // Control+F7 + MAGNIFY_SCREEN_ZOOM_OUT, // Control+F6 + MEDIA_NEXT_TRACK, + MEDIA_PLAY_PAUSE, + MEDIA_PREV_TRACK, + NEXT_IME, + POWER_PRESSED, + POWER_RELEASED, + PREVIOUS_IME, + SWAP_PRIMARY_DISPLAY, + SWITCH_IME, // Switch to another IME depending on the accelerator. + TOGGLE_CAPS_LOCK, + TOGGLE_SPOKEN_FEEDBACK, + TOGGLE_WIFI, + TOUCH_HUD_CLEAR, + VOLUME_DOWN, + VOLUME_MUTE, + VOLUME_UP, +#if !defined(NDEBUG) + PRINT_LAYER_HIERARCHY, + PRINT_VIEW_HIERARCHY, + PRINT_WINDOW_HIERARCHY, + ROTATE_SCREEN, +#endif +}; + +const size_t kActionsAllowedInAppModeLength = + arraysize(kActionsAllowedInAppMode); + } // namespace ash diff --git a/ash/accelerators/accelerator_table.h b/ash/accelerators/accelerator_table.h index 4410997..f8d99ac 100644 --- a/ash/accelerators/accelerator_table.h +++ b/ash/accelerators/accelerator_table.h @@ -149,6 +149,12 @@ ASH_EXPORT extern const AcceleratorAction kNonrepeatableActions[]; // The number of elements in kNonrepeatableActions. ASH_EXPORT extern const size_t kNonrepeatableActionsLength; +// Actions allowed in app mode. +ASH_EXPORT extern const AcceleratorAction kActionsAllowedInAppMode[]; + +// The number of elements in kActionsAllowedInAppMode. +ASH_EXPORT extern const size_t kActionsAllowedInAppModeLength; + } // namespace ash #endif // ASH_ACCELERATORS_ACCELERATOR_TABLE_H_ diff --git a/ash/shell/shell_delegate_impl.cc b/ash/shell/shell_delegate_impl.cc index 8054963..6b57e1e 100644 --- a/ash/shell/shell_delegate_impl.cc +++ b/ash/shell/shell_delegate_impl.cc @@ -4,11 +4,13 @@ #include "ash/shell/shell_delegate_impl.h" +#include <limits> + #include "ash/caps_lock_delegate_stub.h" #include "ash/host/root_window_host_factory.h" +#include "ash/shell/context_menu.h" #include "ash/shell/example_factory.h" #include "ash/shell/launcher_delegate_impl.h" -#include "ash/shell/context_menu.h" #include "ash/shell/toplevel_window.h" #include "ash/shell_window_ids.h" #include "ash/wm/window_util.h" @@ -49,6 +51,10 @@ bool ShellDelegateImpl::IsFirstRunAfterBoot() const { return false; } +bool ShellDelegateImpl::IsRunningInForcedAppMode() const { + return false; +} + bool ShellDelegateImpl::CanLockScreen() const { return true; } diff --git a/ash/shell/shell_delegate_impl.h b/ash/shell/shell_delegate_impl.h index 311ebef..9ac2f78 100644 --- a/ash/shell/shell_delegate_impl.h +++ b/ash/shell/shell_delegate_impl.h @@ -5,6 +5,8 @@ #ifndef ASH_SHELL_SHELL_DELEGATE_IMPL_H_ #define ASH_SHELL_SHELL_DELEGATE_IMPL_H_ +#include <string> + #include "ash/shell_delegate.h" #include "base/compiler_specific.h" @@ -24,6 +26,7 @@ class ShellDelegateImpl : public ash::ShellDelegate { virtual bool IsUserLoggedIn() const OVERRIDE; virtual bool IsSessionStarted() const OVERRIDE; virtual bool IsFirstRunAfterBoot() const OVERRIDE; + virtual bool IsRunningInForcedAppMode() const OVERRIDE; virtual bool CanLockScreen() const OVERRIDE; virtual void LockScreen() OVERRIDE; virtual void UnlockScreen() OVERRIDE; diff --git a/ash/shell_delegate.h b/ash/shell_delegate.h index cd7c10a..8fbfeb3 100644 --- a/ash/shell_delegate.h +++ b/ash/shell_delegate.h @@ -5,7 +5,7 @@ #ifndef ASH_SHELL_DELEGATE_H_ #define ASH_SHELL_DELEGATE_H_ -#include <vector> +#include <string> #include "ash/ash_export.h" #include "ash/magnifier/magnifier_constants.h" @@ -90,6 +90,9 @@ class ASH_EXPORT ShellDelegate { // restarted, typically due to logging in as a guest or logging out. virtual bool IsFirstRunAfterBoot() const = 0; + // Returns true if we're running in forced app mode. + virtual bool IsRunningInForcedAppMode() const = 0; + // Returns true if a user is logged in whose session can be locked (i.e. the // user has a password with which to unlock the session). virtual bool CanLockScreen() const = 0; diff --git a/ash/test/test_shell_delegate.cc b/ash/test/test_shell_delegate.cc index eb0d823..63af2e5 100644 --- a/ash/test/test_shell_delegate.cc +++ b/ash/test/test_shell_delegate.cc @@ -4,7 +4,7 @@ #include "ash/test/test_shell_delegate.h" -#include <algorithm> +#include <limits> #include "ash/caps_lock_delegate_stub.h" #include "ash/host/root_window_host_factory.h" @@ -45,6 +45,10 @@ bool TestShellDelegate::IsFirstRunAfterBoot() const { return false; } +bool TestShellDelegate::IsRunningInForcedAppMode() const { + return false; +} + bool TestShellDelegate::CanLockScreen() const { return user_logged_in_ && can_lock_screen_; } diff --git a/ash/test/test_shell_delegate.h b/ash/test/test_shell_delegate.h index ce6415a..862d6ae 100644 --- a/ash/test/test_shell_delegate.h +++ b/ash/test/test_shell_delegate.h @@ -5,6 +5,8 @@ #ifndef ASH_TEST_TEST_SHELL_DELEGATE_H_ #define ASH_TEST_TEST_SHELL_DELEGATE_H_ +#include <string> + #include "ash/shell_delegate.h" #include "base/compiler_specific.h" #include "base/memory/scoped_ptr.h" @@ -23,6 +25,7 @@ class TestShellDelegate : public ShellDelegate { virtual bool IsUserLoggedIn() const OVERRIDE; virtual bool IsSessionStarted() const OVERRIDE; virtual bool IsFirstRunAfterBoot() const OVERRIDE; + virtual bool IsRunningInForcedAppMode() const OVERRIDE; virtual bool CanLockScreen() const OVERRIDE; virtual void LockScreen() OVERRIDE; virtual void UnlockScreen() OVERRIDE; @@ -71,6 +74,7 @@ class TestShellDelegate : public ShellDelegate { virtual string16 GetProductName() const OVERRIDE; int num_exit_requests() const { return num_exit_requests_; } + private: friend class ash::test::AshTestBase; diff --git a/chrome/browser/app_mode/app_mode_utils.cc b/chrome/browser/app_mode/app_mode_utils.cc index ca3baa5..c4cdeb2 100644 --- a/chrome/browser/app_mode/app_mode_utils.cc +++ b/chrome/browser/app_mode/app_mode_utils.cc @@ -4,16 +4,51 @@ #include "chrome/browser/app_mode/app_mode_utils.h" +#include "base/basictypes.h" #include "base/command_line.h" +#include "base/logging.h" +#include "chrome/app/chrome_command_ids.h" #include "chrome/common/chrome_switches.h" namespace chrome { +bool IsCommandAllowedInAppMode(int command_id) { + DCHECK(IsRunningInForcedAppMode()); + + const int kAllowed[] = { + IDC_BACK, + IDC_FORWARD, + IDC_RELOAD, + IDC_STOP, + IDC_RELOAD_IGNORING_CACHE, + IDC_RELOAD_CLEARING_CACHE, + IDC_CUT, + IDC_COPY, + IDC_COPY_URL, + IDC_PASTE, + IDC_ZOOM_PLUS, + IDC_ZOOM_NORMAL, + IDC_ZOOM_MINUS, + }; + + for (size_t i = 0; i < arraysize(kAllowed); ++i) { + if (kAllowed[i] == command_id) + return true; + } + + return false; +} + bool IsRunningInAppMode() { CommandLine* command_line = CommandLine::ForCurrentProcess(); return command_line->HasSwitch(switches::kKioskMode) || - (command_line->HasSwitch(switches::kForceAppMode) && - command_line->HasSwitch(switches::kAppId)); + IsRunningInForcedAppMode(); +} + +bool IsRunningInForcedAppMode() { + CommandLine* command_line = CommandLine::ForCurrentProcess(); + return command_line->HasSwitch(switches::kForceAppMode) && + command_line->HasSwitch(switches::kAppId); } } // namespace switches diff --git a/chrome/browser/app_mode/app_mode_utils.h b/chrome/browser/app_mode/app_mode_utils.h index 1fe9605..487b454 100644 --- a/chrome/browser/app_mode/app_mode_utils.h +++ b/chrome/browser/app_mode/app_mode_utils.h @@ -7,9 +7,15 @@ namespace chrome { -// Return true if browser process is run in kiosk or forces app mode. +// Returns true if the given browser command is allowed in app mode. +bool IsCommandAllowedInAppMode(int command_id); + +// Return true if browser process is run in kiosk or forced app mode. bool IsRunningInAppMode(); +// Return true if browser process is run in forced app mode. +bool IsRunningInForcedAppMode(); + } // namespace switches #endif // CHROME_BROWSER_APP_MODE_APP_MODE_UTILS_H_ diff --git a/chrome/browser/tab_contents/render_view_context_menu.cc b/chrome/browser/tab_contents/render_view_context_menu.cc index ba0b6bb..8cb23b5 100644 --- a/chrome/browser/tab_contents/render_view_context_menu.cc +++ b/chrome/browser/tab_contents/render_view_context_menu.cc @@ -18,6 +18,7 @@ #include "base/time.h" #include "base/utf_string_conversions.h" #include "chrome/app/chrome_command_ids.h" +#include "chrome/browser/app_mode/app_mode_utils.h" #include "chrome/browser/autocomplete/autocomplete_classifier.h" #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" #include "chrome/browser/autocomplete/autocomplete_match.h" @@ -231,9 +232,9 @@ void DevToolsInspectElementAt(RenderViewHost* rvh, int x, int y) { } // Helper function to escape "&" as "&&". -void EscapeAmpersands(string16& text) { +void EscapeAmpersands(string16* text) { const char16 ampersand[] = {'&', 0}; - ReplaceChars(text, ampersand, ASCIIToUTF16("&&"), &text); + ReplaceChars(*text, ampersand, ASCIIToUTF16("&&"), text); } } // namespace @@ -298,9 +299,9 @@ bool RenderViewContextMenu::ExtensionContextAndPatternMatch( const content::ContextMenuParams& params, MenuItem::ContextList contexts, const extensions::URLPatternSet& target_url_patterns) { - bool has_link = !params.link_url.is_empty(); - bool has_selection = !params.selection_text.empty(); - bool in_frame = !params.frame_url.is_empty(); + const bool has_link = !params.link_url.is_empty(); + const bool has_selection = !params.selection_text.empty(); + const bool in_frame = !params.frame_url.is_empty(); if (contexts.Contains(MenuItem::ALL) || (has_selection && contexts.Contains(MenuItem::SELECTION)) || @@ -395,7 +396,7 @@ void RenderViewContextMenu::AppendAllExtensionItems() { for (i = sorted_ids.begin(); i != sorted_ids.end(); ++i) { string16 printable_selection_text = PrintableSelectionText(); - EscapeAmpersands(printable_selection_text); + EscapeAmpersands(&printable_selection_text); extension_items_.AppendExtensionItems(i->second, printable_selection_text, &index); @@ -406,6 +407,11 @@ void RenderViewContextMenu::AppendAllExtensionItems() { } void RenderViewContextMenu::InitMenu() { + if (chrome::IsRunningInForcedAppMode()) { + AppendAppModeItems(); + return; + } + chrome::ViewType view_type = chrome::GetViewType(source_web_contents_); if (view_type == chrome::VIEW_TYPE_APP_SHELL) { AppendPlatformAppItems(); @@ -418,8 +424,8 @@ void RenderViewContextMenu::InitMenu() { return; } - bool has_link = !params_.unfiltered_link_url.is_empty(); - bool has_selection = !params_.selection_text.empty(); + const bool has_link = !params_.unfiltered_link_url.is_empty(); + const bool has_selection = !params_.selection_text.empty(); if (AppendCustomItems()) { // If there's a selection, don't early return when there are custom items, @@ -519,6 +525,15 @@ const Extension* RenderViewContextMenu::GetExtension() const { source_web_contents_->GetRenderViewHost()); } +void RenderViewContextMenu::AppendAppModeItems() { + const bool has_selection = !params_.selection_text.empty(); + + if (params_.is_editable) + AppendEditableItems(); + else if (has_selection) + AppendCopyItem(); +} + void RenderViewContextMenu::AppendPlatformAppItems() { const Extension* platform_app = GetExtension(); @@ -528,7 +543,7 @@ void RenderViewContextMenu::AppendPlatformAppItems() { DCHECK(platform_app->is_platform_app()); - bool has_selection = !params_.selection_text.empty(); + const bool has_selection = !params_.selection_text.empty(); // Add undo/redo, cut/copy/paste etc for text fields. if (params_.is_editable) @@ -558,7 +573,7 @@ void RenderViewContextMenu::AppendPlatformAppItems() { } void RenderViewContextMenu::AppendPopupExtensionItems() { - bool has_selection = !params_.selection_text.empty(); + const bool has_selection = !params_.selection_text.empty(); if (params_.is_editable) AppendEditableItems(); @@ -832,7 +847,7 @@ void RenderViewContextMenu::AppendSearchProvider() { return; string16 printable_selection_text = PrintableSelectionText(); - EscapeAmpersands(printable_selection_text); + EscapeAmpersands(&printable_selection_text); if (AutocompleteMatch::IsSearchType(match.type)) { const TemplateURL* const default_provider = @@ -858,7 +873,10 @@ void RenderViewContextMenu::AppendSearchProvider() { } void RenderViewContextMenu::AppendEditableItems() { - AppendSpellingSuggestionsSubMenu(); + const bool use_spellcheck_and_search = !chrome::IsRunningInForcedAppMode(); + + if (use_spellcheck_and_search) + AppendSpellingSuggestionsSubMenu(); menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_UNDO, IDS_CONTENT_CONTEXT_UNDO); @@ -877,13 +895,14 @@ void RenderViewContextMenu::AppendEditableItems() { IDS_CONTENT_CONTEXT_DELETE); menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); - if (!params_.keyword_url.is_empty()) { + if (use_spellcheck_and_search && !params_.keyword_url.is_empty()) { menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_ADDSEARCHENGINE, IDS_CONTENT_CONTEXT_ADDSEARCHENGINE); menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); } - AppendSpellcheckOptionsSubMenu(); + if (use_spellcheck_and_search) + AppendSpellcheckOptionsSubMenu(); AppendSpeechInputOptionsSubMenu(); AppendPlatformEditableItems(); diff --git a/chrome/browser/tab_contents/render_view_context_menu.h b/chrome/browser/tab_contents/render_view_context_menu.h index a44e4b8..d93b6ed 100644 --- a/chrome/browser/tab_contents/render_view_context_menu.h +++ b/chrome/browser/tab_contents/render_view_context_menu.h @@ -204,6 +204,7 @@ class RenderViewContextMenu : public ui::SimpleMenuModel::Delegate, // Gets the extension (if any) associated with the WebContents that we're in. const extensions::Extension* GetExtension() const; + void AppendAppModeItems(); void AppendPlatformAppItems(); void AppendPopupExtensionItems(); void AppendPanelItems(); diff --git a/chrome/browser/ui/ash/chrome_shell_delegate.cc b/chrome/browser/ui/ash/chrome_shell_delegate.cc index 5699754..d91e568 100644 --- a/chrome/browser/ui/ash/chrome_shell_delegate.cc +++ b/chrome/browser/ui/ash/chrome_shell_delegate.cc @@ -58,6 +58,10 @@ ChromeShellDelegate::~ChromeShellDelegate() { instance_ = NULL; } +bool ChromeShellDelegate::IsRunningInForcedAppMode() const { + return chrome::IsRunningInForcedAppMode(); +} + void ChromeShellDelegate::UnlockScreen() { // This is used only for testing thus far. NOTIMPLEMENTED(); diff --git a/chrome/browser/ui/ash/chrome_shell_delegate.h b/chrome/browser/ui/ash/chrome_shell_delegate.h index 47b3dad..d99511d 100644 --- a/chrome/browser/ui/ash/chrome_shell_delegate.h +++ b/chrome/browser/ui/ash/chrome_shell_delegate.h @@ -5,6 +5,8 @@ #ifndef CHROME_BROWSER_UI_ASH_CHROME_SHELL_DELEGATE_H_ #define CHROME_BROWSER_UI_ASH_CHROME_SHELL_DELEGATE_H_ +#include <string> + #include "ash/launcher/launcher_types.h" #include "ash/shell_delegate.h" #include "base/basictypes.h" @@ -37,6 +39,7 @@ class ChromeShellDelegate : public ash::ShellDelegate, virtual bool IsUserLoggedIn() const OVERRIDE; virtual bool IsSessionStarted() const OVERRIDE; virtual bool IsFirstRunAfterBoot() const OVERRIDE; + virtual bool IsRunningInForcedAppMode() const OVERRIDE; virtual bool CanLockScreen() const OVERRIDE; virtual void LockScreen() OVERRIDE; virtual void UnlockScreen() OVERRIDE; diff --git a/chrome/browser/ui/views/frame/browser_view.cc b/chrome/browser/ui/views/frame/browser_view.cc index 960d642..363e409 100644 --- a/chrome/browser/ui/views/frame/browser_view.cc +++ b/chrome/browser/ui/views/frame/browser_view.cc @@ -2455,10 +2455,16 @@ void BrowserView::LoadAccelerators() { DCHECK(focus_manager); // Let's fill our own accelerator table. + const bool is_app_mode = chrome::IsRunningInForcedAppMode(); const std::vector<chrome::AcceleratorMapping> accelerator_list( chrome::GetAcceleratorList()); for (std::vector<chrome::AcceleratorMapping>::const_iterator it = accelerator_list.begin(); it != accelerator_list.end(); ++it) { + // In app mode, only allow accelerators of white listed commands to pass + // through. + if (is_app_mode && !chrome::IsCommandAllowedInAppMode(it->command_id)) + continue; + ui::Accelerator accelerator(it->keycode, it->modifiers); accelerator_table_[accelerator] = it->command_id; |