id_generator_custom_bindings.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2014 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. #include "extensions/renderer/id_generator_custom_bindings.h"
  5. #include <stdint.h>
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "extensions/renderer/script_context.h"
  9. #include "v8/include/v8-function-callback.h"
  10. namespace extensions {
  11. IdGeneratorCustomBindings::IdGeneratorCustomBindings(ScriptContext* context)
  12. : ObjectBackedNativeHandler(context) {}
  13. void IdGeneratorCustomBindings::AddRoutes() {
  14. RouteHandlerFunction(
  15. "GetNextId", base::BindRepeating(&IdGeneratorCustomBindings::GetNextId,
  16. base::Unretained(this)));
  17. RouteHandlerFunction(
  18. "GetNextScopedId",
  19. base::BindRepeating(&IdGeneratorCustomBindings::GetNextScopedId,
  20. base::Unretained(this)));
  21. }
  22. void IdGeneratorCustomBindings::GetNextId(
  23. const v8::FunctionCallbackInfo<v8::Value>& args) {
  24. // Make sure 0 is never returned because some APIs (particularly WebRequest)
  25. // have special meaning for 0 IDs.
  26. static int32_t next_id = 1;
  27. args.GetReturnValue().Set(next_id++);
  28. }
  29. void IdGeneratorCustomBindings::GetNextScopedId(
  30. const v8::FunctionCallbackInfo<v8::Value>& args) {
  31. args.GetReturnValue().Set(context()->GetNextIdFromCounter());
  32. }
  33. } // namespace extensions