| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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/extensions/api/identity/identity_launch_web_auth_flow_f
unction.h" |
| 6 |
| 7 #include "chrome/browser/extensions/api/identity/identity_api.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/common/extensions/api/identity.h" |
| 10 |
| 11 namespace extensions { |
| 12 |
| 13 namespace { |
| 14 |
| 15 static const char kChromiumDomainRedirectUrlPattern[] = |
| 16 "https://%s.chromiumapp.org/"; |
| 17 |
| 18 } // namespace |
| 19 |
| 20 namespace identity = api::identity; |
| 21 |
| 22 IdentityLaunchWebAuthFlowFunction::IdentityLaunchWebAuthFlowFunction() {} |
| 23 |
| 24 IdentityLaunchWebAuthFlowFunction::~IdentityLaunchWebAuthFlowFunction() { |
| 25 if (auth_flow_) |
| 26 auth_flow_.release()->DetachDelegateAndDelete(); |
| 27 } |
| 28 |
| 29 bool IdentityLaunchWebAuthFlowFunction::RunAsync() { |
| 30 if (GetProfile()->IsOffTheRecord()) { |
| 31 error_ = identity_constants::kOffTheRecord; |
| 32 return false; |
| 33 } |
| 34 |
| 35 std::unique_ptr<identity::LaunchWebAuthFlow::Params> params( |
| 36 identity::LaunchWebAuthFlow::Params::Create(*args_)); |
| 37 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 38 |
| 39 GURL auth_url(params->details.url); |
| 40 WebAuthFlow::Mode mode = |
| 41 params->details.interactive && *params->details.interactive ? |
| 42 WebAuthFlow::INTERACTIVE : WebAuthFlow::SILENT; |
| 43 |
| 44 // Set up acceptable target URLs. (Does not include chrome-extension |
| 45 // scheme for this version of the API.) |
| 46 InitFinalRedirectURLPrefix(extension()->id()); |
| 47 |
| 48 AddRef(); // Balanced in OnAuthFlowSuccess/Failure. |
| 49 |
| 50 auth_flow_.reset(new WebAuthFlow(this, GetProfile(), auth_url, mode)); |
| 51 auth_flow_->Start(); |
| 52 return true; |
| 53 } |
| 54 |
| 55 void IdentityLaunchWebAuthFlowFunction::InitFinalRedirectURLPrefixForTest( |
| 56 const std::string& extension_id) { |
| 57 InitFinalRedirectURLPrefix(extension_id); |
| 58 } |
| 59 |
| 60 void IdentityLaunchWebAuthFlowFunction::InitFinalRedirectURLPrefix( |
| 61 const std::string& extension_id) { |
| 62 if (final_url_prefix_.is_empty()) { |
| 63 final_url_prefix_ = GURL(base::StringPrintf( |
| 64 kChromiumDomainRedirectUrlPattern, extension_id.c_str())); |
| 65 } |
| 66 } |
| 67 |
| 68 void IdentityLaunchWebAuthFlowFunction::OnAuthFlowFailure( |
| 69 WebAuthFlow::Failure failure) { |
| 70 switch (failure) { |
| 71 case WebAuthFlow::WINDOW_CLOSED: |
| 72 error_ = identity_constants::kUserRejected; |
| 73 break; |
| 74 case WebAuthFlow::INTERACTION_REQUIRED: |
| 75 error_ = identity_constants::kInteractionRequired; |
| 76 break; |
| 77 case WebAuthFlow::LOAD_FAILED: |
| 78 error_ = identity_constants::kPageLoadFailure; |
| 79 break; |
| 80 default: |
| 81 NOTREACHED() << "Unexpected error from web auth flow: " << failure; |
| 82 error_ = identity_constants::kInvalidRedirect; |
| 83 break; |
| 84 } |
| 85 SendResponse(false); |
| 86 if (auth_flow_) |
| 87 auth_flow_.release()->DetachDelegateAndDelete(); |
| 88 Release(); // Balanced in RunAsync. |
| 89 } |
| 90 |
| 91 void IdentityLaunchWebAuthFlowFunction::OnAuthFlowURLChange( |
| 92 const GURL& redirect_url) { |
| 93 if (redirect_url.GetWithEmptyPath() == final_url_prefix_) { |
| 94 SetResult(base::MakeUnique<base::StringValue>(redirect_url.spec())); |
| 95 SendResponse(true); |
| 96 if (auth_flow_) |
| 97 auth_flow_.release()->DetachDelegateAndDelete(); |
| 98 Release(); // Balanced in RunAsync. |
| 99 } |
| 100 } |
| 101 |
| 102 } // namespace extensions |
| OLD | NEW |