|
1 | 1 | # Web Share Target API Interface
|
2 | 2 |
|
3 |
| -**Date**: 2017-01-12 |
4 |
| - |
5 |
| -This document is a rough spec (i.e., *not* a formal web standard draft) of the |
6 |
| -Web Share Target API. This API allows websites to register to receive shared |
7 |
| -content from either the [Web Share API](https://github.com/mgiuca/web-share), or |
8 |
| -system events (e.g., shares from native apps). |
9 |
| - |
10 |
| -This API has 2 proposed designs: both require the user agent to support [web |
11 |
| -app manifests](https://www.w3.org/TR/appmanifest/), while the second requires |
12 |
| -support for [service workers](https://www.w3.org/TR/service-workers/). We are |
13 |
| -planning to explore both approaches to determine which is more acceptable. The |
14 |
| -[Web Share API](https://github.com/mgiuca/web-share) is not required, but |
15 |
| -recommended. |
16 |
| - |
17 |
| -Examples of using the Share Target API for sharing can be seen in the |
18 |
| -[explainer document](explainer.md). |
19 |
| - |
20 |
| -**Note**: The Web Share Target API is a proposal of the [Ballista |
21 |
| -project](https://github.com/chromium/ballista), which aims to explore |
22 |
| -website-to-website and website-to-native interoperability. |
23 |
| - |
24 |
| -## App manifest |
25 |
| - |
26 |
| -The first thing a handler needs to do is declare its share handling capabilities |
27 |
| -in its [web app manifest](https://www.w3.org/TR/appmanifest/): |
28 |
| - |
29 |
| -### Approach 1: |
30 |
| - |
31 |
| -We expect apps that are sharing data to share one or more fields, including |
32 |
| -title, text, and URL. These fields can be passed to the target app as query |
33 |
| -parameters, and each is optional. If a web app can handle a share, they should |
34 |
| -include (in the manifest) a template URL (relative to the manifest URL) that the |
35 |
| -shared data can be inserted into. |
36 |
| - |
37 |
| -```WebIDL |
38 |
| -dictionary ShareTarget { |
39 |
| - DOMString url_template; |
40 |
| -}; |
41 |
| -
|
42 |
| -partial dictionary Manifest { |
43 |
| - ShareTarget share_target; |
44 |
| -}; |
45 |
| -``` |
46 |
| - |
47 |
| -The `url_template` member will contain placeholders for each field of the form |
48 |
| -"{field}". Each placeholder in the template will be replaced with the value of |
49 |
| -the corresponding field, that has been shared by the source app. If a given |
50 |
| -field was not shared, its placeholder will be replaced with an empty string. |
51 |
| -An example url template is here: |
52 |
| - |
53 |
| -```json |
54 |
| -"share_target": { |
55 |
| - "url_template": "/share?title={title}&text={text}&url={url}" |
56 |
| -} |
57 |
| -``` |
58 |
| - |
59 |
| -The `url_template` member also allows the target web app to specify which |
60 |
| -attributes of the shared data it cares about. e.g. for Web Share, the passed |
61 |
| -data includes title, text, and URL, but the receiving web app may only care |
62 |
| -about message and URL. |
63 |
| - |
64 |
| -### Approach 2: |
65 |
| - |
66 |
| -```WebIDL |
67 |
| -partial dictionary Manifest { |
68 |
| - boolean supports_share; |
69 |
| -}; |
70 |
| -``` |
71 |
| -The `"supports_share"` member of the manifest, if `true`, indicates that the app |
72 |
| -can receive share events from requesters, or the system. |
73 |
| - |
74 |
| -### Things to note |
75 |
| - |
76 |
| -The declarative nature of the manifest allows search services to index and |
77 |
| -present web applications that handle shares. |
78 |
| - |
79 |
| -Handlers declaring `supports_share` or `url_template` in their manifest |
80 |
| -will **not** be automatically registered; the user must explicitly authorize |
81 |
| -the registration. How this takes place is still under consideration (see [User |
82 |
| -Flow](explainer.md#user-flow), but will ultimately be at the discretion of the |
83 |
| -user agent (the user may be automatically prompted, or may have to explicitly |
84 |
| -request registration). |
85 |
| - |
86 |
| -**For consideration**: We may wish to provide a method for websites to |
87 |
| -explicitly request to prompt the user for handler registration. There would |
88 |
| -still be a requirement to declare `supports_share` in the manifest. For now, |
89 |
| -we have omitted such a method from the design to keep control in the hands of |
90 |
| -user agents. It is easier to add such a method later than remove it. |
91 |
| - |
92 |
| -## Handling incoming shares |
93 |
| - |
94 |
| -### Approach 1 |
95 |
| - |
96 |
| -Recall the URL template from "Approach 1": |
97 |
| - |
98 |
| -` |
99 |
| -/share?title={title}&text={text}&url={url} |
100 |
| -` |
101 |
| - |
102 |
| -This will be filled with the share data, and opened by the browser, when the |
103 |
| -user selects the target app. |
104 |
| - |
105 |
| -For example, if a source app shares the data: |
106 |
| - |
107 |
| -```JSON |
108 |
| -{ |
109 |
| - "title": "Web Share Target API", |
110 |
| - "text": "An API that allows web apps to receive shared data", |
111 |
| - "url": "https://github.com/WICG/web-share-target" |
112 |
| -} |
113 |
| -``` |
114 |
| - |
115 |
| -The browser will then launch the picker UI, and the user picks some target |
116 |
| -app. If the target app is www.example.com, the browser will launch the |
117 |
| -following URL in a new window or tab: |
118 |
| - |
119 |
| -` |
120 |
| -https://www.example.com/share?title=Web%20Share%20Target%20API&text=An%20API%20that%20allows%20web%20apps%20to%20receive%20shared%20data&url=https://github.com/WICG/web-share-target |
121 |
| -` |
122 |
| - |
123 |
| -Thus, the receiving web app should handle the shared data as desired, at that |
124 |
| -URL. |
125 |
| - |
126 |
| -### Approach 2 |
127 |
| - |
128 |
| -Handlers **must** have a registered [service |
129 |
| -worker](https://www.w3.org/TR/service-workers/). |
130 |
| - |
131 |
| -When the user picks a registered web app as the target of a share, the |
132 |
| -handler's service worker starts up (if it is not already running), and a |
133 |
| -`"share"` event is fired at the global object. |
134 |
| - |
135 |
| -```WebIDL |
136 |
| -partial interface WorkerGlobalScope { |
137 |
| - attribute EventHandler onshare; |
138 |
| -}; |
139 |
| -
|
140 |
| -interface ShareEvent : ExtendableEvent { |
141 |
| - readonly attribute ShareData data; |
142 |
| -
|
143 |
| - void reject(DOMException error); |
144 |
| -}; |
145 |
| -
|
146 |
| -dictionary ShareData { |
147 |
| - DOMString? title; |
148 |
| - DOMString? text; |
149 |
| - DOMString? url; |
150 |
| -}; |
151 |
| -``` |
152 |
| - |
153 |
| -The `onshare` handler (with corresponding event type `"share"`) takes a |
154 |
| -`ShareEvent`. The `data` field provides data from the sending application. |
155 |
| - |
156 |
| -How the handler deals with the data object is at the handler's discretion, and |
157 |
| -will generally depend on the type of app. Here are some suggestions: |
158 |
| - |
159 |
| -* An email client might draft a new email, using `title` as the subject of an |
160 |
| - email, with `text` and `url` concatenated together as the body. |
161 |
| -* A social networking app might draft a new post, ignoring `title`, using `text` |
162 |
| - as the body of the message and adding `url` as a link. If `text` is missing, |
163 |
| - it might use `url` in the body as well. If `url` is missing, it might scan |
164 |
| - `text` looking for a URL and add that as a link. |
165 |
| -* A text messaging app might draft a new message, ignoring `title` and using |
166 |
| - `text` and `url` concatenated together. It might truncate the text or replace |
167 |
| - `url` with a short link to fit into the message size. |
168 |
| - |
169 |
| -A share fails if: |
170 |
| - |
171 |
| -* The handler had no registered service worker. |
172 |
| -* There was an error during service worker initialization. |
173 |
| -* There is no event handler for `share` events. |
174 |
| -* The event handler explicitly calls the event's `reject` method (either in the |
175 |
| - event handler, or in the promise passed to the event's |
176 |
| - [`waitUntil`](https://www.w3.org/TR/service-workers/#wait-until-method) |
177 |
| - method). |
178 |
| -* The promise passed to `waitUntil` is rejected. |
179 |
| - |
180 |
| -Once the event completes without failing, the share automatically succeeds, and |
181 |
| -the requester's promise is resolved. The end of the event's lifetime marks the |
182 |
| -end of the share, and there is no further communication in either direction. |
183 |
| - |
184 |
| -The Share Target API is defined entirely within the service worker. If the |
185 |
| -handler needs to provide UI (which should be the common case), the service |
186 |
| -worker must create a foreground page and send the appropriate data between the |
187 |
| -worker and foreground page, out of band. The `share` event handler is [allowed |
188 |
| -to show a |
189 |
| -popup](https://html.spec.whatwg.org/multipage/browsers.html#allowed-to-show-a-popup), |
190 |
| -which means it can call the |
191 |
| -[`clients.openWindow`](https://www.w3.org/TR/service-workers/#clients-openwindow-method) |
192 |
| -method. |
193 |
| - |
194 |
| -## Where do shares come from? |
195 |
| - |
196 |
| -Share events can be sent from a variety of places: |
197 |
| - |
198 |
| -* Built-in trigger (e.g., user picks "Share" from a browser's menu, to share the |
199 |
| - URL in the address bar). |
200 |
| -* A native application. |
201 |
| -* A web application using the [Web Share |
202 |
| - API](https://github.com/mgiuca/web-share). |
203 |
| - |
204 |
| -There will usually be a picker that lets the user select a target app. This |
205 |
| -could be the native system app picker, or a user-agent-supplied picker. The apps |
206 |
| -could include other system apps and actions alongside the web app handlers. |
207 |
| - |
208 |
| -If an event comes from a web app, the `data` field of the event should be a |
209 |
| -clone of the `data` parameter to `navigator.share`. If the event comes from some |
210 |
| -other source, the user agent may construct the `data` object as appropriate. |
| 3 | +See [Specification](https://wicg.github.io/web-share-target/). |
0 commit comments