OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 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. | |
4 | |
5 #include "chrome/browser/remoting/setup_flow_login_step.h" | |
6 | |
7 #include "base/json/json_reader.h" | |
8 #include "base/json/json_writer.h" | |
9 #include "base/string_util.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "base/values.h" | |
12 #include "chrome/browser/dom_ui/dom_ui_util.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/common/net/gaia/gaia_constants.h" | |
15 #include "chrome/common/net/gaia/google_service_auth_error.h" | |
16 | |
17 namespace remoting { | |
18 | |
19 static const wchar_t kLoginIFrameXPath[] = L"//iframe[@id='login']"; | |
20 | |
21 SetupFlowLoginStep::SetupFlowLoginStep() { } | |
22 SetupFlowLoginStep::~SetupFlowLoginStep() { } | |
23 | |
24 void SetupFlowLoginStep::HandleMessage(const std::string& message, | |
25 const ListValue* args) { | |
26 if (message == "SubmitAuth") { | |
Alpha Left Google
2010/12/21 21:52:04
Make this class DOMMessageHandler and turn this in
Sergey Ulanov
2010/12/21 22:26:49
This is not possible right now. We would have to c
| |
27 std::string json(dom_ui_util::GetJsonResponseFromFirstArgumentInList(args)); | |
28 if (json.empty()) | |
29 return; | |
30 | |
31 scoped_ptr<Value> parsed_value(base::JSONReader::Read(json, false)); | |
32 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY)) { | |
33 NOTREACHED() << "Unable to parse auth data"; | |
34 return; | |
35 } | |
36 | |
37 CHECK(parsed_value->IsType(Value::TYPE_DICTIONARY)); | |
38 | |
39 std::string username, password, captcha; | |
40 DictionaryValue* result = static_cast<DictionaryValue*>(parsed_value.get()); | |
41 if (!result->GetString("user", &username) || | |
42 !result->GetString("pass", &password) || | |
43 !result->GetString("captcha", &captcha)) { | |
44 NOTREACHED() << "Unable to parse auth data"; | |
45 return; | |
46 } | |
47 | |
48 OnUserSubmittedAuth(username, password, captcha); | |
49 } | |
50 } | |
51 | |
52 void SetupFlowLoginStep::Cancel() { | |
53 if (authenticator_.get()) | |
54 authenticator_->CancelRequest(); | |
55 } | |
56 | |
57 void SetupFlowLoginStep::OnUserSubmittedAuth(const std::string& user, | |
58 const std::string& password, | |
59 const std::string& captcha) { | |
60 // Save the login name only. | |
61 login_ = user; | |
62 | |
63 // Start the authenticator. | |
64 authenticator_.reset( | |
65 new GaiaAuthFetcher(this, GaiaConstants::kChromeSource, | |
66 flow()->profile()->GetRequestContext())); | |
67 authenticator_->StartClientLogin(user, password, | |
68 GaiaConstants::kRemotingService, | |
69 "", captcha, | |
70 GaiaAuthFetcher::HostedAccountsAllowed); | |
71 } | |
72 | |
73 void SetupFlowLoginStep::OnClientLoginSuccess( | |
74 const GaiaAuthConsumer::ClientLoginResult& credentials) { | |
75 // Save the token for remoting. | |
76 remoting_token_ = credentials.token; | |
77 | |
78 // After login has succeeded try to fetch the token for sync. | |
79 // We need the token for sync to connect to the talk network. | |
80 authenticator_->StartIssueAuthToken(credentials.sid, credentials.lsid, | |
81 GaiaConstants::kSyncService); | |
82 } | |
83 | |
84 void SetupFlowLoginStep::OnClientLoginFailure( | |
85 const GoogleServiceAuthError& error) { | |
86 ShowGaiaFailed(error); | |
87 authenticator_.reset(); | |
88 } | |
89 | |
90 void SetupFlowLoginStep::OnIssueAuthTokenSuccess( | |
91 const std::string& service, const std::string& auth_token) { | |
92 // Save the sync token. | |
93 talk_token_ = auth_token; | |
94 authenticator_.reset(); | |
95 | |
96 Done(new SetupFlowDoneStep()); | |
97 } | |
98 | |
99 void SetupFlowLoginStep::OnIssueAuthTokenFailure(const std::string& service, | |
100 const GoogleServiceAuthError& error) { | |
101 ShowGaiaFailed(error); | |
102 authenticator_.reset(); | |
103 } | |
104 | |
105 void SetupFlowLoginStep::DoStart() { | |
106 DictionaryValue args; | |
107 args.SetString("iframeToShow", "login"); | |
108 // TODO(sergeyu): Supply current login name if the service was started before. | |
109 args.SetString("user", ""); | |
110 args.SetBoolean("editable_user", true); | |
111 ShowGaiaLogin(args); | |
112 } | |
113 | |
114 void SetupFlowLoginStep::ShowGaiaLogin(const DictionaryValue& args) { | |
115 DOMUI* dom_ui = flow()->dom_ui(); | |
116 DCHECK(dom_ui); | |
117 | |
118 dom_ui->CallJavascriptFunction(L"showGaiaLoginIframe"); | |
119 | |
120 std::string json; | |
121 base::JSONWriter::Write(&args, false, &json); | |
122 std::wstring javascript = std::wstring(L"showGaiaLogin") + | |
123 L"(" + UTF8ToWide(json) + L");"; | |
124 ExecuteJavascriptInIFrame(kLoginIFrameXPath, javascript); | |
125 } | |
126 | |
127 void SetupFlowLoginStep::ShowGaiaFailed(const GoogleServiceAuthError& error) { | |
128 DictionaryValue args; | |
129 args.SetString("iframeToShow", "login"); | |
130 args.SetString("user", ""); | |
131 args.SetInteger("error", error.state()); | |
132 args.SetBoolean("editable_user", true); | |
133 args.SetString("captchaUrl", error.captcha().image_url.spec()); | |
134 ShowGaiaLogin(args); | |
135 } | |
136 | |
137 } // namespace remoting | |
OLD | NEW |