Commit f6a8aff4 authored by rbpotter's avatar rbpotter Committed by Commit Bot

Print Preview: Simplify signin

Print Preview Handler was only observing the identity manager if
mirror was enabled. However, mirror is now generally disabled, so Print
Preview never actually received any notifications about cookie jar
changes. This CL changes PrintPreviewHandler to always add this observer
unless cloud print is disabled (in which case Print Preview does not
need login information).

With the observer, the callback after sign in completion is no longer
necessary. If the user signs in to cloud print successfully,
IdentityManager::Observer::OnAccountToCookieCompleted() will be called.
The reload-printer-list event fired from this function triggers a call
to onDestinationsReload(), which is the same call that happens after
the sign in promise is resolved.

The only expected behavior change from this CL is that if the
destinations dialog is open when a user signs in or out in a different
tab, the destinations should refresh.

Bug: 932692, 958767
Change-Id: Ibdabf382f4eb8f6c438376b2f2415c27f4e9c586
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1626933Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#663859}
parent dc112fc8
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include "chrome/browser/printing/print_dialog_cloud.h" #include "chrome/browser/printing/print_dialog_cloud.h"
#include "base/bind.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
...@@ -28,12 +27,8 @@ namespace { ...@@ -28,12 +27,8 @@ namespace {
class SignInObserver : public content::WebContentsObserver { class SignInObserver : public content::WebContentsObserver {
public: public:
SignInObserver(content::WebContents* web_contents, explicit SignInObserver(content::WebContents* web_contents)
const base::Closure& callback) : WebContentsObserver(web_contents), weak_ptr_factory_(this) {}
: WebContentsObserver(web_contents),
callback_(callback),
weak_ptr_factory_(this) {
}
private: private:
// Overridden from content::WebContentsObserver: // Overridden from content::WebContentsObserver:
...@@ -54,13 +49,10 @@ class SignInObserver : public content::WebContentsObserver { ...@@ -54,13 +49,10 @@ class SignInObserver : public content::WebContentsObserver {
void WebContentsDestroyed() override { delete this; } void WebContentsDestroyed() override { delete this; }
void OnSignIn() { void OnSignIn() {
callback_.Run();
if (web_contents()) if (web_contents())
web_contents()->Close(); web_contents()->Close();
} }
GURL cloud_print_url_;
base::Closure callback_;
base::WeakPtrFactory<SignInObserver> weak_ptr_factory_; base::WeakPtrFactory<SignInObserver> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(SignInObserver); DISALLOW_COPY_AND_ASSIGN(SignInObserver);
...@@ -68,9 +60,7 @@ class SignInObserver : public content::WebContentsObserver { ...@@ -68,9 +60,7 @@ class SignInObserver : public content::WebContentsObserver {
} // namespace } // namespace
void CreateCloudPrintSigninTab(Browser* browser, void CreateCloudPrintSigninTab(Browser* browser, bool add_account) {
bool add_account,
const base::Closure& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (AccountConsistencyModeManager::IsMirrorEnabledForProfile( if (AccountConsistencyModeManager::IsMirrorEnabledForProfile(
browser->profile())) { browser->profile())) {
...@@ -88,7 +78,8 @@ void CreateCloudPrintSigninTab(Browser* browser, ...@@ -88,7 +78,8 @@ void CreateCloudPrintSigninTab(Browser* browser,
url, g_browser_process->GetApplicationLocale()), url, g_browser_process->GetApplicationLocale()),
content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); ui::PAGE_TRANSITION_AUTO_BOOKMARK, false));
new SignInObserver(web_contents, callback); // This observer will delete itself after destroying the WebContents.
new SignInObserver(web_contents);
} }
} }
......
...@@ -7,8 +7,6 @@ ...@@ -7,8 +7,6 @@
#include <string> #include <string>
#include "base/callback.h"
class Browser; class Browser;
class Profile; class Profile;
...@@ -20,10 +18,7 @@ namespace print_dialog_cloud { ...@@ -20,10 +18,7 @@ namespace print_dialog_cloud {
// Creates a tab with Google 'sign in' or 'add account' page, based on // Creates a tab with Google 'sign in' or 'add account' page, based on
// passed |add_account| value. // passed |add_account| value.
// Calls |callback| when complete. void CreateCloudPrintSigninTab(Browser* browser, bool add_account);
void CreateCloudPrintSigninTab(Browser* browser,
bool add_account,
const base::Closure& callback);
// Parse switches from command_line and display the print dialog as appropriate. // Parse switches from command_line and display the print dialog as appropriate.
bool CreatePrintDialogFromCommandLine(Profile* profile, bool CreatePrintDialogFromCommandLine(Profile* profile,
......
...@@ -279,15 +279,13 @@ cr.define('print_preview', function() { ...@@ -279,15 +279,13 @@ cr.define('print_preview', function() {
} }
/** /**
* Opens the Google Cloud Print sign-in tab. The DESTINATIONS_RELOAD event * Opens the Google Cloud Print sign-in tab. If the user signs in
* will be dispatched in response. * successfully, the user-accounts-updated event will be sent in response.
* @param {boolean} addAccount Whether to open an 'add a new account' or * @param {boolean} addAccount Whether to open an 'add a new account' or
* default sign in page. * default sign in page.
* @return {!Promise} Promise that resolves when the sign in tab has been
* closed and the destinations should be reloaded.
*/ */
signIn(addAccount) { signIn(addAccount) {
return cr.sendWithPromise('signIn', addAccount); chrome.send('signIn', [addAccount]);
} }
/** /**
......
...@@ -288,9 +288,7 @@ Polymer({ ...@@ -288,9 +288,7 @@ Polymer({
onSignInClick_: function() { onSignInClick_: function() {
this.metrics_.record( this.metrics_.record(
print_preview.Metrics.DestinationSearchBucket.SIGNIN_TRIGGERED); print_preview.Metrics.DestinationSearchBucket.SIGNIN_TRIGGERED);
print_preview.NativeLayer.getInstance().signIn(false).then(() => { print_preview.NativeLayer.getInstance().signIn(false);
this.destinationStore.onDestinationsReload();
});
}, },
/** @private */ /** @private */
...@@ -380,9 +378,7 @@ Polymer({ ...@@ -380,9 +378,7 @@ Polymer({
this.metrics_.record( this.metrics_.record(
print_preview.Metrics.DestinationSearchBucket.ACCOUNT_CHANGED); print_preview.Metrics.DestinationSearchBucket.ACCOUNT_CHANGED);
} else { } else {
print_preview.NativeLayer.getInstance().signIn(true).then( print_preview.NativeLayer.getInstance().signIn(true);
this.destinationStore.onDestinationsReload.bind(
this.destinationStore));
const options = select.querySelectorAll('option'); const options = select.querySelectorAll('option');
for (let i = 0; i < options.length; i++) { for (let i = 0; i < options.length; i++) {
if (options[i].value == this.activeUser) { if (options[i].value == this.activeUser) {
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector>
#include "base/base64.h" #include "base/base64.h"
#include "base/bind.h" #include "base/bind.h"
...@@ -855,22 +856,13 @@ void PrintPreviewHandler::HandlePrinterSetup(const base::ListValue* args) { ...@@ -855,22 +856,13 @@ void PrintPreviewHandler::HandlePrinterSetup(const base::ListValue* args) {
printer_name)); printer_name));
} }
void PrintPreviewHandler::OnSigninComplete(const std::string& callback_id) {
ResolveJavascriptCallback(base::Value(callback_id), base::Value());
}
void PrintPreviewHandler::HandleSignin(const base::ListValue* args) { void PrintPreviewHandler::HandleSignin(const base::ListValue* args) {
std::string callback_id;
bool add_account = false; bool add_account = false;
CHECK(args->GetString(0, &callback_id)); CHECK(args->GetBoolean(0, &add_account));
CHECK(!callback_id.empty());
CHECK(args->GetBoolean(1, &add_account));
chrome::ScopedTabbedBrowserDisplayer displayer(Profile::FromWebUI(web_ui())); chrome::ScopedTabbedBrowserDisplayer displayer(Profile::FromWebUI(web_ui()));
print_dialog_cloud::CreateCloudPrintSigninTab( print_dialog_cloud::CreateCloudPrintSigninTab(displayer.browser(),
displayer.browser(), add_account, add_account);
base::Bind(&PrintPreviewHandler::OnSigninComplete,
weak_factory_.GetWeakPtr(), callback_id));
} }
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
...@@ -1001,7 +993,7 @@ void PrintPreviewHandler::SendInitialSettings( ...@@ -1001,7 +993,7 @@ void PrintPreviewHandler::SendInitialSettings(
initial_settings.SetBoolKey(kHeaderFooter, initial_settings.SetBoolKey(kHeaderFooter,
prefs->GetBoolean(prefs::kPrintHeaderFooter)); prefs->GetBoolean(prefs::kPrintHeaderFooter));
} }
if (prefs->GetBoolean(prefs::kCloudPrintSubmitEnabled) && if (IsCloudPrintEnabled() &&
!base::FeatureList::IsEnabled(features::kCloudPrinterHandler)) { !base::FeatureList::IsEnabled(features::kCloudPrinterHandler)) {
initial_settings.SetStringKey( initial_settings.SetStringKey(
kCloudPrintURL, GURL(cloud_devices::GetCloudPrintURL()).spec()); kCloudPrintURL, GURL(cloud_devices::GetCloudPrintURL()).spec());
...@@ -1024,7 +1016,7 @@ void PrintPreviewHandler::SendInitialSettings( ...@@ -1024,7 +1016,7 @@ void PrintPreviewHandler::SendInitialSettings(
} }
GetNumberFormatAndMeasurementSystem(&initial_settings); GetNumberFormatAndMeasurementSystem(&initial_settings);
if (prefs->GetBoolean(prefs::kCloudPrintSubmitEnabled)) { if (IsCloudPrintEnabled()) {
GetUserAccountList(&initial_settings); GetUserAccountList(&initial_settings);
} }
...@@ -1113,8 +1105,6 @@ WebContents* PrintPreviewHandler::GetInitiator() const { ...@@ -1113,8 +1105,6 @@ WebContents* PrintPreviewHandler::GetInitiator() const {
return dialog_controller->GetInitiator(preview_web_contents()); return dialog_controller->GetInitiator(preview_web_contents());
} }
// TODO(crbug.com/932692): Investigate the replacement or removal of
// this override altogether.
void PrintPreviewHandler::OnAccountsInCookieUpdated( void PrintPreviewHandler::OnAccountsInCookieUpdated(
const identity::AccountsInCookieJarInfo& accounts_in_cookie_jar_info, const identity::AccountsInCookieJarInfo& accounts_in_cookie_jar_info,
const GoogleServiceAuthError& error) { const GoogleServiceAuthError& error) {
...@@ -1330,22 +1320,28 @@ void PrintPreviewHandler::OnPrintResult(const std::string& callback_id, ...@@ -1330,22 +1320,28 @@ void PrintPreviewHandler::OnPrintResult(const std::string& callback_id,
void PrintPreviewHandler::RegisterForGaiaCookieChanges() { void PrintPreviewHandler::RegisterForGaiaCookieChanges() {
DCHECK(!identity_manager_); DCHECK(!identity_manager_);
cloud_print_enabled_ =
GetPrefs()->GetBoolean(prefs::kCloudPrintSubmitEnabled);
if (!cloud_print_enabled_)
return;
Profile* profile = Profile::FromWebUI(web_ui()); Profile* profile = Profile::FromWebUI(web_ui());
identity_manager_ = IdentityManagerFactory::GetForProfile(profile); identity_manager_ = IdentityManagerFactory::GetForProfile(profile);
if (AccountConsistencyModeManager::IsMirrorEnabledForProfile(profile)) {
identity_manager_->AddObserver(this); identity_manager_->AddObserver(this);
}
} }
void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { void PrintPreviewHandler::UnregisterForGaiaCookieChanges() {
if (!identity_manager_) if (!identity_manager_)
return; return;
Profile* profile = Profile::FromWebUI(web_ui());
if (AccountConsistencyModeManager::IsMirrorEnabledForProfile(profile)) {
identity_manager_->RemoveObserver(this); identity_manager_->RemoveObserver(this);
}
identity_manager_ = nullptr; identity_manager_ = nullptr;
cloud_print_enabled_ = false;
}
bool PrintPreviewHandler::IsCloudPrintEnabled() {
return cloud_print_enabled_;
} }
void PrintPreviewHandler::BadMessageReceived() { void PrintPreviewHandler::BadMessageReceived() {
......
...@@ -124,6 +124,7 @@ class PrintPreviewHandler : public content::WebUIMessageHandler, ...@@ -124,6 +124,7 @@ class PrintPreviewHandler : public content::WebUIMessageHandler,
protected: protected:
// Protected so unit tests can override. // Protected so unit tests can override.
virtual PrinterHandler* GetPrinterHandler(PrinterType printer_type); virtual PrinterHandler* GetPrinterHandler(PrinterType printer_type);
virtual bool IsCloudPrintEnabled();
// Shuts down the initiator renderer. Called when a bad IPC message is // Shuts down the initiator renderer. Called when a bad IPC message is
// received. // received.
...@@ -220,11 +221,8 @@ class PrintPreviewHandler : public content::WebUIMessageHandler, ...@@ -220,11 +221,8 @@ class PrintPreviewHandler : public content::WebUIMessageHandler,
void HandleShowSystemDialog(const base::ListValue* args); void HandleShowSystemDialog(const base::ListValue* args);
#endif #endif
// Callback for the signin dialog to call once signin is complete. // Opens a new tab to allow the user to sign into cloud print. |args| holds
void OnSigninComplete(const std::string& callback_id); // a boolean indicating whether the user is adding an account.
// Brings up a dialog to allow the user to sign into cloud print.
// |args| is unused.
void HandleSignin(const base::ListValue* args); void HandleSignin(const base::ListValue* args);
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
...@@ -322,6 +320,9 @@ class PrintPreviewHandler : public content::WebUIMessageHandler, ...@@ -322,6 +320,9 @@ class PrintPreviewHandler : public content::WebUIMessageHandler,
// Whether we have already logged the number of printers this session. // Whether we have already logged the number of printers this session.
bool has_logged_printers_count_; bool has_logged_printers_count_;
// Whether Google Cloud Print is enabled for the active profile.
bool cloud_print_enabled_ = false;
// The settings used for the most recent preview request. // The settings used for the most recent preview request.
base::Value last_preview_settings_; base::Value last_preview_settings_;
......
...@@ -209,6 +209,8 @@ class TestPrintPreviewHandler : public PrintPreviewHandler { ...@@ -209,6 +209,8 @@ class TestPrintPreviewHandler : public PrintPreviewHandler {
return test_printer_handler_.get(); return test_printer_handler_.get();
} }
bool IsCloudPrintEnabled() override { return true; }
void RegisterForGaiaCookieChanges() override {} void RegisterForGaiaCookieChanges() override {}
void UnregisterForGaiaCookieChanges() override {} void UnregisterForGaiaCookieChanges() override {}
......
...@@ -198,7 +198,7 @@ cr.define('print_preview', function() { ...@@ -198,7 +198,7 @@ cr.define('print_preview', function() {
/** @override */ /** @override */
signIn(addAccount) { signIn(addAccount) {
this.methodCalled('signIn', addAccount); this.methodCalled('signIn', addAccount);
return Promise.resolve(); cr.webUIListenerCallback('reload-printer-list');
} }
/** /**
......
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