| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/remoting/setup_flow_login_step.h" | 5 #include "chrome/browser/remoting/setup_flow_login_step.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/dom_ui/dom_ui_util.h" | 12 #include "chrome/browser/dom_ui/dom_ui_util.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/remoting/setup_flow_get_status_step.h" | 14 #include "chrome/browser/remoting/setup_flow_get_status_step.h" |
| 15 #include "chrome/common/net/gaia/gaia_constants.h" | 15 #include "chrome/common/net/gaia/gaia_constants.h" |
| 16 #include "chrome/common/net/gaia/google_service_auth_error.h" | 16 #include "chrome/common/net/gaia/google_service_auth_error.h" |
| 17 | 17 |
| 18 namespace remoting { | 18 namespace remoting { |
| 19 | 19 |
| 20 static const wchar_t kLoginIFrameXPath[] = L"//iframe[@id='login']"; | 20 static const wchar_t kLoginIFrameXPath[] = L"//iframe[@id='login']"; |
| 21 | 21 |
| 22 SetupFlowLoginStep::SetupFlowLoginStep() { } | 22 SetupFlowLoginStep::SetupFlowLoginStep() { } |
| 23 |
| 24 SetupFlowLoginStep::SetupFlowLoginStep(const string16& error_message) |
| 25 : error_message_(error_message) { |
| 26 } |
| 27 |
| 23 SetupFlowLoginStep::~SetupFlowLoginStep() { } | 28 SetupFlowLoginStep::~SetupFlowLoginStep() { } |
| 24 | 29 |
| 25 void SetupFlowLoginStep::HandleMessage(const std::string& message, | 30 void SetupFlowLoginStep::HandleMessage(const std::string& message, |
| 26 const Value* arg) { | 31 const Value* arg) { |
| 27 if (message == "SubmitAuth") { | 32 if (message == "SubmitAuth") { |
| 28 DCHECK(arg); | 33 DCHECK(arg); |
| 29 | 34 |
| 30 std::string json; | 35 std::string json; |
| 31 if (!arg->GetAsString(&json) || json.empty()) { | 36 if (!arg->GetAsString(&json) || json.empty()) { |
| 32 NOTREACHED(); | 37 NOTREACHED(); |
| 33 return; | 38 return; |
| 34 } | 39 } |
| 35 | 40 |
| 36 scoped_ptr<Value> parsed_value(base::JSONReader::Read(json, false)); | 41 scoped_ptr<Value> parsed_value(base::JSONReader::Read(json, false)); |
| 37 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY)) { | 42 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY)) { |
| 38 NOTREACHED() << "Unable to parse auth data"; | 43 NOTREACHED() << "Unable to parse auth data"; |
| 39 return; | 44 return; |
| 40 } | 45 } |
| 41 | 46 |
| 42 CHECK(parsed_value->IsType(Value::TYPE_DICTIONARY)); | 47 CHECK(parsed_value->IsType(Value::TYPE_DICTIONARY)); |
| 43 | 48 |
| 44 std::string username, password, captcha; | 49 std::string username, password, captcha, access_code; |
| 45 const DictionaryValue* result = | 50 const DictionaryValue* result = |
| 46 static_cast<const DictionaryValue*>(parsed_value.get()); | 51 static_cast<const DictionaryValue*>(parsed_value.get()); |
| 47 if (!result->GetString("user", &username) || | 52 if (!result->GetString("user", &username) || |
| 48 !result->GetString("pass", &password) || | 53 !result->GetString("pass", &password) || |
| 49 !result->GetString("captcha", &captcha)) { | 54 !result->GetString("captcha", &captcha) || |
| 55 !result->GetString("access_code", &access_code)) { |
| 50 NOTREACHED() << "Unable to parse auth data"; | 56 NOTREACHED() << "Unable to parse auth data"; |
| 51 return; | 57 return; |
| 52 } | 58 } |
| 53 | 59 |
| 54 OnUserSubmittedAuth(username, password, captcha); | 60 OnUserSubmittedAuth(username, password, captcha, access_code); |
| 55 } | 61 } |
| 56 } | 62 } |
| 57 | 63 |
| 58 void SetupFlowLoginStep::Cancel() { | 64 void SetupFlowLoginStep::Cancel() { |
| 59 if (authenticator_.get()) | 65 if (authenticator_.get()) |
| 60 authenticator_->CancelRequest(); | 66 authenticator_->CancelRequest(); |
| 61 } | 67 } |
| 62 | 68 |
| 63 void SetupFlowLoginStep::OnUserSubmittedAuth(const std::string& user, | 69 void SetupFlowLoginStep::OnUserSubmittedAuth(const std::string& user, |
| 64 const std::string& password, | 70 const std::string& password, |
| 65 const std::string& captcha) { | 71 const std::string& captcha, |
| 72 const std::string& access_code) { |
| 66 flow()->context()->login = user; | 73 flow()->context()->login = user; |
| 67 | 74 |
| 68 // Start the authenticator. | 75 // Start the authenticator. |
| 69 authenticator_.reset( | 76 authenticator_.reset( |
| 70 new GaiaAuthFetcher(this, GaiaConstants::kChromeSource, | 77 new GaiaAuthFetcher(this, GaiaConstants::kChromeSource, |
| 71 flow()->profile()->GetRequestContext())); | 78 flow()->profile()->GetRequestContext())); |
| 72 authenticator_->StartClientLogin(user, password, | 79 |
| 80 std::string remoting_password; |
| 81 if (!access_code.empty()) |
| 82 remoting_password = access_code; |
| 83 else |
| 84 remoting_password = password; |
| 85 |
| 86 authenticator_->StartClientLogin(user, remoting_password, |
| 73 GaiaConstants::kRemotingService, | 87 GaiaConstants::kRemotingService, |
| 74 "", captcha, | 88 "", captcha, |
| 75 GaiaAuthFetcher::HostedAccountsAllowed); | 89 GaiaAuthFetcher::HostedAccountsAllowed); |
| 76 } | 90 } |
| 77 | 91 |
| 78 void SetupFlowLoginStep::OnClientLoginSuccess( | 92 void SetupFlowLoginStep::OnClientLoginSuccess( |
| 79 const GaiaAuthConsumer::ClientLoginResult& credentials) { | 93 const GaiaAuthConsumer::ClientLoginResult& credentials) { |
| 80 // Save the token for remoting. | 94 // Save the token for remoting. |
| 81 flow()->context()->remoting_token = credentials.token; | 95 flow()->context()->remoting_token = credentials.token; |
| 82 | 96 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 105 const GoogleServiceAuthError& error) { | 119 const GoogleServiceAuthError& error) { |
| 106 ShowGaiaFailed(error); | 120 ShowGaiaFailed(error); |
| 107 authenticator_.reset(); | 121 authenticator_.reset(); |
| 108 } | 122 } |
| 109 | 123 |
| 110 void SetupFlowLoginStep::DoStart() { | 124 void SetupFlowLoginStep::DoStart() { |
| 111 DictionaryValue args; | 125 DictionaryValue args; |
| 112 // TODO(sergeyu): Supply current login name if the service was started before. | 126 // TODO(sergeyu): Supply current login name if the service was started before. |
| 113 args.SetString("user", ""); | 127 args.SetString("user", ""); |
| 114 args.SetBoolean("editable_user", true); | 128 args.SetBoolean("editable_user", true); |
| 129 if (!error_message_.empty()) |
| 130 args.SetString("error_message", error_message_); |
| 115 ShowGaiaLogin(args); | 131 ShowGaiaLogin(args); |
| 116 } | 132 } |
| 117 | 133 |
| 118 void SetupFlowLoginStep::ShowGaiaLogin(const DictionaryValue& args) { | 134 void SetupFlowLoginStep::ShowGaiaLogin(const DictionaryValue& args) { |
| 119 DOMUI* dom_ui = flow()->dom_ui(); | 135 DOMUI* dom_ui = flow()->dom_ui(); |
| 120 DCHECK(dom_ui); | 136 DCHECK(dom_ui); |
| 121 | 137 |
| 122 dom_ui->CallJavascriptFunction(L"showLogin"); | 138 dom_ui->CallJavascriptFunction(L"showLogin"); |
| 123 | 139 |
| 124 std::string json; | 140 std::string json; |
| 125 base::JSONWriter::Write(&args, false, &json); | 141 base::JSONWriter::Write(&args, false, &json); |
| 126 std::wstring javascript = std::wstring(L"showGaiaLogin") + | 142 std::wstring javascript = std::wstring(L"showGaiaLogin(") + |
| 127 L"(" + UTF8ToWide(json) + L");"; | 143 UTF8ToWide(json) + L");"; |
| 128 ExecuteJavascriptInIFrame(kLoginIFrameXPath, javascript); | 144 ExecuteJavascriptInIFrame(kLoginIFrameXPath, javascript); |
| 129 } | 145 } |
| 130 | 146 |
| 131 void SetupFlowLoginStep::ShowGaiaFailed(const GoogleServiceAuthError& error) { | 147 void SetupFlowLoginStep::ShowGaiaFailed(const GoogleServiceAuthError& error) { |
| 132 DictionaryValue args; | 148 DictionaryValue args; |
| 133 args.SetString("user", ""); | |
| 134 args.SetInteger("error", error.state()); | 149 args.SetInteger("error", error.state()); |
| 135 args.SetBoolean("editable_user", true); | 150 args.SetBoolean("editable_user", true); |
| 136 args.SetString("captchaUrl", error.captcha().image_url.spec()); | 151 args.SetString("captchaUrl", error.captcha().image_url.spec()); |
| 137 ShowGaiaLogin(args); | 152 ShowGaiaLogin(args); |
| 138 } | 153 } |
| 139 | 154 |
| 140 } // namespace remoting | 155 } // namespace remoting |
| OLD | NEW |