Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Unified Diff: components/gcm_driver/instance_id/instance_id_impl.cc

Issue 1113213002: Generate Instance ID locally. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address more feedback Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/gcm_driver/instance_id/instance_id_impl.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/gcm_driver/instance_id/instance_id_impl.cc
diff --git a/components/gcm_driver/instance_id/instance_id_impl.cc b/components/gcm_driver/instance_id/instance_id_impl.cc
index 4d76ad2286cde3fddb590f048170526d1b93b50c..1dfaab78ec9e5a95f1fd39eb076436f1ba1d6ca7 100644
--- a/components/gcm_driver/instance_id/instance_id_impl.cc
+++ b/components/gcm_driver/instance_id/instance_id_impl.cc
@@ -4,7 +4,12 @@
#include "components/gcm_driver/instance_id/instance_id_impl.h"
+#include <algorithm>
+#include "base/base64.h"
+#include "base/bind.h"
#include "base/logging.h"
+#include "base/message_loop/message_loop.h"
+#include "crypto/random.h"
namespace instance_id {
@@ -21,13 +26,12 @@ InstanceIDImpl::~InstanceIDImpl() {
}
std::string InstanceIDImpl::GetID() {
- NOTIMPLEMENTED();
- return std::string();
+ EnsureIDGenerated();
+ return id_;
}
base::Time InstanceIDImpl::GetCreationTime() {
- NOTIMPLEMENTED();
- return base::Time();
+ return creation_time_;
}
void InstanceIDImpl::GetToken(
@@ -45,7 +49,45 @@ void InstanceIDImpl::DeleteToken(const std::string& audience,
}
void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) {
- NOTIMPLEMENTED();
+ // TODO(jianli): Delete the ID from the store.
+ id_.clear();
+ creation_time_ = base::Time();
+
+ base::MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(callback, this, InstanceID::SUCCESS));
+}
+
+void InstanceIDImpl::EnsureIDGenerated() {
+ if (!id_.empty())
+ return;
+
+ // Now produce the ID in the following steps:
+
+ // 1) Generates the random number in 8 bytes which is required by the server.
+ // We don't want to be strictly cryptographically secure. The server might
+ // reject the ID if there is a conflict or problem.
+ uint8 bytes[kInstanceIDByteLength];
+ crypto::RandBytes(bytes, sizeof(bytes));
+
+ // 2) Transforms the first 4 bits to 0x7. Note that this is required by the
+ // server.
+ bytes[0] &= 0x0f;
+ bytes[0] |= 0x70;
+
+ // 3) Encode the value in Android-compatible base64 scheme:
+ // * URL safe: '/' replaced by '_' and '+' replaced by '-'.
+ // * No padding: any trailing '=' will be removed.
+ base::Base64Encode(
+ base::StringPiece(reinterpret_cast<const char*>(bytes), sizeof(bytes)),
+ &id_);
+ std::replace(id_.begin(), id_.end(), '+', '-');
+ std::replace(id_.begin(), id_.end(), '/', '_');
+ id_.erase(std::remove(id_.begin(), id_.end(), '='), id_.end());
+
+ creation_time_ = base::Time::Now();
+
+ // TODO(jianli): Save the ID to the store.
}
} // namespace instance_id
« no previous file with comments | « components/gcm_driver/instance_id/instance_id_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698