1/*
2 * Copyright (C) 2005 Apple Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#if !PLATFORM(IOS)
30
31#import "WebDefaultContextMenuDelegate.h"
32
33#import "WebDOMOperations.h"
34#import "WebDataSourcePrivate.h"
35#import "WebDefaultUIDelegate.h"
36#import "WebFrameInternal.h"
37#import "WebFrameView.h"
38#import "WebHTMLViewPrivate.h"
39#import "WebLocalizableStringsInternal.h"
40#import "WebNSPasteboardExtras.h"
41#import "WebNSURLRequestExtras.h"
42#import "WebPolicyDelegate.h"
43#import "WebUIDelegate.h"
44#import "WebUIDelegatePrivate.h"
45#import "WebViewInternal.h"
46#import <Foundation/NSURLConnection.h>
47#import <Foundation/NSURLRequest.h>
48#import <WebCore/Editor.h>
49#import <WebCore/Frame.h>
50#import <WebCore/FrameLoader.h>
51#import <WebKitLegacy/DOM.h>
52#import <WebKitLegacy/DOMPrivate.h>
53#import <WebKitSystemInterface.h>
54#import <wtf/Assertions.h>
55
56@implementation WebDefaultUIDelegate (WebContextMenu)
57
58- (NSMenuItem *)menuItemWithTag:(int)tag target:(id)target representedObject:(id)representedObject
59{
60    NSMenuItem *menuItem = [[[NSMenuItem alloc] init] autorelease];
61    [menuItem setTag:tag];
62    [menuItem setTarget:target]; // can be nil
63    [menuItem setRepresentedObject:representedObject];
64
65    NSString *title = nil;
66    SEL action = NULL;
67
68    switch(tag) {
69        case WebMenuItemTagCopy:
70            title = UI_STRING_INTERNAL("Copy", "Copy context menu item");
71            action = @selector(copy:);
72            break;
73        case WebMenuItemTagGoBack:
74            title = UI_STRING_INTERNAL("Back", "Back context menu item");
75            action = @selector(goBack:);
76            break;
77        case WebMenuItemTagGoForward:
78            title = UI_STRING_INTERNAL("Forward", "Forward context menu item");
79            action = @selector(goForward:);
80            break;
81        case WebMenuItemTagStop:
82            title = UI_STRING_INTERNAL("Stop", "Stop context menu item");
83            action = @selector(stopLoading:);
84            break;
85        case WebMenuItemTagReload:
86            title = UI_STRING_INTERNAL("Reload", "Reload context menu item");
87            action = @selector(reload:);
88            break;
89        case WebMenuItemTagSearchInSpotlight:
90            title = UI_STRING_INTERNAL("Search in Spotlight", "Search in Spotlight context menu item");
91            action = @selector(_searchWithSpotlightFromMenu:);
92            break;
93        case WebMenuItemTagSearchWeb: {
94            RetainPtr<CFStringRef> searchProviderName = adoptCF(WKCopyDefaultSearchProviderDisplayName());
95            title = [NSString stringWithFormat:UI_STRING_INTERNAL("Search with %@", "Search with search provider context menu item with provider name inserted"), searchProviderName.get()];
96            action = @selector(_searchWithGoogleFromMenu:);
97            break;
98        }
99        case WebMenuItemTagLookUpInDictionary:
100            title = UI_STRING_INTERNAL("Look Up in Dictionary", "Look Up in Dictionary context menu item");
101            action = @selector(_lookUpInDictionaryFromMenu:);
102            break;
103        case WebMenuItemTagOpenFrameInNewWindow:
104            title = UI_STRING_INTERNAL("Open Frame in New Window", "Open Frame in New Window context menu item");
105            action = @selector(_openFrameInNewWindowFromMenu:);
106            break;
107        default:
108            ASSERT_NOT_REACHED();
109            return nil;
110    }
111
112    if (title)
113        [menuItem setTitle:title];
114
115    [menuItem setAction:action];
116
117    return menuItem;
118}
119
120- (void)appendDefaultItems:(NSArray *)defaultItems toArray:(NSMutableArray *)menuItems
121{
122    ASSERT_ARG(menuItems, menuItems != nil);
123    if ([defaultItems count] > 0) {
124        ASSERT(![[menuItems lastObject] isSeparatorItem]);
125        if (![[defaultItems objectAtIndex:0] isSeparatorItem]) {
126            [menuItems addObject:[NSMenuItem separatorItem]];
127
128            NSEnumerator *e = [defaultItems objectEnumerator];
129            NSMenuItem *item;
130            while ((item = [e nextObject]) != nil) {
131                [menuItems addObject:item];
132            }
133        }
134    }
135}
136
137- (NSArray *)webView:(WebView *)wv contextMenuItemsForElement:(NSDictionary *)element  defaultMenuItems:(NSArray *)defaultMenuItems
138{
139    // The defaultMenuItems here are ones supplied by the WebDocumentView protocol implementation. WebPDFView is
140    // one case that has non-nil default items here.
141    NSMutableArray *menuItems = [NSMutableArray array];
142
143    WebFrame *webFrame = [element objectForKey:WebElementFrameKey];
144
145    if ([[element objectForKey:WebElementIsSelectedKey] boolValue]) {
146        // The Spotlight and Google items are implemented in WebView, and require that the
147        // current document view conforms to WebDocumentText
148        ASSERT([[[webFrame frameView] documentView] conformsToProtocol:@protocol(WebDocumentText)]);
149
150        // FIXME 4184640: The Look Up in Dictionary item is only implemented in WebHTMLView, and so is present but
151        // dimmed for other cases where WebElementIsSelectedKey is present. It would probably
152        // be better not to include it in the menu if the documentView isn't a WebHTMLView, but that could break
153        // existing clients that have code that relies on it being present (unlikely for clients outside of Apple,
154        // but Safari has such code).
155
156        NSMenuItem *lookupMenuItem = [self menuItemWithTag:WebMenuItemTagLookUpInDictionary target:nil representedObject:element];
157        NSString *selectedString = [(id <WebDocumentText>)[[webFrame frameView] documentView] selectedString];
158        [lookupMenuItem setTitle:[NSString stringWithFormat:UI_STRING_INTERNAL("Look Up “%@”", "Look Up context menu item with selected word"), selectedString]];
159        [menuItems addObject:lookupMenuItem];
160
161        [menuItems addObject:[self menuItemWithTag:WebMenuItemTagSearchWeb target:nil representedObject:element]];
162
163        [menuItems addObject:[NSMenuItem separatorItem]];
164        [menuItems addObject:[self menuItemWithTag:WebMenuItemTagCopy target:nil representedObject:element]];
165    } else {
166        WebView *wv = [webFrame webView];
167        if ([wv canGoBack]) {
168            [menuItems addObject:[self menuItemWithTag:WebMenuItemTagGoBack target:wv representedObject:element]];
169        }
170        if ([wv canGoForward]) {
171            [menuItems addObject:[self menuItemWithTag:WebMenuItemTagGoForward target:wv representedObject:element]];
172        }
173        if ([wv isLoading]) {
174            [menuItems addObject:[self menuItemWithTag:WebMenuItemTagStop target:wv representedObject:element]];
175        } else {
176            [menuItems addObject:[self menuItemWithTag:WebMenuItemTagReload target:wv representedObject:element]];
177        }
178
179        if (webFrame != [wv mainFrame]) {
180            [menuItems addObject:[self menuItemWithTag:WebMenuItemTagOpenFrameInNewWindow target:wv representedObject:element]];
181        }
182    }
183
184    // Add the default items at the end, if any, after a separator
185    [self appendDefaultItems:defaultMenuItems toArray:menuItems];
186
187    return menuItems;
188}
189
190@end
191
192#endif // !PLATFORM(IOS)
193