Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/instance_id/instance_id_api.h" | 5 #include "chrome/browser/extensions/api/instance_id/instance_id_api.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/profiles/profile.h" | |
| 9 #include "chrome/browser/services/gcm/instance_id/instance_id_profile_service.h" | |
| 10 #include "chrome/browser/services/gcm/instance_id/instance_id_profile_service_fa ctory.h" | |
| 11 #include "chrome/common/extensions/api/instance_id.h" | |
| 12 #include "components/gcm_driver/instance_id/instance_id_driver.h" | |
| 8 #include "extensions/common/extension.h" | 13 #include "extensions/common/extension.h" |
| 9 | 14 |
| 10 namespace extensions { | 15 namespace extensions { |
| 11 | 16 |
| 17 namespace { | |
| 18 | |
| 19 // Error messages. | |
| 20 const char kInvalidParameter[] = "Function was called with invalid parameters."; | |
| 21 const char kDisabled[] = "Instance ID is currently disabled."; | |
| 22 const char kNetworkError[] = "Network error occurred."; | |
| 23 const char kServerError[] = "Server error occurred."; | |
| 24 const char kUnknownError[] = "Unknown error occurred."; | |
| 25 | |
| 26 const char* InstanceIDResultToError(instance_id::InstanceID::Result result) { | |
| 27 switch (result) { | |
| 28 case instance_id::InstanceID::INVALID_PARAMETER: | |
| 29 return kInvalidParameter; | |
| 30 case instance_id::InstanceID::DISABLED: | |
| 31 return kDisabled; | |
| 32 case instance_id::InstanceID::NETWORK_ERROR: | |
| 33 return kNetworkError; | |
| 34 case instance_id::InstanceID::SERVER_ERROR: | |
| 35 return kServerError; | |
| 36 case instance_id::InstanceID::UNKNOWN_ERROR: | |
| 37 return kUnknownError; | |
| 38 default: | |
| 39 NOTREACHED() << "Unexpected value of result cannot be converted: " | |
| 40 << result; | |
| 41 } | |
| 42 return ""; | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 InstanceIDApiFunction::InstanceIDApiFunction() { | |
| 48 } | |
| 49 | |
| 50 InstanceIDApiFunction::~InstanceIDApiFunction() { | |
| 51 } | |
| 52 | |
| 53 ExtensionFunction::ResponseAction InstanceIDApiFunction::Run() { | |
| 54 if (Profile::FromBrowserContext(browser_context())->IsOffTheRecord()) { | |
| 55 return RespondNow(Error( | |
| 56 "chrome.instanceID not supported in incognito mode")); | |
| 57 } | |
| 58 | |
| 59 if (!IsEnabled()) { | |
| 60 return RespondNow(Error( | |
| 61 InstanceIDResultToError(instance_id::InstanceID::DISABLED))); | |
| 62 } | |
| 63 | |
| 64 return DoWork(); | |
| 65 } | |
| 66 | |
| 67 bool InstanceIDApiFunction::IsEnabled() const { | |
| 68 Profile* profile = Profile::FromBrowserContext(browser_context()); | |
| 69 | |
| 70 return instance_id::InstanceIDProfileService::IsInstanceIDEnabled(profile); | |
| 71 } | |
| 72 | |
| 73 instance_id::InstanceID* InstanceIDApiFunction::GetInstanceID() const { | |
| 74 return instance_id::InstanceIDProfileServiceFactory::GetForProfile( | |
| 75 Profile::FromBrowserContext(browser_context()))->driver()-> | |
| 76 GetInstanceID(extension()->id()); | |
| 77 } | |
| 78 | |
| 12 InstanceIDGetIDFunction::InstanceIDGetIDFunction() {} | 79 InstanceIDGetIDFunction::InstanceIDGetIDFunction() {} |
| 13 | 80 |
| 14 InstanceIDGetIDFunction::~InstanceIDGetIDFunction() {} | 81 InstanceIDGetIDFunction::~InstanceIDGetIDFunction() {} |
| 15 | 82 |
| 16 ExtensionFunction::ResponseAction InstanceIDGetIDFunction::Run() { | 83 ExtensionFunction::ResponseAction InstanceIDGetIDFunction::DoWork() { |
| 17 NOTIMPLEMENTED(); | 84 return RespondNow( |
| 18 return RespondLater(); | 85 OneArgument(new base::StringValue(GetInstanceID()->GetID()))); |
| 19 } | 86 } |
| 20 | 87 |
| 21 InstanceIDGetCreationTimeFunction::InstanceIDGetCreationTimeFunction() {} | 88 InstanceIDGetCreationTimeFunction::InstanceIDGetCreationTimeFunction() {} |
| 22 | 89 |
| 23 InstanceIDGetCreationTimeFunction::~InstanceIDGetCreationTimeFunction() {} | 90 InstanceIDGetCreationTimeFunction::~InstanceIDGetCreationTimeFunction() {} |
| 24 | 91 |
| 25 ExtensionFunction::ResponseAction InstanceIDGetCreationTimeFunction::Run() { | 92 ExtensionFunction::ResponseAction InstanceIDGetCreationTimeFunction::DoWork() { |
| 26 NOTIMPLEMENTED(); | 93 return RespondNow(OneArgument( |
| 27 return RespondLater(); | 94 new base::FundamentalValue( |
| 95 GetInstanceID()->GetCreationTime().ToDoubleT()))); | |
| 28 } | 96 } |
| 29 | 97 |
| 30 InstanceIDGetTokenFunction::InstanceIDGetTokenFunction() {} | 98 InstanceIDGetTokenFunction::InstanceIDGetTokenFunction() {} |
| 31 | 99 |
| 32 InstanceIDGetTokenFunction::~InstanceIDGetTokenFunction() {} | 100 InstanceIDGetTokenFunction::~InstanceIDGetTokenFunction() {} |
| 33 | 101 |
| 34 ExtensionFunction::ResponseAction InstanceIDGetTokenFunction::Run() { | 102 ExtensionFunction::ResponseAction InstanceIDGetTokenFunction::DoWork() { |
| 35 NOTIMPLEMENTED(); | 103 scoped_ptr<api::instance_id::GetToken::Params> params = |
| 104 api::instance_id::GetToken::Params::Create(*args_); | |
| 105 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 106 | |
| 107 std::map<std::string, std::string> options; | |
| 108 if (params->get_token_params.options.get()) { | |
| 109 base::DictionaryValue::Iterator iter( | |
| 110 params->get_token_params.options->additional_properties); | |
| 111 for (; !iter.IsAtEnd(); iter.Advance()) { | |
| 112 const base::StringValue* string_value = NULL; | |
| 113 if (!iter.value().GetAsString(&string_value)) { | |
| 114 return RespondNow(Error(InstanceIDResultToError( | |
| 115 instance_id::InstanceID::INVALID_PARAMETER))); | |
| 116 } | |
| 117 options[iter.key()] = string_value->GetString(); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 GetInstanceID()->GetToken( | |
| 122 params->get_token_params.authorized_entity, | |
| 123 params->get_token_params.scope, | |
| 124 options, | |
| 125 base::Bind(&InstanceIDGetTokenFunction::GetTokenCompleted, this)); | |
| 126 | |
| 36 return RespondLater(); | 127 return RespondLater(); |
| 37 } | 128 } |
| 38 | 129 |
| 130 void InstanceIDGetTokenFunction::GetTokenCompleted( | |
| 131 instance_id::InstanceID* instance_id, | |
|
fgorski
2015/05/07 19:59:53
you are completely ignoring this one. Would it mak
jianli
2015/05/07 20:47:01
Indeed I has removed this from my other patch that
| |
| 132 const std::string& token, | |
| 133 instance_id::InstanceID::Result result) { | |
| 134 if (result == instance_id::InstanceID::SUCCESS) | |
| 135 Respond(OneArgument(new base::StringValue(token))); | |
| 136 else | |
| 137 Respond(Error(InstanceIDResultToError(result))); | |
| 138 } | |
| 139 | |
| 39 InstanceIDDeleteTokenFunction::InstanceIDDeleteTokenFunction() {} | 140 InstanceIDDeleteTokenFunction::InstanceIDDeleteTokenFunction() {} |
| 40 | 141 |
| 41 InstanceIDDeleteTokenFunction::~InstanceIDDeleteTokenFunction() {} | 142 InstanceIDDeleteTokenFunction::~InstanceIDDeleteTokenFunction() {} |
| 42 | 143 |
| 43 ExtensionFunction::ResponseAction InstanceIDDeleteTokenFunction::Run() { | 144 ExtensionFunction::ResponseAction InstanceIDDeleteTokenFunction::DoWork() { |
| 44 NOTIMPLEMENTED(); | 145 scoped_ptr<api::instance_id::DeleteToken::Params> params = |
| 146 api::instance_id::DeleteToken::Params::Create(*args_); | |
| 147 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 148 | |
| 149 GetInstanceID()->DeleteToken( | |
| 150 params->delete_token_params.authorized_entity, | |
| 151 params->delete_token_params.scope, | |
| 152 base::Bind(&InstanceIDDeleteTokenFunction::DeleteTokenCompleted, this)); | |
| 153 | |
| 45 return RespondLater(); | 154 return RespondLater(); |
| 46 } | 155 } |
| 47 | 156 |
| 157 void InstanceIDDeleteTokenFunction::DeleteTokenCompleted( | |
| 158 instance_id::InstanceID* instance_id, | |
| 159 instance_id::InstanceID::Result result) { | |
| 160 if (result == instance_id::InstanceID::SUCCESS) | |
| 161 Respond(NoArguments()); | |
| 162 else | |
| 163 Respond(Error(InstanceIDResultToError(result))); | |
| 164 } | |
| 165 | |
| 48 InstanceIDDeleteIDFunction::InstanceIDDeleteIDFunction() {} | 166 InstanceIDDeleteIDFunction::InstanceIDDeleteIDFunction() {} |
| 49 | 167 |
| 50 InstanceIDDeleteIDFunction::~InstanceIDDeleteIDFunction() {} | 168 InstanceIDDeleteIDFunction::~InstanceIDDeleteIDFunction() {} |
| 51 | 169 |
| 52 ExtensionFunction::ResponseAction InstanceIDDeleteIDFunction::Run() { | 170 ExtensionFunction::ResponseAction InstanceIDDeleteIDFunction::DoWork() { |
| 53 NOTIMPLEMENTED(); | 171 GetInstanceID()->DeleteID( |
| 172 base::Bind(&InstanceIDDeleteIDFunction::DeleteIDCompleted, this)); | |
| 173 | |
| 54 return RespondLater(); | 174 return RespondLater(); |
| 55 } | 175 } |
| 56 | 176 |
| 177 void InstanceIDDeleteIDFunction::DeleteIDCompleted( | |
| 178 instance_id::InstanceID* instance_id, | |
| 179 instance_id::InstanceID::Result result) { | |
| 180 if (result == instance_id::InstanceID::SUCCESS) | |
| 181 Respond(NoArguments()); | |
| 182 else | |
| 183 Respond(Error(InstanceIDResultToError(result))); | |
| 184 } | |
| 185 | |
| 57 } // namespace extensions | 186 } // namespace extensions |
| OLD | NEW |