summaryrefslogtreecommitdiffstats
path: root/remoting/host/setup/win/auth_code_getter.cc
blob: 2fbb68dfc361b78880cea068b3472d096aaf2802 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/setup/win/auth_code_getter.h"

#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_variant.h"
#include "remoting/host/setup/oauth_helper.h"

namespace {
const int kUrlPollIntervalMs = 100;
}  // namespace remoting

namespace remoting {

AuthCodeGetter::AuthCodeGetter() :
    browser_(NULL),
    timer_interval_(base::TimeDelta::FromMilliseconds(kUrlPollIntervalMs)) {
}

AuthCodeGetter::~AuthCodeGetter() {
  KillBrowser();
}

void AuthCodeGetter::GetAuthCode(
    base::Callback<void(const std::string&)> on_auth_code) {
  if (browser_) {
    on_auth_code.Run("");
    return;
  }
  on_auth_code_ = on_auth_code;
  HRESULT hr = browser_.CreateInstance(CLSID_InternetExplorer, NULL,
                                       CLSCTX_LOCAL_SERVER);
  if (FAILED(hr)) {
    on_auth_code_.Run("");
    return;
  }
  base::win::ScopedBstr url(base::UTF8ToWide(
      GetOauthStartUrl(GetDefaultOauthRedirectUrl())).c_str());
  base::win::ScopedVariant empty_variant;
  hr = browser_->Navigate(url, empty_variant.AsInput(), empty_variant.AsInput(),
                          empty_variant.AsInput(), empty_variant.AsInput());
  if (FAILED(hr)) {
    KillBrowser();
    on_auth_code_.Run("");
    return;
  }
  browser_->put_Visible(VARIANT_TRUE);
  StartTimer();
}

void AuthCodeGetter::StartTimer() {
  timer_.Start(FROM_HERE, timer_interval_, this, &AuthCodeGetter::OnTimer);
}

void AuthCodeGetter::OnTimer() {
  std::string auth_code;
  if (TestBrowserUrl(&auth_code)) {
    on_auth_code_.Run(auth_code);
  } else {
    StartTimer();
  }
}

bool AuthCodeGetter::TestBrowserUrl(std::string* auth_code) {
  *auth_code = "";
  if (!browser_) {
    return true;
  }
  base::win::ScopedBstr url;
  HRESULT hr = browser_->get_LocationName(url.Receive());
  if (!SUCCEEDED(hr)) {
    KillBrowser();
    return true;
  }
  *auth_code = GetOauthCodeInUrl(base::WideToUTF8(static_cast<BSTR>(url)),
                                 GetDefaultOauthRedirectUrl());
  if (!auth_code->empty()) {
    KillBrowser();
    return true;
  }
  return false;
}

void AuthCodeGetter::KillBrowser() {
  if (browser_) {
    browser_->Quit();
    browser_.Release();
  }
}

}  // namespace remoting