[contextual_tasks] Underline tabs when associtation changes

This CL makes ActiveTaskContextProvider to observe
1. tab association and dissociation events to correctly
underline associated tabs.
2. task removal events.
After this CL, link clicks from the associated tabs will underline
right away.

Bug: 467783557
Change-Id: I243818acc1e06c987a9995fb46be0bbda8a1303a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7252665
Reviewed-by: Tommy Nyquist <nyquist@chromium.org>
Commit-Queue: Shakti Sahu <shaktisahu@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1558265}
diff --git a/chrome/browser/contextual_tasks/active_task_context_provider_impl.cc b/chrome/browser/contextual_tasks/active_task_context_provider_impl.cc
index d50e286..467ee1e 100644
--- a/chrome/browser/contextual_tasks/active_task_context_provider_impl.cc
+++ b/chrome/browser/contextual_tasks/active_task_context_provider_impl.cc
@@ -110,6 +110,38 @@
   RefreshContext();
 }
 
+void ActiveTaskContextProviderImpl::OnTaskRemoved(
+    const base::Uuid& task_id,
+    ContextualTasksService::TriggerSource source) {
+  if (active_task_id_ == task_id) {
+    // The task that was last shown was just removed. Refresh the tabs.
+    active_task_id_ = std::nullopt;
+    RefreshContext();
+  }
+}
+
+void ActiveTaskContextProviderImpl::OnTaskAssociatedToTab(
+    const base::Uuid& task_id,
+    SessionID tab_id) {
+  // Ignore the event if it is not for the task in the active tab.
+  if (!active_task_id_ || active_task_id_ != task_id) {
+    return;
+  }
+
+  RefreshContext();
+}
+
+void ActiveTaskContextProviderImpl::OnTaskDisassociatedFromTab(
+    const base::Uuid& task_id,
+    SessionID tab_id) {
+  // Ignore the event if it is not for the task in the active tab.
+  if (!active_task_id_ || active_task_id_ != task_id) {
+    return;
+  }
+
+  RefreshContext();
+}
+
 void ActiveTaskContextProviderImpl::RefreshContext() {
   // Increment the callback ID to invalidate any outstanding callbacks.
   callback_id_++;
diff --git a/chrome/browser/contextual_tasks/active_task_context_provider_impl.h b/chrome/browser/contextual_tasks/active_task_context_provider_impl.h
index 935ddfc..1d0e0068 100644
--- a/chrome/browser/contextual_tasks/active_task_context_provider_impl.h
+++ b/chrome/browser/contextual_tasks/active_task_context_provider_impl.h
@@ -49,6 +49,12 @@
   // ContextualTasksService::Observer implementation.
   void OnTaskUpdated(const ContextualTask& task,
                      ContextualTasksService::TriggerSource source) override;
+  void OnTaskRemoved(const base::Uuid& task_id,
+                     ContextualTasksService::TriggerSource source) override;
+  void OnTaskAssociatedToTab(const base::Uuid& task_id,
+                             SessionID tab_id) override;
+  void OnTaskDisassociatedFromTab(const base::Uuid& task_id,
+                                  SessionID tab_id) override;
 
  private:
   // Determines the active task and triggers a context fetch.