Add id to cast web contents observer
Added a id to replace tab_id as unique
identifier for the webviews that are created from the
platform views service. The tab_id was always 0
because is_root_frame is true on creation.
Subsequent lookups for the ax tree id by tab id
were only working by chance. The linear search for the
tab id among the list of windows always matches the
first one found, which was the correct one. This ensures
tab ids will be unique.
Bug: None
Test: display assistant created webviews followed by navigation
causing render frames to be swapped
Change-Id: Ib6b9f1fd217367423f15965e5b760df1f170caa8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2342640
Commit-Queue: Randy Rossi <[email protected]>
Reviewed-by: Daniel Nicoara <[email protected]>
Cr-Commit-Position: refs/heads/master@{#796903}
diff --git a/chromecast/browser/cast_web_contents.h b/chromecast/browser/cast_web_contents.h
index b921830d..171cddf 100644
--- a/chromecast/browser/cast_web_contents.h
+++ b/chromecast/browser/cast_web_contents.h
@@ -269,6 +269,10 @@
// same tab ID at any given time.
virtual int tab_id() const = 0;
+ // An identifier for the WebContents, mainly used by platform views service.
+ // IDs may be re-used but are unique among all live CastWebContents.
+ virtual int id() const = 0;
+
// TODO(seantopping): Hide this, clients shouldn't use WebContents directly.
virtual content::WebContents* web_contents() const = 0;
virtual PageState page_state() const = 0;
diff --git a/chromecast/browser/cast_web_contents_impl.cc b/chromecast/browser/cast_web_contents_impl.cc
index 00fd4cd..e9288081 100644
--- a/chromecast/browser/cast_web_contents_impl.cc
+++ b/chromecast/browser/cast_web_contents_impl.cc
@@ -53,6 +53,9 @@
// IDs start at 1, since 0 is reserved for the root content window.
size_t next_tab_id = 1;
+// Next id for id()
+size_t next_id = 0;
+
// Remove the given CastWebContents pointer from the global instance vector.
void RemoveCastWebContents(CastWebContents* instance) {
auto& all_cast_web_contents = CastWebContents::GetAll();
@@ -129,6 +132,7 @@
activity_url_filter_(std::move(init_params.url_filters)),
main_process_host_(nullptr),
tab_id_(init_params.is_root_window ? 0 : next_tab_id++),
+ id_(next_id++),
is_websql_enabled_(init_params.enable_websql),
is_mixer_audio_enabled_(init_params.enable_mixer_audio),
main_frame_loaded_(false),
@@ -185,6 +189,10 @@
return tab_id_;
}
+int CastWebContentsImpl::id() const {
+ return id_;
+}
+
content::WebContents* CastWebContentsImpl::web_contents() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return web_contents_;
diff --git a/chromecast/browser/cast_web_contents_impl.h b/chromecast/browser/cast_web_contents_impl.h
index 696f53c..247872f2 100644
--- a/chromecast/browser/cast_web_contents_impl.h
+++ b/chromecast/browser/cast_web_contents_impl.h
@@ -56,6 +56,7 @@
// CastWebContents implementation:
int tab_id() const override;
+ int id() const override;
void AddRendererFeatures(std::vector<RendererFeature> features) override;
void AllowWebAndMojoWebUiBindings() override;
void ClearRenderWidgetHostView() override;
@@ -166,6 +167,7 @@
std::vector<RendererFeature> renderer_features_;
const int tab_id_;
+ const int id_;
bool is_websql_enabled_;
bool is_mixer_audio_enabled_;
base::TimeTicks start_loading_ticks_;
diff --git a/chromecast/browser/test/mock_cast_web_view.h b/chromecast/browser/test/mock_cast_web_view.h
index c3df3b2..e1cc121 100644
--- a/chromecast/browser/test/mock_cast_web_view.h
+++ b/chromecast/browser/test/mock_cast_web_view.h
@@ -17,6 +17,7 @@
// CastWebContents implementation
MOCK_METHOD(int, tab_id, (), (const, override));
+ MOCK_METHOD(int, id, (), (const, override));
MOCK_METHOD(content::WebContents*, web_contents, (), (const, override));
MOCK_METHOD(PageState, page_state, (), (const, override));
MOCK_METHOD(QueryableDataHost*, queryable_data_host, (), (const, override));
diff --git a/chromecast/browser/webview/webview_controller.cc b/chromecast/browser/webview/webview_controller.cc
index 2964975..aea2d5b1 100644
--- a/chromecast/browser/webview/webview_controller.cc
+++ b/chromecast/browser/webview/webview_controller.cc
@@ -76,12 +76,12 @@
std::unique_ptr<webview::WebviewResponse> response =
std::make_unique<webview::WebviewResponse>();
- // For webviews, set the ax_id to be the cast_web_contents' tab id rather than
- // the ax tree id for the main frame. The main frame can be replaced after
- // we've set this from navigation. Prefix the string with "T:" to tell the ax
- // bridge to find the cast_web_contents by tab id. Then it can find the
- // current ax tree id from that.
- std::string ax_id = "T:" + base::NumberToString(cast_web_contents_->tab_id());
+ // For webviews, set the ax_id to be the cast_web_contents' id
+ // rather than the ax tree id for the main frame. The main frame can be
+ // replaced after we've set this from navigation. Prefix the string with
+ // "T:" to tell the ax bridge to find the cast_web_contents by id.
+ // Then it can find the current ax tree id from that.
+ std::string ax_id = "T:" + base::NumberToString(cast_web_contents_->id());
response->mutable_create_response()
->mutable_accessibility_info()
->set_ax_tree_id(ax_id);