Skip to content

Instantly share code, notes, and snippets.

@jl2
Created May 31, 2013 22:39
Show Gist options
  • Select an option

  • Save jl2/5688411 to your computer and use it in GitHub Desktop.

Select an option

Save jl2/5688411 to your computer and use it in GitHub Desktop.
Download the latest version of Chromium for Windows using C++ and libCurl. Very similar to the C version.
/*
* main.cpp
* Copyright (c) 2013, Jeremiah LaRocco [email protected]
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <curl/curl.h>
int progress_function(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) {
int num = 50 * (dlnow/dltotal);
std::string progBar;
progBar.reserve(50);
for (auto i=0; i < 50; ++i) {
progBar += (i<num)?"#":" ";
}
std::cout << "|" << progBar << "| " << int(dlnow) << " of " << int(dltotal) << "\r";
return 0;
}
const std::string download_to_memory(CURL *curl, std::string url, bool showProgress = false) {
CURLcode res;
if (curl == NULL) return false;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
if (showProgress) {
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_function);
}
std::string oStr;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, 0);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
[&oStr](void *buffer, size_t size, size_t nmemb, void *) {
size_t thisSize = size * nmemb;
oStr.reserve(oStr.size() + thisSize);
std::copy((uint8_t*)buffer, (uint8_t*)buffer + thisSize, std::back_inserter(oStr));
return thisSize;
});
// Do the download
res = curl_easy_perform(curl);
if (res != 0) {
std::cout << "An error occured while downloading \"" << url << "\"!\nres = " << res << "\n";
oStr.clear();
return "";
}
return oStr;
}
bool download_to_file(CURL *curl, const std::string &url, const std::string &fname, bool showProgress = true) {
CURLcode res;
if (curl == NULL) return false;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
if (showProgress) {
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_function);
}
FILE *outf = std::fopen(fname.c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, outf);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
res = curl_easy_perform(curl);
if (res != 0) {
std::cout << "An error occured while downloading \"" << url << "\"!\nres = " << res << "\n";
return false;
}
std::fclose(outf);
return true;
}
void initializeLibs(CURL **curl) {
// Initialize cUrl
curl_global_init(CURL_GLOBAL_DEFAULT);
*curl = curl_easy_init();
}
void cleanupLibs(CURL *curl) {
curl_easy_cleanup(curl);
curl_global_cleanup();
}
int main(int argc, char *argv[]) {
CURL *curl = NULL;
initializeLibs(&curl);
std::cout << "Fetching latest Chromium build ID.\n";
const std::string version = download_to_memory(curl,
"http://commondatastorage.googleapis.com/chromium-browser-continuous/Win/LAST_CHANGE", version);
if (version.size() == 0) {
std::cout << "Could not fetch latest version number!\nAborting\n";
cleanupLibs(curl);
exit(1);
}
std::cout << "Downloading Chromium build: " << version << "\n";
std::string installerFile = std::string(getenv("TEMP")) + "/mini_installer.exe";
std::string url = "http://commondatastorage.googleapis.com/chromium-browser-continuous/Win/" + version + "/mini_installer.exe";
if (!download_to_file(curl, url, installerFile)) {
std::cout << "\nCould not download version " << version << " of Chromium.\n";
cleanupLibs(curl);
exit(1);
}
std::cout << "\nInstalling Chromium build " << version << "...\n";
system(installerFile.c_str());
std::cout << "Done.\n";
cleanupLibs(curl);
return 0;
}
uchrome.exe: main.cpp Makefile
g++ -Wall -O3 -m64 -std=c++11 -I d:\mingw\include -o uchrome.exe main.cpp -Ld:/mingw/bin -lcurl -lws2_32 -lwinmm -Ld:/mingw/lib64 -lssl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment