Commit 1a42f160 authored by David Black's avatar David Black Committed by Commit Bot

Handle back button behavior.

Previously, the back button immediately navigated backwards in the
Assistant container's UI state.

Now, the back button will navigate backwards in the WebContents history
stack. Only when there is no more history do we return control to the
Assistant container for handling.

See bug for demo.

Bug: b:112355878
Change-Id: I943407c85497f09a5360f21353301b6bc6823315
Reviewed-on: https://chromium-review.googlesource.com/1167646
Commit-Queue: David Black <dmblack@google.com>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#582736}
parent 12e51880
...@@ -144,6 +144,12 @@ void AssistantController::ReleaseWebContents( ...@@ -144,6 +144,12 @@ void AssistantController::ReleaseWebContents(
web_contents_manager_->ReleaseAllWebContents(id_tokens); web_contents_manager_->ReleaseAllWebContents(id_tokens);
} }
void AssistantController::NavigateWebContentsBack(
const base::UnguessableToken& id_token,
mojom::WebContentsManager::NavigateWebContentsBackCallback callback) {
web_contents_manager_->NavigateWebContentsBack(id_token, std::move(callback));
}
void AssistantController::DownloadImage( void AssistantController::DownloadImage(
const GURL& url, const GURL& url,
mojom::AssistantImageDownloader::DownloadCallback callback) { mojom::AssistantImageDownloader::DownloadCallback callback) {
......
...@@ -73,6 +73,13 @@ class ASH_EXPORT AssistantController ...@@ -73,6 +73,13 @@ class ASH_EXPORT AssistantController
// |id_token_list|. // |id_token_list|.
void ReleaseWebContents(const std::vector<base::UnguessableToken>& id_tokens); void ReleaseWebContents(const std::vector<base::UnguessableToken>& id_tokens);
// Navigates the WebContents uniquely identified by |id_token| back relative
// to the current history entry. The supplied |callback| will run specifying
// true if navigation occurred, false otherwise.
void NavigateWebContentsBack(
const base::UnguessableToken& id_token,
mojom::WebContentsManager::NavigateWebContentsBackCallback callback);
// Downloads the image found at the specified |url|. On completion, the // Downloads the image found at the specified |url|. On completion, the
// supplied |callback| will be run with the downloaded image. If the download // supplied |callback| will be run with the downloaded image. If the download
// attempt is unsuccessful, a NULL image is returned. // attempt is unsuccessful, a NULL image is returned.
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "ash/assistant/assistant_controller.h" #include "ash/assistant/assistant_controller.h"
#include "ash/assistant/assistant_ui_controller.h" #include "ash/assistant/assistant_ui_controller.h"
#include "ash/assistant/ui/assistant_ui_constants.h" #include "ash/assistant/ui/assistant_ui_constants.h"
#include "ash/assistant/ui/caption_bar.h"
#include "ash/assistant/util/deep_link_util.h" #include "ash/assistant/util/deep_link_util.h"
#include "ash/public/cpp/app_list/answer_card_contents_registry.h" #include "ash/public/cpp/app_list/answer_card_contents_registry.h"
#include "ash/public/interfaces/web_contents_manager.mojom.h" #include "ash/public/interfaces/web_contents_manager.mojom.h"
...@@ -54,11 +53,36 @@ void AssistantWebView::InitLayout() { ...@@ -54,11 +53,36 @@ void AssistantWebView::InitLayout() {
// Caption bar. // Caption bar.
caption_bar_ = new CaptionBar(); caption_bar_ = new CaptionBar();
caption_bar_->set_delegate(assistant_controller_->ui_controller()); caption_bar_->set_delegate(this);
caption_bar_->SetButtonVisible(CaptionButtonId::kMinimize, false); caption_bar_->SetButtonVisible(CaptionButtonId::kMinimize, false);
AddChildView(caption_bar_); AddChildView(caption_bar_);
} }
bool AssistantWebView::OnCaptionButtonPressed(CaptionButtonId id) {
CaptionBarDelegate* delegate = assistant_controller_->ui_controller();
// We need special handling of the back button. When possible, the back button
// should navigate backwards in the managed WebContents' history stack.
if (id == CaptionButtonId::kBack && web_contents_id_token_.has_value()) {
assistant_controller_->NavigateWebContentsBack(
web_contents_id_token_.value(),
base::BindOnce(
[](CaptionBarDelegate* delegate, bool navigated) {
// If the WebContents' did not navigate it is because we are
// already at the first entry in the history stack and we cannot
// navigate back any further. In this case, we give back control
// to our primary caption button delegate.
if (!navigated)
delegate->OnCaptionButtonPressed(CaptionButtonId::kBack);
},
base::Unretained(delegate)));
return true;
}
// For all other buttons we defer to our primary caption button delegate.
return delegate->OnCaptionButtonPressed(id);
}
void AssistantWebView::OnDeepLinkReceived( void AssistantWebView::OnDeepLinkReceived(
assistant::util::DeepLinkType type, assistant::util::DeepLinkType type,
const std::map<std::string, std::string>& params) { const std::map<std::string, std::string>& params) {
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <memory> #include <memory>
#include "ash/assistant/assistant_controller_observer.h" #include "ash/assistant/assistant_controller_observer.h"
#include "ash/assistant/ui/caption_bar.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/optional.h" #include "base/optional.h"
#include "ui/views/view.h" #include "ui/views/view.h"
...@@ -19,14 +20,14 @@ class UnguessableToken; ...@@ -19,14 +20,14 @@ class UnguessableToken;
namespace ash { namespace ash {
class AssistantController; class AssistantController;
class CaptionBar;
// AssistantWebView is a child of AssistantBubbleView which allows Assistant UI // AssistantWebView is a child of AssistantBubbleView which allows Assistant UI
// to render remotely hosted content within its bubble. It provides a CaptionBar // to render remotely hosted content within its bubble. It provides a CaptionBar
// for window level controls and a WebView/RemoteViewHost for embedding web // for window level controls and a WebView/RemoteViewHost for embedding web
// contents. // contents.
class AssistantWebView : public views::View, class AssistantWebView : public views::View,
public AssistantControllerObserver { public AssistantControllerObserver,
public CaptionBarDelegate {
public: public:
explicit AssistantWebView(AssistantController* assistant_controller); explicit AssistantWebView(AssistantController* assistant_controller);
~AssistantWebView() override; ~AssistantWebView() override;
...@@ -36,6 +37,9 @@ class AssistantWebView : public views::View, ...@@ -36,6 +37,9 @@ class AssistantWebView : public views::View,
int GetHeightForWidth(int width) const override; int GetHeightForWidth(int width) const override;
void ChildPreferredSizeChanged(views::View* child) override; void ChildPreferredSizeChanged(views::View* child) override;
// CaptionBarDelegate:
bool OnCaptionButtonPressed(CaptionButtonId id) override;
// AssistantControllerObserver: // AssistantControllerObserver:
void OnDeepLinkReceived( void OnDeepLinkReceived(
assistant::util::DeepLinkType type, assistant::util::DeepLinkType type,
......
...@@ -32,6 +32,12 @@ interface WebContentsManager { ...@@ -32,6 +32,12 @@ interface WebContentsManager {
// Releases any resources associated with any WebContents uniquely identified // Releases any resources associated with any WebContents uniquely identified
// by one of the specified |id_tokens|. // by one of the specified |id_tokens|.
ReleaseAllWebContents(array<mojo_base.mojom.UnguessableToken> id_tokens); ReleaseAllWebContents(array<mojo_base.mojom.UnguessableToken> id_tokens);
// Navigates the WebContents uniquely identified by |id_token| back relative
// to the current history entry. The callback returns true if the WebContents
// were navigated, false otherwise.
NavigateWebContentsBack(mojo_base.mojom.UnguessableToken id_token)
=> (bool navigated);
}; };
// Defines parameters for a managed WebContents. // Defines parameters for a managed WebContents.
......
...@@ -83,6 +83,18 @@ class ManagedWebContents : public content::WebContentsDelegate, ...@@ -83,6 +83,18 @@ class ManagedWebContents : public content::WebContentsDelegate,
std::move(callback_).Run(embed_token_); std::move(callback_).Run(embed_token_);
} }
void NavigateBack(
ash::mojom::WebContentsManager::NavigateWebContentsBackCallback
callback) {
content::NavigationController& controller = web_contents_->GetController();
if (controller.CanGoBack()) {
controller.GoBack();
std::move(callback).Run(true);
} else {
std::move(callback).Run(false);
}
}
private: private:
void InitWebContents(Profile* profile, void InitWebContents(Profile* profile,
ash::mojom::ManagedWebContentsParamsPtr params) { ash::mojom::ManagedWebContentsParamsPtr params) {
...@@ -191,3 +203,9 @@ void WebContentsManager::ReleaseAllWebContents( ...@@ -191,3 +203,9 @@ void WebContentsManager::ReleaseAllWebContents(
for (const base::UnguessableToken& id_token : id_tokens) for (const base::UnguessableToken& id_token : id_tokens)
managed_web_contents_map_.erase(id_token); managed_web_contents_map_.erase(id_token);
} }
void WebContentsManager::NavigateWebContentsBack(
const base::UnguessableToken& id_token,
ash::mojom::WebContentsManager::NavigateWebContentsBackCallback callback) {
managed_web_contents_map_[id_token]->NavigateBack(std::move(callback));
}
...@@ -39,6 +39,10 @@ class WebContentsManager : public ash::mojom::WebContentsManager { ...@@ -39,6 +39,10 @@ class WebContentsManager : public ash::mojom::WebContentsManager {
void ReleaseWebContents(const base::UnguessableToken& id_token) override; void ReleaseWebContents(const base::UnguessableToken& id_token) override;
void ReleaseAllWebContents( void ReleaseAllWebContents(
const std::vector<base::UnguessableToken>& id_tokens) override; const std::vector<base::UnguessableToken>& id_tokens) override;
void NavigateWebContentsBack(
const base::UnguessableToken& id_token,
ash::mojom::WebContentsManager::NavigateWebContentsBackCallback callback)
override;
private: private:
mojo::Binding<ash::mojom::WebContentsManager> binding_; mojo::Binding<ash::mojom::WebContentsManager> binding_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment