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