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 "chrome/browser/extensions/api/identity/web_auth_flow.h" | 5 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
15 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
16 #include "chrome/browser/extensions/component_loader.h" | 16 #include "chrome/browser/extensions/component_loader.h" |
17 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/common/extensions/api/identity_private.h" | 19 #include "chrome/common/extensions/api/identity_private.h" |
20 #include "chrome/common/extensions/extension_constants.h" | 20 #include "chrome/common/extensions/extension_constants.h" |
21 #include "chrome/grit/browser_resources.h" | 21 #include "chrome/grit/browser_resources.h" |
22 #include "components/guest_view/browser/guest_view_base.h" | 22 #include "components/guest_view/browser/guest_view_base.h" |
23 #include "content/public/browser/navigation_details.h" | 23 #include "content/public/browser/navigation_details.h" |
24 #include "content/public/browser/navigation_entry.h" | 24 #include "content/public/browser/navigation_entry.h" |
| 25 #include "content/public/browser/navigation_handle.h" |
25 #include "content/public/browser/notification_details.h" | 26 #include "content/public/browser/notification_details.h" |
26 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
27 #include "content/public/browser/notification_source.h" | 28 #include "content/public/browser/notification_source.h" |
28 #include "content/public/browser/notification_types.h" | 29 #include "content/public/browser/notification_types.h" |
29 #include "content/public/browser/render_frame_host.h" | 30 #include "content/public/browser/render_frame_host.h" |
30 #include "content/public/browser/resource_request_details.h" | 31 #include "content/public/browser/resource_request_details.h" |
31 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
32 #include "crypto/random.h" | 33 #include "crypto/random.h" |
33 #include "extensions/browser/app_window/app_window.h" | 34 #include "extensions/browser/app_window/app_window.h" |
34 #include "extensions/browser/event_router.h" | 35 #include "extensions/browser/event_router.h" |
35 #include "extensions/browser/extension_system.h" | 36 #include "extensions/browser/extension_system.h" |
| 37 #include "net/http/http_response_headers.h" |
36 #include "url/gurl.h" | 38 #include "url/gurl.h" |
37 | 39 |
38 using content::RenderViewHost; | 40 using content::RenderViewHost; |
39 using content::ResourceRedirectDetails; | 41 using content::ResourceRedirectDetails; |
40 using content::WebContents; | 42 using content::WebContents; |
41 using content::WebContentsObserver; | 43 using content::WebContentsObserver; |
42 using guest_view::GuestViewBase; | 44 using guest_view::GuestViewBase; |
43 | 45 |
44 namespace extensions { | 46 namespace extensions { |
45 | 47 |
46 namespace identity_private = api::identity_private; | 48 namespace identity_private = api::identity_private; |
47 | 49 |
48 WebAuthFlow::WebAuthFlow( | 50 WebAuthFlow::WebAuthFlow( |
49 Delegate* delegate, | 51 Delegate* delegate, |
50 Profile* profile, | 52 Profile* profile, |
51 const GURL& provider_url, | 53 const GURL& provider_url, |
52 Mode mode) | 54 Mode mode) |
53 : delegate_(delegate), | 55 : delegate_(delegate), |
54 profile_(profile), | 56 profile_(profile), |
55 provider_url_(provider_url), | 57 provider_url_(provider_url), |
56 mode_(mode), | 58 mode_(mode), |
57 embedded_window_created_(false) { | 59 embedded_window_created_(false) { |
| 60 TRACE_EVENT_ASYNC_BEGIN0("identity", "WebAuthFlow", this); |
58 } | 61 } |
59 | 62 |
60 WebAuthFlow::~WebAuthFlow() { | 63 WebAuthFlow::~WebAuthFlow() { |
61 DCHECK(delegate_ == NULL); | 64 DCHECK(delegate_ == NULL); |
62 | 65 |
63 // Stop listening to notifications first since some of the code | 66 // Stop listening to notifications first since some of the code |
64 // below may generate notifications. | 67 // below may generate notifications. |
65 registrar_.RemoveAll(); | 68 registrar_.RemoveAll(); |
66 WebContentsObserver::Observe(nullptr); | 69 WebContentsObserver::Observe(nullptr); |
67 | 70 |
68 if (!app_window_key_.empty()) { | 71 if (!app_window_key_.empty()) { |
69 AppWindowRegistry::Get(profile_)->RemoveObserver(this); | 72 AppWindowRegistry::Get(profile_)->RemoveObserver(this); |
70 | 73 |
71 if (app_window_ && app_window_->web_contents()) | 74 if (app_window_ && app_window_->web_contents()) |
72 app_window_->web_contents()->Close(); | 75 app_window_->web_contents()->Close(); |
73 } | 76 } |
| 77 TRACE_EVENT_ASYNC_END0("identity", "WebAuthFlow", this); |
74 } | 78 } |
75 | 79 |
76 void WebAuthFlow::Start() { | 80 void WebAuthFlow::Start() { |
77 AppWindowRegistry::Get(profile_)->AddObserver(this); | 81 AppWindowRegistry::Get(profile_)->AddObserver(this); |
78 | 82 |
79 // Attach a random ID string to the window so we can recognize it | 83 // Attach a random ID string to the window so we can recognize it |
80 // in OnAppWindowAdded. | 84 // in OnAppWindowAdded. |
81 std::string random_bytes; | 85 std::string random_bytes; |
82 crypto::RandBytes(base::WriteInto(&random_bytes, 33), 32); | 86 crypto::RandBytes(base::WriteInto(&random_bytes, 33), 32); |
83 base::Base64Encode(random_bytes, &app_window_key_); | 87 base::Base64Encode(random_bytes, &app_window_key_); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 WebContentsObserver::Observe(web_contents); | 174 WebContentsObserver::Observe(web_contents); |
171 | 175 |
172 registrar_.RemoveAll(); | 176 registrar_.RemoveAll(); |
173 } | 177 } |
174 | 178 |
175 void WebAuthFlow::RenderProcessGone(base::TerminationStatus status) { | 179 void WebAuthFlow::RenderProcessGone(base::TerminationStatus status) { |
176 if (delegate_) | 180 if (delegate_) |
177 delegate_->OnAuthFlowFailure(WebAuthFlow::WINDOW_CLOSED); | 181 delegate_->OnAuthFlowFailure(WebAuthFlow::WINDOW_CLOSED); |
178 } | 182 } |
179 | 183 |
180 void WebAuthFlow::DidStartProvisionalLoadForFrame( | |
181 content::RenderFrameHost* render_frame_host, | |
182 const GURL& validated_url, | |
183 bool is_error_page, | |
184 bool is_iframe_srcdoc) { | |
185 if (!render_frame_host->GetParent()) | |
186 BeforeUrlLoaded(validated_url); | |
187 } | |
188 | |
189 void WebAuthFlow::DidFailProvisionalLoad( | |
190 content::RenderFrameHost* render_frame_host, | |
191 const GURL& validated_url, | |
192 int error_code, | |
193 const base::string16& error_description, | |
194 bool was_ignored_by_handler) { | |
195 TRACE_EVENT_ASYNC_STEP_PAST1("identity", | |
196 "WebAuthFlow", | |
197 this, | |
198 "DidFailProvisionalLoad", | |
199 "error_code", | |
200 error_code); | |
201 if (delegate_) | |
202 delegate_->OnAuthFlowFailure(LOAD_FAILED); | |
203 } | |
204 | |
205 void WebAuthFlow::DidGetRedirectForResourceRequest( | 184 void WebAuthFlow::DidGetRedirectForResourceRequest( |
206 const content::ResourceRedirectDetails& details) { | 185 const content::ResourceRedirectDetails& details) { |
207 BeforeUrlLoaded(details.new_url); | 186 BeforeUrlLoaded(details.new_url); |
208 } | 187 } |
209 | 188 |
210 void WebAuthFlow::TitleWasSet(content::NavigationEntry* entry, | 189 void WebAuthFlow::TitleWasSet(content::NavigationEntry* entry, |
211 bool explicit_set) { | 190 bool explicit_set) { |
212 if (delegate_) | 191 if (delegate_) |
213 delegate_->OnAuthFlowTitleChange(base::UTF16ToUTF8(entry->GetTitle())); | 192 delegate_->OnAuthFlowTitleChange(base::UTF16ToUTF8(entry->GetTitle())); |
214 } | 193 } |
215 | 194 |
216 void WebAuthFlow::DidStopLoading() { | 195 void WebAuthFlow::DidStopLoading() { |
217 AfterUrlLoaded(); | 196 AfterUrlLoaded(); |
218 } | 197 } |
219 | 198 |
220 void WebAuthFlow::DidNavigateMainFrame( | 199 void WebAuthFlow::DidStartNavigation( |
221 const content::LoadCommittedDetails& details, | 200 content::NavigationHandle* navigation_handle) { |
222 const content::FrameNavigateParams& params) { | 201 if (navigation_handle->IsInMainFrame()) |
223 if (delegate_ && details.http_status_code >= 400) | 202 BeforeUrlLoaded(navigation_handle->GetURL()); |
| 203 } |
| 204 |
| 205 void WebAuthFlow::DidFinishNavigation( |
| 206 content::NavigationHandle* navigation_handle) { |
| 207 bool failed = false; |
| 208 |
| 209 if (navigation_handle->GetNetErrorCode() != net::OK) { |
| 210 failed = true; |
| 211 TRACE_EVENT_ASYNC_STEP_PAST1("identity", "WebAuthFlow", this, |
| 212 "DidFinishNavigationFailure", "error_code", |
| 213 navigation_handle->GetNetErrorCode()); |
| 214 } else if (navigation_handle->IsInMainFrame() && |
| 215 navigation_handle->GetResponseHeaders()->response_code() >= 400) { |
| 216 failed = true; |
| 217 TRACE_EVENT_ASYNC_STEP_PAST1( |
| 218 "identity", "WebAuthFlow", this, "DidFinishNavigationFailure", |
| 219 "response_code", |
| 220 navigation_handle->GetResponseHeaders()->response_code()); |
| 221 } |
| 222 |
| 223 if (failed && delegate_) |
224 delegate_->OnAuthFlowFailure(LOAD_FAILED); | 224 delegate_->OnAuthFlowFailure(LOAD_FAILED); |
225 } | 225 } |
226 | 226 |
227 } // namespace extensions | 227 } // namespace extensions |
OLD | NEW |