Repository: electron/electron
File: shell/browser/api/electron_api_web_contents.cc (C++)

Code before:
      v8::Isolate* isolate,
      const scoped_refptr<content::DevToolsAgentHost>& val) {
    gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
    dict.Set("id", val->GetId());
    dict.Set("url", val->GetURL().spec());
    return dict.GetHandle();
  }
};

template <>
struct Converter<content::NavigationEntry*> {
  static bool FromV8(v8::Isolate* isolate,
                     v8::Local<v8::Value> val,
                     content::NavigationEntry** out) {
    gin_helper::Dictionary dict;
    if (!gin::ConvertFromV8(isolate, val, &dict))
      return false;

    std::string url_str;
    std::string title;
    std::string encoded_page_state;

    if (!dict.Get("url", &url_str) || !dict.Get("title", &title))
      return false;

    GURL url(url_str);
    if (!url.is_valid())
      return false;

    // Create entry with validated URL
    auto entry = content::NavigationEntry::Create();
    entry->SetURL(url);
    entry->SetTitle(base::UTF8ToUTF16(title));

    // Handle optional page state
    if (dict.Get("pageState", &encoded_page_state)) {
      std::string decoded_page_state;
      if (base::Base64Decode(encoded_page_state, &decoded_page_state)) {
        auto restore_context = content::NavigationEntryRestoreContext::Create();

        auto page_state =
            blink::PageState::CreateFromEncodedData(decoded_page_state);
        if (!page_state.IsValid())
          return false;

        entry->SetPageState(std::move(page_state), restore_context.get());
      }
    }

    *out = entry.release();
    return true;

Review comment (suggestion):
Pretty sure we have a `GURL` converter already somewhere, saves the duplication if we use that

Code after:
  static v8::Local<v8::Value> ToV8(
      v8::Isolate* isolate,
      const scoped_refptr<content::DevToolsAgentHost>& val) {
    gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
    dict.Set("id", val->GetId());
    dict.Set("url", val->GetURL().spec());
    return dict.GetHandle();
  }
};

template <>
struct Converter<content::NavigationEntry*> {
  static bool FromV8(v8::Isolate* isolate,
                     v8::Local<v8::Value> val,
                     content::NavigationEntry** out) {
    gin_helper::Dictionary dict;
    if (!gin::ConvertFromV8(isolate, val, &dict))
      return false;

    std::string url_str;
    std::string title;
    std::string encoded_page_state;
    GURL url;

    if (!dict.Get("url", &url) || !dict.Get("title", &title))
      return false;

    auto entry = content::NavigationEntry::Create();
    entry->SetURL(url);
    entry->SetTitle(base::UTF8ToUTF16(title));

    // Handle optional page state
    if (dict.Get("pageState", &encoded_page_state)) {
      std::string decoded_page_state;
      if (base::Base64Decode(encoded_page_state, &decoded_page_state)) {
        auto restore_context = content::NavigationEntryRestoreContext::Create();

        auto page_state =
            blink::PageState::CreateFromEncodedData(decoded_page_state);
        if (!page_state.IsValid())
          return false;

        entry->SetPageState(std::move(page_state), restore_context.get());
      }
    }

    *out = entry.release();
    return true;
  }

  static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,