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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
// Copyright (c) 2009 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 "chrome/browser/cocoa/external_protocol_dialog.h"
#include "app/l10n_util_mac.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/sys_string_conversions.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/external_protocol_handler.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
///////////////////////////////////////////////////////////////////////////////
// ExternalProtocolHandler
// static
void ExternalProtocolHandler::RunExternalProtocolDialog(
const GURL& url, int render_process_host_id, int routing_id) {
[[ExternalProtocolDialogController alloc] initWithGURL:&url];
}
///////////////////////////////////////////////////////////////////////////////
// ExternalProtocolDialogController
@interface ExternalProtocolDialogController(Private)
- (void)alertEnded:(NSAlert *)alert
returnCode:(int)returnCode
contextInfo:(void*)contextInfo;
- (string16)appNameForProtocol;
@end
@implementation ExternalProtocolDialogController
- (id)initWithGURL:(const GURL*)url {
DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
url_ = *url;
creation_time_ = base::Time::Now();
string16 appName = [self appNameForProtocol];
if (appName.length() == 0) {
// No registered apps for this protocol; give up and go home.
[self autorelease];
return nil;
}
alert_ = [[NSAlert alloc] init];
[alert_ setMessageText:
l10n_util::GetNSStringWithFixup(IDS_EXTERNAL_PROTOCOL_TITLE)];
NSButton* allowButton = [alert_ addButtonWithTitle:
l10n_util::GetNSStringWithFixup(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT)];
[allowButton setKeyEquivalent:@""]; // disallow as default
[alert_ addButtonWithTitle:
l10n_util::GetNSStringWithFixup(
IDS_EXTERNAL_PROTOCOL_CANCEL_BUTTON_TEXT)];
const int kMaxUrlWithoutSchemeSize = 256;
std::wstring elided_url_without_scheme;
ElideString(ASCIIToWide(url_.possibly_invalid_spec()),
kMaxUrlWithoutSchemeSize, &elided_url_without_scheme);
NSString* urlString = l10n_util::GetNSStringFWithFixup(
IDS_EXTERNAL_PROTOCOL_INFORMATION,
ASCIIToUTF16(url_.scheme() + ":"),
WideToUTF16(elided_url_without_scheme));
NSString* appString = l10n_util::GetNSStringFWithFixup(
IDS_EXTERNAL_PROTOCOL_APPLICATION_TO_LAUNCH,
appName);
NSString* warningString =
l10n_util::GetNSStringWithFixup(IDS_EXTERNAL_PROTOCOL_WARNING);
NSString* informativeText =
[NSString stringWithFormat:@"%@\n\n%@\n\n%@",
urlString,
appString,
warningString];
[alert_ setInformativeText:informativeText];
[alert_ setShowsSuppressionButton:YES];
[[alert_ suppressionButton] setTitle:
l10n_util::GetNSStringWithFixup(IDS_EXTERNAL_PROTOCOL_CHECKBOX_TEXT)];
[alert_ beginSheetModalForWindow:nil // nil here makes it app-modal
modalDelegate:self
didEndSelector:@selector(alertEnded:returnCode:contextInfo:)
contextInfo:nil];
return self;
}
- (void)dealloc {
[alert_ release];
[super dealloc];
}
- (void)alertEnded:(NSAlert *)alert
returnCode:(int)returnCode
contextInfo:(void*)contextInfo {
ExternalProtocolHandler::BlockState blockState =
ExternalProtocolHandler::UNKNOWN;
switch (returnCode) {
case NSAlertFirstButtonReturn:
blockState = ExternalProtocolHandler::DONT_BLOCK;
break;
case NSAlertSecondButtonReturn:
blockState = ExternalProtocolHandler::BLOCK;
break;
default:
NOTREACHED();
}
// Set the "don't warn me again" info.
if ([[alert_ suppressionButton] state] == NSOnState) {
ExternalProtocolHandler::SetBlockState(UTF8ToWide(url_.scheme()),
blockState);
}
if (blockState == ExternalProtocolHandler::DONT_BLOCK) {
UMA_HISTOGRAM_LONG_TIMES("clickjacking.launch_url",
base::Time::Now() - creation_time_);
ExternalProtocolHandler::LaunchUrlWithoutSecurityCheck(url_);
}
[self autorelease];
}
- (string16)appNameForProtocol {
NSURL* url = [NSURL URLWithString:
base::SysUTF8ToNSString(url_.possibly_invalid_spec())];
CFURLRef openingApp = NULL;
OSStatus status = LSGetApplicationForURL((CFURLRef)url,
kLSRolesAll,
NULL,
&openingApp);
if (status != noErr) {
// likely kLSApplicationNotFoundErr
return string16();
}
NSString* appPath = [(NSURL*)openingApp path];
CFRelease(openingApp); // NOT A BUG; LSGetApplicationForURL retains for us
NSString* appDisplayName =
[[NSFileManager defaultManager] displayNameAtPath:appPath];
return base::SysNSStringToUTF16(appDisplayName);
}
@end
|