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 "remoting/host/setup/win/auth_code_getter.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/time/time.h"
9 #include "base/win/scoped_bstr.h"
10 #include "base/win/scoped_variant.h"
11 #include "remoting/host/setup/oauth_helper.h"
14 const int kUrlPollIntervalMs
= 100;
15 } // namespace remoting
19 AuthCodeGetter::AuthCodeGetter() :
21 timer_interval_(base::TimeDelta::FromMilliseconds(kUrlPollIntervalMs
)) {
24 AuthCodeGetter::~AuthCodeGetter() {
28 void AuthCodeGetter::GetAuthCode(
29 base::Callback
<void(const std::string
&)> on_auth_code
) {
34 on_auth_code_
= on_auth_code
;
35 HRESULT hr
= browser_
.CreateInstance(CLSID_InternetExplorer
, nullptr,
38 on_auth_code_
.Run("");
41 base::win::ScopedBstr
url(base::UTF8ToWide(
42 GetOauthStartUrl(GetDefaultOauthRedirectUrl())).c_str());
43 base::win::ScopedVariant empty_variant
;
44 hr
= browser_
->Navigate(url
, empty_variant
.AsInput(), empty_variant
.AsInput(),
45 empty_variant
.AsInput(), empty_variant
.AsInput());
48 on_auth_code_
.Run("");
51 browser_
->put_Visible(VARIANT_TRUE
);
55 void AuthCodeGetter::StartTimer() {
56 timer_
.Start(FROM_HERE
, timer_interval_
, this, &AuthCodeGetter::OnTimer
);
59 void AuthCodeGetter::OnTimer() {
60 std::string auth_code
;
61 if (TestBrowserUrl(&auth_code
)) {
62 on_auth_code_
.Run(auth_code
);
68 bool AuthCodeGetter::TestBrowserUrl(std::string
* auth_code
) {
70 if (!browser_
.get()) {
73 base::win::ScopedBstr url
;
74 HRESULT hr
= browser_
->get_LocationName(url
.Receive());
79 *auth_code
= GetOauthCodeInUrl(base::WideToUTF8(static_cast<BSTR
>(url
)),
80 GetDefaultOauthRedirectUrl());
81 if (!auth_code
->empty()) {
88 void AuthCodeGetter::KillBrowser() {
95 } // namespace remoting