summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/file-system-access/resources/message-target.js
blob: 191b4748ab5fc5c0e0416d1e96c476262c6cfe0d (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
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
154
155
156
157
'use strict';

// This script depends on the following scripts:
//    /file-system-access/resources/messaging-helpers.js
//    /file-system-access/resources/test-helpers.js

// add_message_event_handlers() is the helper function used to setup all
// message targets, including iframes and workers.
//
// Adds a message event handler and a message error handler to |receiver|.
// The 'data' property from received MessageEvents must include a 'type'
// property. The 'type' selects the test logic to run. Most message type
// handlers use postMessage() to respond to the sender with test results.
// The sender then validates the test results after receiving the response.
//
// Both |target| and |target_origin| are optional. |target| is used
// to send message responses back to the sender. When omitted, the
// 'source' from received MessageEvents is used instead.
//
// For window messaging, |target_origin| specifies the origin to receive
// responses. Most window tests use '*' for the |target_origin|. Worker
// and message port tests must use undefined for |target_origin| to avoid
// exceptions.
function add_message_event_handlers(receiver, target, target_origin) {
  receiver.addEventListener('message', async function (message_event) {
    const message_data = message_event.data;

    // Reply to the sender using the 'source' from the received MessageEvent.
    let message_source = message_event.source;
    if (message_source === null) {
      // However, some message senders, like DedicatedWorkers, don't include
      // a source. Fallback to the target when the source is null.
      message_source = target;
    }

    try {
      switch (message_data.type) {
        case 'receive-message-port':
          // Receive a MessagePort to use as a message target for testing.
          add_message_event_handlers(
            /*receiver=*/message_data.message_port,
            /*target=*/message_data.message_port);
          message_data.message_port.start();
          break;

        case 'create-broadcast-channel':
          // Create a BroadcastChannel to use as a message target for testing.
          const broadcast_channel =
            new BroadcastChannel(message_data.broadcast_channel_name);
          add_message_event_handlers(
            /*receiver=*/broadcast_channel,
            /*target=*/broadcast_channel);
          message_source.postMessage(
            { type: 'broadcast-channel-created' },
            { targetOrigin: target_origin });
          break;

        case 'receive-file-system-handles':
          // Receive a list of cloned FileSystemFileHandles. Access the
          // properties of each FileSystemFileHandle by serializing the
          // handle to a JavaScript object. Then respond with the serialized
          // results, enabling the sender to verify that the cloned handle
          // produced the expected property values from this execution context.
          const serialized_handles = [];
          const cloned_handles = message_data.cloned_handles;
          for (let i = 0; i < cloned_handles.length; ++i) {
            const serialized = await serialize_handle(cloned_handles[i]);
            serialized_handles.push(serialized);
          }
          message_source.postMessage({
            type: 'receive-serialized-file-system-handles',
            serialized_handles,
            // Respond with the cloned handles to create new clones for
            // the sender to verify.
            cloned_handles,
          }, { targetOrigin: target_origin });
          break;

        case 'receive-serialized-file-system-handles':
          // Do nothing. This message is meant for test runner validation.
          // Other message targets may receive this message while testing
          // broadcast channels.
          break;

        case 'create-file':
          // Create a new file and then respond to the sender with it.
          const directory = await navigator.storage.getDirectory();
          const file_handle =
            await directory.getFileHandle('temp-file', { create: true });
          message_source.postMessage(
            { type: 'receive-file', file_handle },
            { targetOrigin: target_origin });
          break;

        case 'create-directory':
          // Create a new directory and then respond to the sender with it.
          const parent_directory = await navigator.storage.getDirectory();
          const directory_handle =
            await parent_directory.getDirectoryHandle('temp-directory',
              { create: true });
          message_source.postMessage(
            { type: 'receive-directory', directory_handle },
            { targetOrigin: target_origin });
          break;

        case 'create-sync-access-handle':
          // Receive a file and create a sync access handle out of it. Report
          // success to the sender.
          let success = true;
          try {
            const access_handle = await message_data.file_handle.createSyncAccessHandle();
            access_handle.close();
          } catch (error) {
            success = false;
          }

          message_source.postMessage(
            { type: 'receive-sync-access-handle-result', success },
            { targetOrigin: target_origin });
          break;

        default:
          throw `Unknown message type: '${message_data.type}'`;
      }
    } catch (error) {
      // Respond with an error to trigger a failure in the sender's
      // test runner.
      message_source.postMessage(`ERROR: ${error}`,
        { targetOrigin: target_origin });
    }
  });

  receiver.addEventListener('messageerror', async function (message_event) {
    // Select the target for message responses (see comment in 'message' event
    // listener above).
    let message_source = message_event.source;
    if (message_source === null) {
      message_source = target;
    }

    try {
      // Respond with the MessageEvent's property values, enabling the sender
      // to verify results.
      const serialized_message_error_event =
        serialize_message_error_event(message_event);
      message_source.postMessage({
        type: 'serialized-message-error',
        serialized_message_error_event
      }, { targetOrigin: target_origin });
    } catch (error) {
      // Respond with an error to trigger a failure in the sender's
      // test runner.
      message_source.postMessage(`ERROR: ${error}`,
        { targetOrigin: target_origin });
    }
  });
}