forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharing_service_picker.mm
114 lines (95 loc) · 3.82 KB
/
sharing_service_picker.mm
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/app_shim_remote_cocoa/sharing_service_picker.h"
#include <utility>
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "third_party/blink/public/mojom/webshare/webshare.mojom.h"
#include "url/gurl.h"
@interface SharingServicePicker
: NSObject <NSSharingServiceDelegate, NSSharingServicePickerDelegate>
// Displays the NSSharingServicePicker which is positioned center and overlaps
// WebContents and the Non Client area.
- (void)show;
@end
@implementation SharingServicePicker {
base::scoped_nsobject<NSSharingServicePicker> picker_;
remote_cocoa::mojom::RenderWidgetHostNSView::ShowSharingServicePickerCallback
callback_;
NSView* view_;
}
- (instancetype)initWithItems:(NSArray*)items
callback:(remote_cocoa::mojom::RenderWidgetHostNSView::
ShowSharingServicePickerCallback)cb
view:(NSView*)view {
if ((self = [super init])) {
picker_.reset([[NSSharingServicePicker alloc] initWithItems:items]);
picker_.get().delegate = self;
callback_ = std::move(cb);
view_ = view;
}
return self;
}
- (void)sharingServicePicker:(NSSharingServicePicker*)sharingServicePicker
didChooseSharingService:(NSSharingService*)service {
// When the NSSharingServicePicker gets invoked but then the picker gets
// dismissed, this is the only delegate method called, and it's called with a
// nil service, so run the callback.
if (!service) {
std::move(callback_).Run(blink::mojom::ShareError::CANCELED);
}
}
- (void)show {
NSRect viewFrame = [view_ frame];
CGSize size = viewFrame.size;
NSRect rect = NSMakeRect(size.width / 2, size.height, 1, 1);
[picker_ showRelativeToRect:rect ofView:view_ preferredEdge:NSMaxXEdge];
}
- (void)sharingService:(NSSharingService*)sharingService
didShareItems:(NSArray*)items {
std::move(callback_).Run(blink::mojom::ShareError::OK);
}
- (void)sharingService:(NSSharingService*)sharingService
didFailToShareItems:(NSArray*)items
error:(NSError*)error {
error.code == NSUserCancelledError
? std::move(callback_).Run(blink::mojom::ShareError::CANCELED)
: std::move(callback_).Run(blink::mojom::ShareError::INTERNAL_ERROR);
}
- (id<NSSharingServiceDelegate>)
sharingServicePicker:(NSSharingServicePicker*)sharingServicePicker
delegateForSharingService:(NSSharingService*)sharingService {
return self;
}
- (NSWindow*)sharingService:(NSSharingService*)sharingService
sourceWindowForShareItems:(NSArray*)items
sharingContentScope:(NSSharingContentScope*)sharingContentScope {
return [view_ window];
}
@end
namespace remote_cocoa {
void ShowSharingServicePickerForView(
NSView* view,
const std::string& title,
const std::string& text,
const std::string& url,
const std::vector<std::string>& file_paths,
mojom::RenderWidgetHostNSView::ShowSharingServicePickerCallback callback) {
NSString* ns_title = base::SysUTF8ToNSString(title);
NSString* ns_url = base::SysUTF8ToNSString(url);
NSString* ns_text = base::SysUTF8ToNSString(text);
NSMutableArray* items =
[NSMutableArray arrayWithArray:@[ ns_title, ns_url, ns_text ]];
for (const auto& file_path : file_paths) {
NSString* ns_file_path = base::SysUTF8ToNSString(file_path);
NSURL* file_url = [NSURL fileURLWithPath:ns_file_path];
[items addObject:file_url];
}
base::scoped_nsobject<SharingServicePicker> picker(
[[SharingServicePicker alloc] initWithItems:items
callback:std::move(callback)
view:view]);
[picker.get() show];
}
} // namespace remote_cocoa