Merge pull request #6018 from electron/string-map
Cleanup code for autoUpdater
This commit is contained in:
commit
8fe5dd22b2
9 changed files with 32 additions and 67 deletions
|
@ -9,7 +9,6 @@
|
|||
#include "atom/browser/native_window.h"
|
||||
#include "atom/browser/window_list.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 "native_mate/dictionary.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
|
@ -80,6 +79,12 @@ void AutoUpdater::OnWindowAllClosed() {
|
|||
QuitAndInstall();
|
||||
}
|
||||
|
||||
void AutoUpdater::SetFeedURL(const std::string& url, mate::Arguments* args) {
|
||||
auto_updater::AutoUpdater::HeaderMap headers;
|
||||
args->GetNext(&headers);
|
||||
auto_updater::AutoUpdater::SetFeedURL(url, headers);
|
||||
}
|
||||
|
||||
void AutoUpdater::QuitAndInstall() {
|
||||
// If we don't have any window then quitAndInstall immediately.
|
||||
WindowList* window_list = WindowList::GetInstance();
|
||||
|
@ -103,8 +108,8 @@ mate::Handle<AutoUpdater> AutoUpdater::Create(v8::Isolate* isolate) {
|
|||
void AutoUpdater::BuildPrototype(
|
||||
v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> prototype) {
|
||||
mate::ObjectTemplateBuilder(isolate, prototype)
|
||||
.SetMethod("_setFeedURL", &auto_updater::AutoUpdater::SetFeedURL)
|
||||
.SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates)
|
||||
.SetMethod("setFeedURL", &AutoUpdater::SetFeedURL)
|
||||
.SetMethod("quitAndInstall", &AutoUpdater::QuitAndInstall);
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "atom/browser/api/event_emitter.h"
|
||||
#include "atom/browser/auto_updater.h"
|
||||
#include "atom/browser/window_list_observer.h"
|
||||
#include "native_mate/arguments.h"
|
||||
#include "native_mate/handle.h"
|
||||
|
||||
namespace atom {
|
||||
|
@ -43,6 +44,7 @@ class AutoUpdater : public mate::EventEmitter<AutoUpdater>,
|
|||
void OnWindowAllClosed() override;
|
||||
|
||||
private:
|
||||
void SetFeedURL(const std::string& url, mate::Arguments* args);
|
||||
void QuitAndInstall();
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(AutoUpdater);
|
||||
|
|
|
@ -38,34 +38,33 @@ void AutoUpdater::SetFeedURL(const std::string& feed,
|
|||
NSURL* url = [NSURL URLWithString:base::SysUTF8ToNSString(feed)];
|
||||
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];
|
||||
|
||||
for (auto&& it : requestHeaders) {
|
||||
for (const auto& it : requestHeaders) {
|
||||
[urlRequest setValue:base::SysUTF8ToNSString(it.second)
|
||||
forHTTPHeaderField:base::SysUTF8ToNSString(it.first)];
|
||||
}
|
||||
|
||||
if (g_updater == nil) {
|
||||
// Initialize the SQRLUpdater.
|
||||
@try {
|
||||
g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest];
|
||||
} @catch (NSException* error) {
|
||||
delegate->OnError(base::SysNSStringToUTF8(error.reason));
|
||||
return;
|
||||
}
|
||||
if (g_updater)
|
||||
[g_updater release];
|
||||
|
||||
[[g_updater rac_valuesForKeyPath:@"state" observer:g_updater]
|
||||
subscribeNext:^(NSNumber *stateNumber) {
|
||||
int state = [stateNumber integerValue];
|
||||
// Dispatching the event on main thread.
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (state == SQRLUpdaterStateCheckingForUpdate)
|
||||
delegate->OnCheckingForUpdate();
|
||||
else if (state == SQRLUpdaterStateDownloadingUpdate)
|
||||
delegate->OnUpdateAvailable();
|
||||
});
|
||||
}];
|
||||
} else {
|
||||
g_updater.updateRequest = urlRequest;
|
||||
// Initialize the SQRLUpdater.
|
||||
@try {
|
||||
g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest];
|
||||
} @catch (NSException* error) {
|
||||
delegate->OnError(base::SysNSStringToUTF8(error.reason));
|
||||
return;
|
||||
}
|
||||
|
||||
[[g_updater rac_valuesForKeyPath:@"state" observer:g_updater]
|
||||
subscribeNext:^(NSNumber *stateNumber) {
|
||||
int state = [stateNumber integerValue];
|
||||
// Dispatching the event on main thread.
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (state == SQRLUpdaterStateCheckingForUpdate)
|
||||
delegate->OnCheckingForUpdate();
|
||||
else if (state == SQRLUpdaterStateDownloadingUpdate)
|
||||
delegate->OnUpdateAvailable();
|
||||
});
|
||||
}];
|
||||
}
|
||||
|
||||
// static
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
#include <string>
|
||||
|
||||
#include "atom/common/crash_reporter/crash_reporter.h"
|
||||
#include "atom/common/native_mate_converters/string_map_converter.h"
|
||||
#include "base/bind.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
// 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_
|
|
@ -97,7 +97,7 @@ The `autoUpdater` object has the following methods:
|
|||
### `autoUpdater.setFeedURL(url[, requestHeaders])`
|
||||
|
||||
* `url` String
|
||||
* `requestHeaders` Object - HTTP request headers (_OS X_)
|
||||
* `requestHeaders` Object _OS X_ - HTTP request headers.
|
||||
|
||||
Sets the `url` and initialize the auto updater.
|
||||
|
||||
|
|
|
@ -377,7 +377,6 @@
|
|||
'atom/common/native_mate_converters/net_converter.cc',
|
||||
'atom/common/native_mate_converters/net_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/v8_value_converter.cc',
|
||||
'atom/common/native_mate_converters/v8_value_converter.h',
|
||||
|
|
|
@ -3,8 +3,4 @@ const autoUpdater = process.atomBinding('auto_updater').autoUpdater
|
|||
|
||||
Object.setPrototypeOf(autoUpdater, EventEmitter.prototype)
|
||||
|
||||
autoUpdater.setFeedURL = function (url, headers) {
|
||||
return autoUpdater._setFeedURL(url, headers || {})
|
||||
}
|
||||
|
||||
module.exports = autoUpdater
|
||||
|
|
2
vendor/native_mate
vendored
2
vendor/native_mate
vendored
|
@ -1 +1 @@
|
|||
Subproject commit 4ad6ecd19617ac33c09e93ccb6d8e652ac1ac126
|
||||
Subproject commit e75f2aa087db346efc4b530f9e1ce7d3a72a3434
|
Loading…
Reference in a new issue