<commit_before>/*
 * Copyright 2010-2011 <NAME> (<EMAIL>)
 *
 * 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.
 */

function ChromeListeners() { 
  this.detached_tab = null;
  this.enabled = false;
  
  this.enable = function() {
    if (! this.enabled) {
      this.enabled = true;
      chrome.tabs.onUpdated.addListener(this.onTabUpdated);
      chrome.tabs.onCreated.addListener(this.onTabCreated);
      chrome.tabs.onRemoved.addListener(this.onTabRemoved);
      chrome.tabs.onDetached.addListener(this.onTabDetached);
      chrome.tabs.onAttached.addListener(this.onTabAttached);
    }
  };
  
  this.disable = function() {
    if (this.enabled) {
      this.enabled = false;
      chrome.tabs.onUpdated.removeListener(this.onTabUpdated);
      chrome.tabs.onCreated.removeListener(this.onTabCreated);
      chrome.tabs.onRemoved.removeListener(this.onTabRemoved);
      chrome.tabs.onDetached.removeListener(this.onTabDetached);
      chrome.tabs.onAttached.removeListener(this.onTabAttached);
    }
  };
  
  this.onTabUpdated = function(tab_id, change_info, tab) {
    if(change_info.url != undefined) {
      supertabs.updateTab(tab.windowId, tab.id, tab.url);
    }
  };
  
  this.onTabCreated = function(tab) {
    supertabs.addTab(tab.windowId, tab.id, tab.index, tab.url);
  };
  
  this.onTabRemoved = function(tab_id) {
    supertabs.deleteTab(tab_id)
  };
  
  this.onTabDetached = function(tab_id, detach_info) {
    this.detached_tab = supertabs.deleteTab(tab_id);
  };
  
  this.onTabAttached = function(tab_id, attach_info) {
    supertabs.addTab(attach_info.newWindowId,
        tab_id,
        attach_info.newPosition,
        this.detached_tab.url);
  };
}


function extensionRequestsHandler(request, sender, sendResponse) {
  var handlers = new Object();
  handlers['get_supertabs'] = getSupertabsRequestHandler;
  handlers['open_supertab'] = openSupertabRequestHandler;
  handlers['validate_login'] = loginRequestHandler;
  handlers['logout'] = logoutRequestHandler;

  handlers[request.request_type](request, sender, sendResponse);
}

function getSupertabsRequestHandler(request, sender, sendResponse) {
  sendResponse(supertabs_api.getJsonifiable());
}

function openSupertabRequestHandler(request, sender, sendResponse) {
  openSupertab(request.supertab_id);
}

function loginRequestHandler(request, sender, sendResponse) {
  supertabs_api.login(request.args.username, request.args.password,
      function(response, credentials, error, msg) {
        if (error == null) {
          supertabs_api.pull_all_tabs(function(response, credentials, error, msg) {
            console.log(JSON.stringify(response));
            if (response.supertabs.length > 0) {
              localStorage.setItem("original_tabs",
                JSON.stringify(supertabs_api.supertabs.getJSONObject()));
              supertabs_api.supertabs.supertabs = new Array();
              supertabs_api.supertabs.fromJSONObject(response);
            } else {
              supertabs_api.push_all_tabs(supertabs_api.supertabs.getJSONObject());
            }
            listeners.enable();
            sendResponse({success : true});
          });
        } else {
          sendResponse({success : false});
        }
      });
}

function logoutRequestHandler(request, sender, sendResponse) {
  supertabs_api.logout(function() {
    chrome.windows.getAll({populate: false}, function(windows) {
      supertabs_api.supertabs.supertabs = new Array();
      supertabs_api.supertabs.fromJSONObject(JSON.parse(localStorage.getItem("original_tabs")));
      for (var i = 0; i < supertabs_api.supertabs.supertabs.length; i++) {
        openSupertab(supertabs_api.supertabs.supertabs[i].supertab_id);
      }
      for (var i = 0; i < windows.length; i++) {
        chrome.windows.remove(windows[i].windowId);
      }
      sendResponse();
    });
  });
}

function openSupertab(id) {
  var s = supertabs.getSupertabBySupertabId(id);
  if (s == null || s.open) {
    return;
  }

  listeners.disable();

  // Open the first tab
  create_data = new Object;
  create_data.url = s.tabs[0].url;

  chrome.windows.create(create_data,
      function(window) {
        supertabs.updateWindowChromeId(id, window.id);

        // Unfortunately, the window variable does not list the tabs
        // So we have to request them
        chrome.tabs.getAllInWindow(window.id,
          function(tabs) {
            // tabCreation will update the current chrome ids
            // and open the next tab
            tabCreation(tabs[0], id, s.tabs[0].supertab_id, 0);
          });
      });<commit_msg>Fixed the opening of original tabs at log out.<commit_after>/*
 * Copyright 2010-2011 <NAME> (<EMAIL>)
 *
 * 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.
 */

function ChromeListeners() { 
  this.detached_tab = null;
  this.enabled = false;
  
  this.enable = function() {
    if (! this.enabled) {
      this.enabled = true;
      chrome.tabs.onUpdated.addListener(this.onTabUpdated);
      chrome.tabs.onCreated.addListener(this.onTabCreated);
      chrome.tabs.onRemoved.addListener(this.onTabRemoved);
      chrome.tabs.onDetached.addListener(this.onTabDetached);
      chrome.tabs.onAttached.addListener(this.onTabAttached);
    }
  };
  
  this.disable = function() {
    if (this.enabled) {
      this.enabled = false;
      chrome.tabs.onUpdated.removeListener(this.onTabUpdated);
      chrome.tabs.onCreated.removeListener(this.onTabCreated);
      chrome.tabs.onRemoved.removeListener(this.onTabRemoved);
      chrome.tabs.onDetached.removeListener(this.onTabDetached);
      chrome.tabs.onAttached.removeListener(this.onTabAttached);
    }
  };
  
  this.onTabUpdated = function(tab_id, change_info, tab) {
    if(change_info.url != undefined) {
      supertabs.updateTab(tab.windowId, tab.id, tab.url);
    }
  };
  
  this.onTabCreated = function(tab) {
    supertabs.addTab(tab.windowId, tab.id, tab.index, tab.url);
  };
  
  this.onTabRemoved = function(tab_id) {
    supertabs.deleteTab(tab_id)
  };
  
  this.onTabDetached = function(tab_id, detach_info) {
    this.detached_tab = supertabs.deleteTab(tab_id);
  };
  
  this.onTabAttached = function(tab_id, attach_info) {
    supertabs.addTab(attach_info.newWindowId,
        tab_id,
        attach_info.newPosition,
        this.detached_tab.url);
  };
}


function extensionRequestsHandler(request, sender, sendResponse) {
  var handlers = new Object();
  handlers['get_supertabs'] = getSupertabsRequestHandler;
  handlers['open_supertab'] = openSupertabRequestHandler;
  handlers['validate_login'] = loginRequestHandler;
  handlers['logout'] = logoutRequestHandler;

  handlers[request.request_type](request, sender, sendResponse);
}

function getSupertabsRequestHandler(request, sender, sendResponse) {
  sendResponse(supertabs_api.getJsonifiable());
}

function openSupertabRequestHandler(request, sender, sendResponse) {
  openSupertab(request.supertab_id);
}

function loginRequestHandler(request, sender, sendResponse) {
  supertabs_api.login(request.args.username, request.args.password,
      function(response, credentials, error, msg) {
        if (error == null) {
          supertabs_api.pull_all_tabs(function(response, credentials, error, msg) {
            localStorage.setItem("original_tabs",
              JSON.stringify(supertabs_api.supertabs.getJSONObject()));
            if (response.supertabs.length > 0) {
              supertabs_api.supertabs.supertabs = new Array();
              supertabs_api.supertabs.fromJSONObject(response);
            } else {
              supertabs_api.push_all_tabs(supertabs_api.supertabs.getJSONObject());
            }
            listeners.enable();
            sendResponse({success : true});
          });
        } else {
          sendResponse({success : false});
        }
      });
}

function logoutRequestHandler(request, sender, sendResponse) {
  supertabs_api.logout(function() {
    chrome.windows.getAll({populate: false}, function(windows) {
      supertabs_api.supertabs.supertabs = new Array();
      supertabs_api.supertabs.fromJSONObject(JSON.parse(localStorage.getItem("original_tabs")));
      for (var i = 0; i < supertabs_api.supertabs.supertabs.length; i++) {
        openSupertab(supertabs_api.supertabs.supertabs[i].supertab_id);
      }
      for (var i = 0; i < windows.length; i++) {
        chrome.windows.remove(windows[i].id);
      }
      sendResponse();
    });
  });
}

function openSupertab(id) {
  var s = supertabs.getSupertabBySupertabId(id);
  if (s == null || s.open) {
    return;
  }

  listeners.disable();

  // Open the first tab
  create_data = new Object;
  create_data.url = s.tabs[0].url;

  chrome.windows.create(create_data,
      function(window) {
        supertabs.updateWindowChromeId(id, window.id);

        // Unfortunately, the window variable does not list the tabs
        // So we have to request them
        chrome.tabs.getAllInWindow(window.id,
          function(tabs) {
            // tabCreation will update the current chrome ids
            // and open the next tab
            tabCreation(tabs[0], id, s.tabs[0].supertab_id, 0);
          });
      });
{'fraction_non_alphanumeric': 0.11549073709105243, 'fraction_numerical': 0.003744580212849823, 'mean_word_length': 3.2375782881002086, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '2451078', 'n_tokens_mistral': 3166, 'n_tokens_neox': 3125, 'n_words': 589}