Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / printing / print_dialog_cloud.cc
blobb22feeb5b0dc11e4921f03ed4894f30407a02c34
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/printing/print_dialog_cloud.h"
7 #include "base/bind.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "components/cloud_devices/common/cloud_devices_urls.h"
14 #include "components/google/core/browser/google_util.h"
15 #include "components/signin/core/browser/signin_header_helper.h"
16 #include "components/signin/core/common/profile_management_switches.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_contents_observer.h"
21 namespace print_dialog_cloud {
23 namespace {
25 class SignInObserver : public content::WebContentsObserver {
26 public:
27 SignInObserver(content::WebContents* web_contents,
28 const base::Closure& callback)
29 : WebContentsObserver(web_contents),
30 callback_(callback),
31 weak_ptr_factory_(this) {
34 private:
35 // Overridden from content::WebContentsObserver:
36 void DidNavigateMainFrame(
37 const content::LoadCommittedDetails& details,
38 const content::FrameNavigateParams& params) override {
39 if (cloud_devices::IsCloudPrintURL(params.url)) {
40 base::ThreadTaskRunnerHandle::Get()->PostTask(
41 FROM_HERE, base::Bind(&SignInObserver::OnSignIn,
42 weak_ptr_factory_.GetWeakPtr()));
46 void WebContentsDestroyed() override { delete this; }
48 void OnSignIn() {
49 callback_.Run();
50 if (web_contents())
51 web_contents()->Close();
54 GURL cloud_print_url_;
55 base::Closure callback_;
56 base::WeakPtrFactory<SignInObserver> weak_ptr_factory_;
58 DISALLOW_COPY_AND_ASSIGN(SignInObserver);
61 } // namespace
63 void CreateCloudPrintSigninTab(Browser* browser,
64 bool add_account,
65 const base::Closure& callback) {
66 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
67 if (switches::IsEnableAccountConsistency() &&
68 !browser->profile()->IsOffTheRecord()) {
69 browser->window()->ShowAvatarBubbleFromAvatarButton(
70 add_account ? BrowserWindow::AVATAR_BUBBLE_MODE_ADD_ACCOUNT
71 : BrowserWindow::AVATAR_BUBBLE_MODE_SIGNIN,
72 signin::ManageAccountsParams());
73 } else {
74 GURL url = add_account ? cloud_devices::GetCloudPrintAddAccountURL()
75 : cloud_devices::GetCloudPrintSigninURL();
76 content::WebContents* web_contents =
77 browser->OpenURL(content::OpenURLParams(
78 google_util::AppendGoogleLocaleParam(
79 url, g_browser_process->GetApplicationLocale()),
80 content::Referrer(),
81 NEW_FOREGROUND_TAB,
82 ui::PAGE_TRANSITION_AUTO_BOOKMARK,
83 false));
84 new SignInObserver(web_contents, callback);
88 } // namespace print_dialog_cloud