summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/npapi/plugin_instance_mac.mm
blob: 8c8016faa58b7b679cada0e454f0dba636ab00da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <AppKit/AppKit.h>

#include "base/logging.h"
#include "build/build_config.h"
#include "webkit/plugins/npapi/plugin_instance.h"

// When C++ exceptions are disabled, the C++ library defines |try| and
// |catch| so as to allow exception-expecting C++ code to build properly when
// language support for exceptions is not present.  These macros interfere
// with the use of |@try| and |@catch| in Objective-C files such as this one.
// Undefine these macros here, after everything has been #included, since
// there will be no C++ uses and only Objective-C uses from this point on.
#undef try
#undef catch

namespace webkit {
namespace npapi {

namespace {

// Returns an autoreleased NSEvent constructed from the given np_event,
// targeting the given window.
NSEvent* NSEventForNPCocoaEvent(NPCocoaEvent* np_event, NSWindow* window) {
  bool mouse_down = 1;
  switch (np_event->type) {
    case NPCocoaEventMouseDown:
      mouse_down = 1;
      break;
    case NPCocoaEventMouseUp:
      mouse_down = 0;
      break;
    default:
      // If plugins start bringing up context menus for things other than
      // clicks, this will need more plumbing; for now just log it and proceed
      // as if it were a mouse down.
      NOTREACHED();
  }
  NSEventType event_type = NSLeftMouseDown;
  switch (np_event->data.mouse.buttonNumber) {
    case 0:
      event_type = mouse_down ? NSLeftMouseDown : NSLeftMouseUp;
      break;
    case 1:
      event_type = mouse_down ? NSRightMouseDown : NSRightMouseUp;
      break;
    default:
      event_type = mouse_down ? NSOtherMouseDown : NSOtherMouseUp;
      break;
  }

  NSInteger click_count = np_event->data.mouse.clickCount;
  NSInteger modifiers = np_event->data.mouse.modifierFlags;
  // NPCocoaEvent doesn't have a timestamp, so just use the current time.
  NSEvent* event =
      [NSEvent mouseEventWithType:event_type
                         location:NSMakePoint(0, 0)
                    modifierFlags:modifiers
                        timestamp:[[NSApp currentEvent] timestamp]
                     windowNumber:[window windowNumber]
                          context:[NSGraphicsContext currentContext]
                      eventNumber:0
                       clickCount:click_count
                         pressure:1.0];
  return event;
}

}  // namespace

NPError PluginInstance::PopUpContextMenu(NPMenu* menu) {
  if (!currently_handled_event_)
    return NPERR_GENERIC_ERROR;

  CGRect main_display_bounds = CGDisplayBounds(CGMainDisplayID());
  NSPoint screen_point = NSMakePoint(
      plugin_origin_.x() + currently_handled_event_->data.mouse.pluginX,
      plugin_origin_.y() + currently_handled_event_->data.mouse.pluginY);
  // Plugin offsets are upper-left based, so flip vertically for Cocoa.
  screen_point.y = main_display_bounds.size.height - screen_point.y;

  NSMenu* nsmenu = reinterpret_cast<NSMenu*>(menu);
  NPError return_val = NPERR_NO_ERROR;
  @try {
    [nsmenu popUpMenuPositioningItem:nil atLocation:screen_point inView:nil];
  }
  @catch (NSException* e) {
    NSLog(@"Caught exception while handling PopUpContextMenu: %@", e);
    return_val = NPERR_GENERIC_ERROR;
  }

  return return_val;
}

ScopedCurrentPluginEvent::ScopedCurrentPluginEvent(PluginInstance* instance,
                                                   NPCocoaEvent* event)
    : instance_(instance) {
  instance_->set_currently_handled_event(event);
}

ScopedCurrentPluginEvent::~ScopedCurrentPluginEvent() {
  instance_->set_currently_handled_event(NULL);
}

}  // namespace npapi
}  // namespace webkit