OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/setup/win/auth_code_getter.h" | 5 #include "remoting/host/setup/win/auth_code_getter.h" |
6 | 6 |
7 #include "base/time.h" | 7 #include "base/time.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "base/win/scoped_bstr.h" | 9 #include "base/win/scoped_bstr.h" |
10 #include "base/win/scoped_variant.h" | 10 #include "base/win/scoped_variant.h" |
11 #include "remoting/host/setup/oauth_helper.h" | 11 #include "remoting/host/setup/oauth_helper.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 const int kUrlPollIntervalMs = 100; | 14 const int kUrlPollIntervalMs = 100; |
15 } // namespace remoting | 15 } // namespace remoting |
16 | 16 |
17 namespace remoting { | 17 namespace remoting { |
18 | 18 |
19 AuthCodeGetter::AuthCodeGetter() : | 19 AuthCodeGetter::AuthCodeGetter() : |
20 browser_(NULL), | 20 browser_(NULL), |
21 browser_running_(false), | |
22 timer_interval_(base::TimeDelta::FromMilliseconds(kUrlPollIntervalMs)) { | 21 timer_interval_(base::TimeDelta::FromMilliseconds(kUrlPollIntervalMs)) { |
23 } | 22 } |
24 | 23 |
25 AuthCodeGetter::~AuthCodeGetter() { | 24 AuthCodeGetter::~AuthCodeGetter() { |
26 KillBrowser(); | 25 KillBrowser(); |
27 } | 26 } |
28 | 27 |
29 void AuthCodeGetter::GetAuthCode( | 28 void AuthCodeGetter::GetAuthCode( |
30 base::Callback<void(const std::string&)> on_auth_code) { | 29 base::Callback<void(const std::string&)> on_auth_code) { |
31 if (browser_running_) { | 30 if (browser_) { |
32 on_auth_code.Run(""); | 31 on_auth_code.Run(""); |
33 return; | 32 return; |
34 } | 33 } |
35 on_auth_code_ = on_auth_code; | 34 on_auth_code_ = on_auth_code; |
36 HRESULT hr = browser_.CreateInstance(CLSID_InternetExplorer, NULL, | 35 HRESULT hr = browser_.CreateInstance(CLSID_InternetExplorer, NULL, |
37 CLSCTX_LOCAL_SERVER); | 36 CLSCTX_LOCAL_SERVER); |
38 if (FAILED(hr)) { | 37 if (FAILED(hr)) { |
39 on_auth_code_.Run(""); | 38 on_auth_code_.Run(""); |
40 return; | 39 return; |
41 } | 40 } |
42 browser_running_ = true; | |
43 base::win::ScopedBstr url(UTF8ToWide( | 41 base::win::ScopedBstr url(UTF8ToWide( |
44 GetOauthStartUrl(GetDefaultOauthRedirectUrl())).c_str()); | 42 GetOauthStartUrl(GetDefaultOauthRedirectUrl())).c_str()); |
45 base::win::ScopedVariant empty_variant; | 43 base::win::ScopedVariant empty_variant; |
46 hr = browser_->Navigate(url, empty_variant.AsInput(), empty_variant.AsInput(), | 44 hr = browser_->Navigate(url, empty_variant.AsInput(), empty_variant.AsInput(), |
47 empty_variant.AsInput(), empty_variant.AsInput()); | 45 empty_variant.AsInput(), empty_variant.AsInput()); |
48 if (FAILED(hr)) { | 46 if (FAILED(hr)) { |
49 KillBrowser(); | 47 KillBrowser(); |
50 on_auth_code_.Run(""); | 48 on_auth_code_.Run(""); |
51 return; | 49 return; |
52 } | 50 } |
53 browser_->put_Visible(VARIANT_TRUE); | 51 browser_->put_Visible(VARIANT_TRUE); |
54 StartTimer(); | 52 StartTimer(); |
55 } | 53 } |
56 | 54 |
57 void AuthCodeGetter::StartTimer() { | 55 void AuthCodeGetter::StartTimer() { |
58 timer_.Start(FROM_HERE, timer_interval_, this, &AuthCodeGetter::OnTimer); | 56 timer_.Start(FROM_HERE, timer_interval_, this, &AuthCodeGetter::OnTimer); |
59 } | 57 } |
60 | 58 |
61 void AuthCodeGetter::OnTimer() { | 59 void AuthCodeGetter::OnTimer() { |
62 std::string auth_code; | 60 std::string auth_code; |
63 if (TestBrowserUrl(&auth_code)) { | 61 if (TestBrowserUrl(&auth_code)) { |
64 on_auth_code_.Run(auth_code); | 62 on_auth_code_.Run(auth_code); |
65 } else { | 63 } else { |
66 StartTimer(); | 64 StartTimer(); |
67 } | 65 } |
68 } | 66 } |
69 | 67 |
70 bool AuthCodeGetter::TestBrowserUrl(std::string* auth_code) { | 68 bool AuthCodeGetter::TestBrowserUrl(std::string* auth_code) { |
71 *auth_code = ""; | 69 *auth_code = ""; |
72 if (!browser_running_) { | 70 if (!browser_) { |
73 return true; | 71 return true; |
74 } | 72 } |
75 base::win::ScopedBstr url; | 73 base::win::ScopedBstr url; |
76 HRESULT hr = browser_->get_LocationName(url.Receive()); | 74 HRESULT hr = browser_->get_LocationName(url.Receive()); |
77 if (!SUCCEEDED(hr)) { | 75 if (!SUCCEEDED(hr)) { |
78 KillBrowser(); | 76 KillBrowser(); |
79 return true; | 77 return true; |
80 } | 78 } |
81 *auth_code = GetOauthCodeInUrl(WideToUTF8(static_cast<BSTR>(url)), | 79 *auth_code = GetOauthCodeInUrl(WideToUTF8(static_cast<BSTR>(url)), |
82 GetDefaultOauthRedirectUrl()); | 80 GetDefaultOauthRedirectUrl()); |
83 if (!auth_code->empty()) { | 81 if (!auth_code->empty()) { |
84 KillBrowser(); | 82 KillBrowser(); |
85 return true; | 83 return true; |
86 } | 84 } |
87 return false; | 85 return false; |
88 } | 86 } |
89 | 87 |
90 void AuthCodeGetter::KillBrowser() { | 88 void AuthCodeGetter::KillBrowser() { |
91 if (browser_running_) { | 89 if (browser_) { |
92 browser_->Quit(); | 90 browser_->Quit(); |
93 browser_running_ = false; | 91 browser_.Release(); |
94 } | 92 } |
95 } | 93 } |
96 | 94 |
97 } // namespace remoting | 95 } // namespace remoting |
OLD | NEW |