Home | History | Annotate | Download | only in cocoa
      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.
     33 class RWHVMEditCommandHelper {
     34    FRIEND_TEST_ALL_PREFIXES(RWHVMEditCommandHelperTest,
     35                             TestAddEditingSelectorsToClass);
     36    FRIEND_TEST_ALL_PREFIXES(RWHVMEditCommandHelperTest,
     37                             TestEditingCommandDelivery);
     38 
     39  public:
     40   RWHVMEditCommandHelper();
     41   ~RWHVMEditCommandHelper();
     42 
     43   // Adds editing selectors to the objc class using the objc runtime APIs.
     44   // Each selector is connected to a single c method which forwards the message
     45   // to WebCore's ExecuteEditCommand() function.
     46   // This method is idempotent.
     47   // The class passed in must conform to the RenderWidgetHostViewMacOwner
     48   // protocol.
     49   void AddEditingSelectorsToClass(Class klass);
     50 
     51   // Is a given menu item currently enabled?
     52   // SEL - the objc selector currently associated with an NSMenuItem.
     53   // owner - An object we can retrieve a RenderWidgetHostViewMac from to
     54   // determine the command states.
     55   bool IsMenuItemEnabled(SEL item_action,
     56                          id<RenderWidgetHostViewMacOwner> owner);
     57 
     58   // Converts an editing selector into a command name that can be sent to
     59   // webkit.
     60   static NSString* CommandNameForSelector(SEL selector);
     61 
     62  protected:
     63   // Gets a list of all the selectors that AddEditingSelectorsToClass adds to
     64   // the aforementioned class.
     65   // returns an array of NSStrings WITHOUT the trailing ':'s.
     66   NSArray* GetEditSelectorNames();
     67 
     68  private:
     69   base::hash_set<std::string> edit_command_set_;
     70   DISALLOW_COPY_AND_ASSIGN(RWHVMEditCommandHelper);
     71 };
     72 
     73 #endif  // CHROME_BROWSER_UI_COCOA_RWHVM_EDITCOMMAND_HELPER_H_
     74