rwhvm_editcommand_helper.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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#ifndef CHROME_BROWSER_UI_COCOA_RWHVM_EDITCOMMAND_HELPER_H_
6#define CHROME_BROWSER_UI_COCOA_RWHVM_EDITCOMMAND_HELPER_H_
7#pragma once
8
9#import <Cocoa/Cocoa.h>
10
11#include "base/basictypes.h"
12#include "base/hash_tables.h"
13#include "base/gtest_prod_util.h"
14#include "chrome/browser/renderer_host/render_widget_host_view_mac.h"
15
16// RenderWidgetHostViewMacEditCommandHelper is the real name of this class
17// but that's too long, so we use a shorter version.
18//
19// This class mimics the behavior of WebKit's WebView class in a way that makes
20// sense for Chrome.
21//
22// WebCore has the concept of "core commands", basically named actions such as
23// "Select All" and "Move Cursor Left".  The commands are executed using their
24// string value by WebCore.
25//
26// This class is responsible for 2 things:
27// 1. Provide an abstraction to determine the enabled/disabled state of menu
28// items that correspond to edit commands.
29// 2. Hook up a bunch of objc selectors to the RenderWidgetHostViewCocoa object.
30// (note that this is not a misspelling of RenderWidgetHostViewMac, it's in
31//  fact a distinct object) When these selectors are called, the relevant
32// edit command is executed in WebCore.
33class RWHVMEditCommandHelper {
34   FRIEND_TEST_ALL_PREFIXES(RWHVMEditCommandHelperTest,
35                            TestAddEditingSelectorsToClass);
36   FRIEND_TEST_ALL_PREFIXES(RWHVMEditCommandHelperTest,
37                            TestEditingCommandDelivery);
38
39 public:
40  RWHVMEditCommandHelper();
41
42  // Adds editing selectors to the objc class using the objc runtime APIs.
43  // Each selector is connected to a single c method which forwards the message
44  // to WebCore's ExecuteEditCommand() function.
45  // This method is idempotent.
46  // The class passed in must conform to the RenderWidgetHostViewMacOwner
47  // protocol.
48  void AddEditingSelectorsToClass(Class klass);
49
50  // Is a given menu item currently enabled?
51  // SEL - the objc selector currently associated with an NSMenuItem.
52  // owner - An object we can retrieve a RenderWidgetHostViewMac from to
53  // determine the command states.
54  bool IsMenuItemEnabled(SEL item_action,
55                         id<RenderWidgetHostViewMacOwner> owner);
56
57  // Converts an editing selector into a command name that can be sent to
58  // webkit.
59  static NSString* CommandNameForSelector(SEL selector);
60
61 protected:
62  // Gets a list of all the selectors that AddEditingSelectorsToClass adds to
63  // the aforementioned class.
64  // returns an array of NSStrings WITHOUT the trailing ':'s.
65  NSArray* GetEditSelectorNames();
66
67 private:
68  base::hash_set<std::string> edit_command_set_;
69  DISALLOW_COPY_AND_ASSIGN(RWHVMEditCommandHelper);
70};
71
72#endif  // CHROME_BROWSER_UI_COCOA_RWHVM_EDITCOMMAND_HELPER_H_
73