| 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/extensions/extension_chrome_auth_private_api.h" | 5 #include "chrome/browser/extensions/extension_chrome_auth_private_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" | 10 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" |
| 11 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.
h" | 11 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.
h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 bool IsCloudPrintEnableURL(Profile* profile, const GURL& url) { | |
| 17 ExtensionService* service = profile->GetExtensionService(); | |
| 18 const Extension* cloud_print_app = service->GetExtensionById( | |
| 19 extension_misc::kCloudPrintAppId, false); | |
| 20 if (!cloud_print_app) { | |
| 21 #if !defined(OS_CHROMEOS) | |
| 22 NOTREACHED(); | |
| 23 #endif // !defined(OS_CHROMEOS) | |
| 24 return false; | |
| 25 } | |
| 26 return (service->extensions()->GetHostedAppByURL(ExtensionURLInfo(url)) == | |
| 27 cloud_print_app); | |
| 28 } | |
| 29 | |
| 30 bool test_mode = false; | 16 bool test_mode = false; |
| 31 | 17 |
| 32 const char kAccessDeniedError[] = | |
| 33 "Cannot call this API from a non-cloudprint URL."; | |
| 34 } // namespace | 18 } // namespace |
| 35 | 19 |
| 36 SetCloudPrintCredentialsFunction::SetCloudPrintCredentialsFunction() { | 20 SetCloudPrintCredentialsFunction::SetCloudPrintCredentialsFunction() { |
| 37 } | 21 } |
| 38 | 22 |
| 39 SetCloudPrintCredentialsFunction::~SetCloudPrintCredentialsFunction() { | 23 SetCloudPrintCredentialsFunction::~SetCloudPrintCredentialsFunction() { |
| 40 } | 24 } |
| 41 | 25 |
| 42 bool SetCloudPrintCredentialsFunction::RunImpl() { | 26 bool SetCloudPrintCredentialsFunction::RunImpl() { |
| 43 // This has to be called from the specific cloud print app. | |
| 44 if (!IsCloudPrintEnableURL(profile_, source_url())) { | |
| 45 error_ = kAccessDeniedError; | |
| 46 return false; | |
| 47 } | |
| 48 | |
| 49 std::string user_email; | 27 std::string user_email; |
| 50 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &user_email)); | 28 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &user_email)); |
| 51 std::string robot_email; | 29 std::string robot_email; |
| 52 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &robot_email)); | 30 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &robot_email)); |
| 53 std::string credentials; | 31 std::string credentials; |
| 54 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &credentials)); | 32 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &credentials)); |
| 55 if (test_mode) { | 33 if (test_mode) { |
| 56 std::string test_response = user_email; | 34 std::string test_response = user_email; |
| 57 test_response.append(robot_email); | 35 test_response.append(robot_email); |
| 58 test_response.append(credentials); | 36 test_response.append(credentials); |
| 59 result_.reset(Value::CreateStringValue(test_response)); | 37 result_.reset(Value::CreateStringValue(test_response)); |
| 60 } else { | 38 } else { |
| 61 CloudPrintProxyServiceFactory::GetForProfile(profile_)-> | 39 CloudPrintProxyServiceFactory::GetForProfile(profile_)-> |
| 62 EnableForUserWithRobot(credentials, robot_email, user_email); | 40 EnableForUserWithRobot(credentials, robot_email, user_email); |
| 63 } | 41 } |
| 64 SendResponse(true); | 42 SendResponse(true); |
| 65 return true; | 43 return true; |
| 66 } | 44 } |
| 67 | 45 |
| 68 // static | 46 // static |
| 69 void SetCloudPrintCredentialsFunction::SetTestMode(bool test_mode_enabled) { | 47 void SetCloudPrintCredentialsFunction::SetTestMode(bool test_mode_enabled) { |
| 70 test_mode = test_mode_enabled; | 48 test_mode = test_mode_enabled; |
| 71 } | 49 } |
| OLD | NEW |