Add optional requestHeaders argument to autoUpdater.setFeedURL + allow calling mulitple times
This commit is contained in:
parent
c68ca16148
commit
c89d8b19b6
10 changed files with 70 additions and 33 deletions
|
@ -9,6 +9,7 @@
|
||||||
#include "atom/browser/native_window.h"
|
#include "atom/browser/native_window.h"
|
||||||
#include "atom/browser/window_list.h"
|
#include "atom/browser/window_list.h"
|
||||||
#include "atom/common/native_mate_converters/callback.h"
|
#include "atom/common/native_mate_converters/callback.h"
|
||||||
|
#include "atom/common/native_mate_converters/string_map_converter.h"
|
||||||
#include "atom/common/node_includes.h"
|
#include "atom/common/node_includes.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
#include "native_mate/object_template_builder.h"
|
#include "native_mate/object_template_builder.h"
|
||||||
|
@ -102,7 +103,7 @@ mate::Handle<AutoUpdater> AutoUpdater::Create(v8::Isolate* isolate) {
|
||||||
void AutoUpdater::BuildPrototype(
|
void AutoUpdater::BuildPrototype(
|
||||||
v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> prototype) {
|
v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> prototype) {
|
||||||
mate::ObjectTemplateBuilder(isolate, prototype)
|
mate::ObjectTemplateBuilder(isolate, prototype)
|
||||||
.SetMethod("setFeedURL", &auto_updater::AutoUpdater::SetFeedURL)
|
.SetMethod("_setFeedURL", &auto_updater::AutoUpdater::SetFeedURL)
|
||||||
.SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates)
|
.SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates)
|
||||||
.SetMethod("quitAndInstall", &AutoUpdater::QuitAndInstall);
|
.SetMethod("quitAndInstall", &AutoUpdater::QuitAndInstall);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,8 @@ void AutoUpdater::SetDelegate(Delegate* delegate) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !defined(OS_MACOSX) || defined(MAS_BUILD)
|
#if !defined(OS_MACOSX) || defined(MAS_BUILD)
|
||||||
void AutoUpdater::SetFeedURL(const std::string& url) {
|
void AutoUpdater::SetFeedURL(const std::string& url,
|
||||||
|
const HeaderMap& requestHeaders) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void AutoUpdater::CheckForUpdates() {
|
void AutoUpdater::CheckForUpdates() {
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#ifndef ATOM_BROWSER_AUTO_UPDATER_H_
|
#ifndef ATOM_BROWSER_AUTO_UPDATER_H_
|
||||||
#define ATOM_BROWSER_AUTO_UPDATER_H_
|
#define ATOM_BROWSER_AUTO_UPDATER_H_
|
||||||
|
|
||||||
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "base/macros.h"
|
#include "base/macros.h"
|
||||||
|
@ -42,11 +43,14 @@ class Delegate {
|
||||||
|
|
||||||
class AutoUpdater {
|
class AutoUpdater {
|
||||||
public:
|
public:
|
||||||
|
typedef std::map<std::string, std::string> HeaderMap;
|
||||||
|
|
||||||
// Gets/Sets the delegate.
|
// Gets/Sets the delegate.
|
||||||
static Delegate* GetDelegate();
|
static Delegate* GetDelegate();
|
||||||
static void SetDelegate(Delegate* delegate);
|
static void SetDelegate(Delegate* delegate);
|
||||||
|
|
||||||
static void SetFeedURL(const std::string& url);
|
static void SetFeedURL(const std::string& url,
|
||||||
|
const HeaderMap& requestHeaders);
|
||||||
static void CheckForUpdates();
|
static void CheckForUpdates();
|
||||||
static void QuitAndInstall();
|
static void QuitAndInstall();
|
||||||
|
|
||||||
|
|
|
@ -29,16 +29,22 @@ bool g_update_available = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void AutoUpdater::SetFeedURL(const std::string& feed) {
|
void AutoUpdater::SetFeedURL(const std::string& feed,
|
||||||
|
const HeaderMap& requestHeaders) {
|
||||||
|
Delegate* delegate = GetDelegate();
|
||||||
|
if (!delegate)
|
||||||
|
return;
|
||||||
|
|
||||||
|
NSURL* url = [NSURL URLWithString:base::SysUTF8ToNSString(feed)];
|
||||||
|
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];
|
||||||
|
|
||||||
|
for (auto&& it : requestHeaders) {
|
||||||
|
[urlRequest setValue:base::SysUTF8ToNSString(it.second)
|
||||||
|
forHTTPHeaderField:base::SysUTF8ToNSString(it.first)];
|
||||||
|
}
|
||||||
|
|
||||||
if (g_updater == nil) {
|
if (g_updater == nil) {
|
||||||
Delegate* delegate = GetDelegate();
|
|
||||||
if (!delegate)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Initialize the SQRLUpdater.
|
// Initialize the SQRLUpdater.
|
||||||
NSURL* url = [NSURL URLWithString:base::SysUTF8ToNSString(feed)];
|
|
||||||
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:url];
|
|
||||||
|
|
||||||
@try {
|
@try {
|
||||||
g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest];
|
g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest];
|
||||||
} @catch (NSException* error) {
|
} @catch (NSException* error) {
|
||||||
|
@ -57,6 +63,8 @@ void AutoUpdater::SetFeedURL(const std::string& feed) {
|
||||||
delegate->OnUpdateAvailable();
|
delegate->OnUpdateAvailable();
|
||||||
});
|
});
|
||||||
}];
|
}];
|
||||||
|
} else {
|
||||||
|
g_updater.updateRequest = urlRequest;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "atom/common/crash_reporter/crash_reporter.h"
|
#include "atom/common/crash_reporter/crash_reporter.h"
|
||||||
|
#include "atom/common/native_mate_converters/string_map_converter.h"
|
||||||
#include "base/bind.h"
|
#include "base/bind.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
|
|
||||||
|
@ -15,24 +16,6 @@ using crash_reporter::CrashReporter;
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
template<>
|
|
||||||
struct Converter<std::map<std::string, std::string> > {
|
|
||||||
static bool FromV8(v8::Isolate* isolate,
|
|
||||||
v8::Local<v8::Value> val,
|
|
||||||
std::map<std::string, std::string>* out) {
|
|
||||||
if (!val->IsObject())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
v8::Local<v8::Object> dict = val->ToObject();
|
|
||||||
v8::Local<v8::Array> keys = dict->GetOwnPropertyNames();
|
|
||||||
for (uint32_t i = 0; i < keys->Length(); ++i) {
|
|
||||||
v8::Local<v8::Value> key = keys->Get(i);
|
|
||||||
(*out)[V8ToString(key)] = V8ToString(dict->Get(key));
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
struct Converter<CrashReporter::UploadReportResult> {
|
struct Converter<CrashReporter::UploadReportResult> {
|
||||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||||
|
|
35
atom/common/native_mate_converters/string_map_converter.h
Normal file
35
atom/common/native_mate_converters/string_map_converter.h
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
// Copyright (c) 2016 GitHub, Inc.
|
||||||
|
// Use of this source code is governed by the MIT license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING_MAP_CONVERTER_H_
|
||||||
|
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING_MAP_CONVERTER_H_
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "native_mate/converter.h"
|
||||||
|
|
||||||
|
namespace mate {
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct Converter<std::map<std::string, std::string> > {
|
||||||
|
static bool FromV8(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::Value> val,
|
||||||
|
std::map<std::string, std::string>* out) {
|
||||||
|
if (!val->IsObject())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
v8::Local<v8::Object> dict = val->ToObject();
|
||||||
|
v8::Local<v8::Array> keys = dict->GetOwnPropertyNames();
|
||||||
|
for (uint32_t i = 0; i < keys->Length(); ++i) {
|
||||||
|
v8::Local<v8::Value> key = keys->Get(i);
|
||||||
|
(*out)[V8ToString(key)] = V8ToString(dict->Get(key));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace mate
|
||||||
|
|
||||||
|
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING_MAP_CONVERTER_H_
|
|
@ -94,12 +94,12 @@ On Windows only `releaseName` is available.
|
||||||
|
|
||||||
The `autoUpdater` object has the following methods:
|
The `autoUpdater` object has the following methods:
|
||||||
|
|
||||||
### `autoUpdater.setFeedURL(url)`
|
### `autoUpdater.setFeedURL(url[, requestHeaders])`
|
||||||
|
|
||||||
* `url` String
|
* `url` String
|
||||||
|
* `requestHeaders` Object - HTTP request headers (_OS X_)
|
||||||
|
|
||||||
Sets the `url` and initialize the auto updater. The `url` cannot be changed
|
Sets the `url` and initialize the auto updater.
|
||||||
once it is set.
|
|
||||||
|
|
||||||
### `autoUpdater.checkForUpdates()`
|
### `autoUpdater.checkForUpdates()`
|
||||||
|
|
||||||
|
|
|
@ -377,6 +377,7 @@
|
||||||
'atom/common/native_mate_converters/net_converter.cc',
|
'atom/common/native_mate_converters/net_converter.cc',
|
||||||
'atom/common/native_mate_converters/net_converter.h',
|
'atom/common/native_mate_converters/net_converter.h',
|
||||||
'atom/common/native_mate_converters/string16_converter.h',
|
'atom/common/native_mate_converters/string16_converter.h',
|
||||||
|
'atom/common/native_mate_converters/string_map_converter.h',
|
||||||
'atom/common/native_mate_converters/ui_base_types_converter.h',
|
'atom/common/native_mate_converters/ui_base_types_converter.h',
|
||||||
'atom/common/native_mate_converters/v8_value_converter.cc',
|
'atom/common/native_mate_converters/v8_value_converter.cc',
|
||||||
'atom/common/native_mate_converters/v8_value_converter.h',
|
'atom/common/native_mate_converters/v8_value_converter.h',
|
||||||
|
|
|
@ -3,4 +3,8 @@ const autoUpdater = process.atomBinding('auto_updater').autoUpdater
|
||||||
|
|
||||||
Object.setPrototypeOf(autoUpdater, EventEmitter.prototype)
|
Object.setPrototypeOf(autoUpdater, EventEmitter.prototype)
|
||||||
|
|
||||||
|
autoUpdater.setFeedURL = function (url, headers) {
|
||||||
|
return autoUpdater._setFeedURL(url, headers || {})
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = autoUpdater
|
module.exports = autoUpdater
|
||||||
|
|
|
@ -19,7 +19,7 @@ AutoUpdater.prototype.quitAndInstall = function () {
|
||||||
return app.quit()
|
return app.quit()
|
||||||
}
|
}
|
||||||
|
|
||||||
AutoUpdater.prototype.setFeedURL = function (updateURL) {
|
AutoUpdater.prototype.setFeedURL = function (updateURL, headers) {
|
||||||
this.updateURL = updateURL
|
this.updateURL = updateURL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue