blob: caefbf2cedd6f3d06541a7bb94b586035608dfd1 [file] [log] [blame]
[email protected]87ab41e72012-01-04 18:45:111// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]73852b8f2010-05-14 00:38:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/printing/print_dialog_cloud.h"
[email protected]443e9312013-05-06 06:17:346
[email protected]ba4fc242011-10-04 18:56:567#include "base/bind.h"
avib896c712015-12-26 02:10:438#include "base/macros.h"
gabb15e19072016-05-11 20:45:419#include "base/threading/thread_task_runner_handle.h"
[email protected]4c2eb402014-06-13 02:18:5610#include "chrome/browser/browser_process.h"
[email protected]8ecad5e2010-12-02 21:18:3311#include "chrome/browser/profiles/profile.h"
[email protected]6c465292014-01-14 15:38:3112#include "chrome/browser/ui/browser.h"
[email protected]8c885f662014-07-04 16:37:3413#include "chrome/browser/ui/browser_window.h"
[email protected]09cff78782014-04-20 22:04:4814#include "components/cloud_devices/common/cloud_devices_urls.h"
[email protected]8e44a5b02014-06-19 19:03:2415#include "components/google/core/browser/google_util.h"
droger6414e4352015-07-16 20:39:1616#include "components/signin/core/browser/signin_header_helper.h"
gogerald71bf6c902015-12-08 00:49:3717#include "components/signin/core/browser/signin_metrics.h"
[email protected]8c885f662014-07-04 16:37:3418#include "components/signin/core/common/profile_management_switches.h"
[email protected]c38831a12011-10-28 12:44:4919#include "content/public/browser/browser_thread.h"
jama3d58012017-02-02 00:00:4120#include "content/public/browser/navigation_handle.h"
[email protected]0ec4898e2011-12-30 21:09:2421#include "content/public/browser/web_contents.h"
[email protected]6c465292014-01-14 15:38:3122#include "content/public/browser/web_contents_observer.h"
[email protected]520c2022012-03-15 00:13:1523
vitalybuka66de8e32015-06-26 20:58:5324namespace print_dialog_cloud {
[email protected]631bb742011-11-02 11:29:3925
[email protected]6c465292014-01-14 15:38:3126namespace {
27
[email protected]6c465292014-01-14 15:38:3128class SignInObserver : public content::WebContentsObserver {
29 public:
30 SignInObserver(content::WebContents* web_contents,
[email protected]6c465292014-01-14 15:38:3131 const base::Closure& callback)
32 : WebContentsObserver(web_contents),
[email protected]6c465292014-01-14 15:38:3133 callback_(callback),
34 weak_ptr_factory_(this) {
35 }
36
37 private:
38 // Overridden from content::WebContentsObserver:
jama3d58012017-02-02 00:00:4139 void DidFinishNavigation(
40 content::NavigationHandle* navigation_handle) override {
41 if (!navigation_handle->IsInMainFrame() ||
42 !navigation_handle->HasCommitted()) {
43 return;
44 }
45
46 if (cloud_devices::IsCloudPrintURL(navigation_handle->GetURL())) {
skyostil02598352015-06-12 12:37:2547 base::ThreadTaskRunnerHandle::Get()->PostTask(
tzikeb815222017-04-20 06:52:0248 FROM_HERE, base::BindOnce(&SignInObserver::OnSignIn,
49 weak_ptr_factory_.GetWeakPtr()));
[email protected]6c465292014-01-14 15:38:3150 }
51 }
52
dchengdf7e44a72014-10-21 23:50:1953 void WebContentsDestroyed() override { delete this; }
[email protected]6c465292014-01-14 15:38:3154
55 void OnSignIn() {
56 callback_.Run();
57 if (web_contents())
58 web_contents()->Close();
59 }
60
61 GURL cloud_print_url_;
62 base::Closure callback_;
63 base::WeakPtrFactory<SignInObserver> weak_ptr_factory_;
64
65 DISALLOW_COPY_AND_ASSIGN(SignInObserver);
66};
67
68} // namespace
69
[email protected]6c465292014-01-14 15:38:3170void CreateCloudPrintSigninTab(Browser* browser,
[email protected]7b92eea9e2014-04-01 09:17:1171 bool add_account,
[email protected]6c465292014-01-14 15:38:3172 const base::Closure& callback) {
vitalybuka66de8e32015-06-26 20:58:5373 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
[email protected]8c885f662014-07-04 16:37:3474 if (switches::IsEnableAccountConsistency() &&
75 !browser->profile()->IsOffTheRecord()) {
76 browser->window()->ShowAvatarBubbleFromAvatarButton(
77 add_account ? BrowserWindow::AVATAR_BUBBLE_MODE_ADD_ACCOUNT
78 : BrowserWindow::AVATAR_BUBBLE_MODE_SIGNIN,
gogerald71bf6c902015-12-08 00:49:3779 signin::ManageAccountsParams(),
jlebel233d5952017-02-23 10:55:3780 signin_metrics::AccessPoint::ACCESS_POINT_CLOUD_PRINT, false);
[email protected]8c885f662014-07-04 16:37:3481 } else {
82 GURL url = add_account ? cloud_devices::GetCloudPrintAddAccountURL()
83 : cloud_devices::GetCloudPrintSigninURL();
84 content::WebContents* web_contents =
85 browser->OpenURL(content::OpenURLParams(
86 google_util::AppendGoogleLocaleParam(
87 url, g_browser_process->GetApplicationLocale()),
nick3b04f322016-08-31 19:29:1988 content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB,
89 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false));
vitalybuka66de8e32015-06-26 20:58:5390 new SignInObserver(web_contents, callback);
[email protected]8c885f662014-07-04 16:37:3491 }
[email protected]4cd49022012-01-19 20:37:3792}
93
[email protected]228f0f02013-11-15 05:58:3694} // namespace print_dialog_cloud