 +      helper.on(this._targetRegistry, TargetRegistry.Events.TargetCreated, this._onTargetCreated.bind(this)),
 +      helper.on(this._targetRegistry, TargetRegistry.Events.TargetChanged, this._onTargetChanged.bind(this)),
 +      helper.on(this._targetRegistry, TargetRegistry.Events.TargetDestroyed, this._onTargetDestroyed.bind(this)),
++      helper.on(this._targetRegistry, TargetRegistry.Events.PageTargetReady, this._onPageTargetReady.bind(this)),
 +    ];
 +  }
 +
 +  dispose() {
 +    helper.removeListeners(this._eventListeners);
-+    for (const browserContextId of this._browserContextsToDispose)
-+      this._contextManager.removeBrowserContext(browserContextId);
-+    this._browserContextsToDispose.clear();
++    for (const [browserContextId, options] of this._createdBrowserContextOptions) {
++      if (options.removeOnDetach)
++        this._contextManager.removeBrowserContext(browserContextId);
++    }
++    this._createdBrowserContextOptions.clear();
 +  }
 +
 +  _onTargetCreated(targetInfo) {

+        defaultValue: dialog.defaultValue(),
+      });
+    }
+  }
+
+  async setUserAgent(options) {
+    return await this._contentSession.send('Page.setUserAgent', options);
+  }
+
+  async setFileInputFiles(options) {

+    this._contextManager = BrowserContextManager.instance();
+    this._targetRegistry = TargetRegistry.instance();
+    this._enabled = false;
+    this._eventListeners = [];
+    this._createdBrowserContextOptions = new Map();
+  }
+
+  async attachToTarget({targetId}) {
+    if (!this._enabled)
+      throw new Error('Target domain is not enabled');
+    const sessionId = this._session.dispatcher().createSession(targetId);
+    return {sessionId};
+  }
+
+  async createBrowserContext(options) {
+    if (!this._enabled)
+      throw new Error('Target domain is not enabled');
+    const browserContextId = this._contextManager.createBrowserContext();
+    // TODO: introduce BrowserContext class, with options?
+    this._createdBrowserContextOptions.set(browserContextId, options);
+    return {browserContextId};
+  }
+
+  async removeBrowserContext({browserContextId}) {
+    if (!this._enabled)
+      throw new Error('Target domain is not enabled');
+    this._createdBrowserContextOptions.delete(browserContextId);
+    this._contextManager.removeBrowserContext(browserContextId);
+  }
+
