<commit_before>/*
 * Copyright (C) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.explorer.client.base.http.crossdomain;

import com.google.api.explorer.client.base.ApiRequest;
import com.google.api.explorer.client.base.ApiResponse;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.ScriptElement;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Builds requests to make to an cross-domain proxy iframe.
 *
 * <p>
 * This consists of adding a <script> tag to the page to load the JS library to
 * make cross-domain requests to the server.
 * </p>
 *
 * @author <EMAIL> (<NAME>)
 */
public class CrossDomainRequestBuilder {

  private static final String JS_CLIENT_NAME = "ae_f85e3bd0744c7080861c3ae42085d071.js";
  private static final String JS_CLIENT_URL = "https://ssl.gstatic.com/gb/js/" + JS_CLIENT_NAME;

  private int timeoutMillis;

  private static native boolean scriptLoaded() /*-{
    return !!$wnd.googleapis && !!$wnd.googleapis.newHttpRequest;
  }-*/;

  public void setTimeoutMillis(int timeoutMillis) {
    this.timeoutMillis = timeoutMillis;
  }

  public CrossDomainRequest makeRequest(
      final ApiRequest request, AsyncCallback<ApiResponse> callback) {
    final CrossDomainRequest xdr = new CrossDomainRequest(callback, timeoutMillis);

    /** Adds a script tag to the page to load the JS library used to make requests. */
    if (!scriptLoaded()) {
      ScriptElement script = Document.get().createScriptElement();
      script.setSrc(JS_CLIENT_URL);
      Document.get().getElementsByTagName("head").getItem(0).appendChild(script);

      new Timer() {
        @Override
        public void run() {
          if (scriptLoaded()) {
            setBaseUrl();
            doMakeRequest(request, xdr);
            cancel();
          }
        }
      }.scheduleRepeating(100);
    } else {
      doMakeRequest(request, xdr);
    }
    return xdr;
  }

  private <T> void doMakeRequest(ApiRequest request, final CrossDomainRequest xdr) {
    JavaScriptObject jso = CrossDomainRequest.convertRequest(request);
    xdr.sendRequest(jso);
  }

  private static native void setBaseUrl() /*-{
    var proxy = @com.google.api.explorer.client.base.Config::baseUrl + '/static/proxy.html';
    $wnd['__GOOGLEAPIS'] = {
      'googleapis.config': {
        'proxy': proxy,
        'gcv':
            @com.google.api.explorer.client.base.http.crossdomain.CrossDomainRequestBuilder::JS_CLIENT_NAME
      }
    };
  }-*/;
}<commit_msg>Use GWT 2.4's ScriptInjector instead of injecting scripts ourselves.<commit_after>/*
 * Copyright (C) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.explorer.client.base.http.crossdomain;

import com.google.api.explorer.client.base.ApiRequest;
import com.google.api.explorer.client.base.ApiResponse;
import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Builds requests to make to an cross-domain proxy iframe.
 *
 * <p>
 * This consists of adding a <script> tag to the page to load the JS library to
 * make cross-domain requests to the server.
 * </p>
 *
 * @author <EMAIL> (<NAME>)
 */
public class CrossDomainRequestBuilder {

  private static final String JS_CLIENT_NAME = "ae_f85e3bd0744c7080861c3ae42085d071.js";
  private static final String JS_CLIENT_URL = "https://ssl.gstatic.com/gb/js/" + JS_CLIENT_NAME;

  private int timeoutMillis;

  private static native boolean scriptLoaded() /*-{
    return !!$wnd.googleapis && !!$wnd.googleapis.newHttpRequest;
  }-*/;

  public void setTimeoutMillis(int timeoutMillis) {
    this.timeoutMillis = timeoutMillis;
  }

  public CrossDomainRequest makeRequest(
      final ApiRequest request, AsyncCallback<ApiResponse> callback) {
    final CrossDomainRequest xdr = new CrossDomainRequest(callback, timeoutMillis);

    /** Adds a script tag to the page to load the JS library used to make requests. */
    if (!scriptLoaded()) {
      ScriptInjector.fromUrl(JS_CLIENT_URL).setCallback(new Callback<Void, Exception>() {
        public void onSuccess(Void result) {
          setBaseUrl();
          doMakeRequest(request, xdr);
        }

        public void onFailure(Exception e) {
          throw new RuntimeException(e);
        }
      }).inject();
    } else {
      doMakeRequest(request, xdr);
    }
    return xdr;
  }

  private <T> void doMakeRequest(ApiRequest request, final CrossDomainRequest xdr) {
    JavaScriptObject jso = CrossDomainRequest.convertRequest(request);
    xdr.sendRequest(jso);
  }

  private static native void setBaseUrl() /*-{
    var proxy = @com.google.api.explorer.client.base.Config::baseUrl + '/static/proxy.html';
    $wnd['__GOOGLEAPIS'] = {
      'googleapis.config': {
        'proxy': proxy,
        'gcv':
            @com.google.api.explorer.client.base.http.crossdomain.CrossDomainRequestBuilder::JS_CLIENT_NAME
      }
    };
  }-*/;
}
{'fraction_non_alphanumeric': 0.10855476344782891, 'fraction_numerical': 0.01101749837977965, 'mean_word_length': 3.499271137026239, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7254147', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1806, 'n_words': 508}