Skip to content

Commit

Permalink
Merge pull request #1136 from brave/sync-hostname
Browse files Browse the repository at this point in the history
Use `gethostname` to setup default device name
  • Loading branch information
darkdh authored and cezaraugusto committed Jan 4, 2019
1 parent d4d13cf commit ba02f2d
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 51 deletions.
1 change: 1 addition & 0 deletions components/brave_sync/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ source_set("js_sync_lib_impl") {
"//content/public/browser",
"//extensions/browser",
"//services/network/public/cpp",
"//net",
"//ui/base",
]
}
Expand Down
34 changes: 21 additions & 13 deletions components/brave_sync/brave_sync_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "content/public/browser/browser_thread.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "content/public/browser/network_service_instance.h"
#include "net/base/network_interfaces.h"

namespace brave_sync {

Expand Down Expand Up @@ -166,11 +167,6 @@ void BraveSyncServiceImpl::OnSetupSyncHaveCode(const std::string& sync_words,
return;
}

if (device_name.empty()) {
OnSyncSetupError("ERR_SYNC_NO_DEVICE_NAME");
return;
}

if (initializing_) {
NotifyLogMessage("currently initializing");
return;
Expand All @@ -181,7 +177,7 @@ void BraveSyncServiceImpl::OnSetupSyncHaveCode(const std::string& sync_words,
return;
}

sync_prefs_->SetThisDeviceName(device_name);
SetDeviceName(device_name);
initializing_ = true;

sync_prefs_->SetSyncEnabled(true);
Expand All @@ -196,11 +192,6 @@ void BraveSyncServiceImpl::OnSetupSyncNewToSync(
return;
}

if (device_name.empty()) {
OnSyncSetupError("ERR_SYNC_NO_DEVICE_NAME");
return;
}

if (initializing_) {
NotifyLogMessage("currently initializing");
return;
Expand All @@ -213,8 +204,7 @@ void BraveSyncServiceImpl::OnSetupSyncNewToSync(

sync_words_.clear(); // If the previous attempt was connect to sync chain
// and failed to receive save-init-data

sync_prefs_->SetThisDeviceName(device_name);
SetDeviceName(device_name);
initializing_ = true;

sync_prefs_->SetSyncEnabled(true);
Expand Down Expand Up @@ -686,4 +676,22 @@ void BraveSyncServiceImpl::ResetSyncInternal() {
sync_prefs_->SetSyncEnabled(false);
}

void BraveSyncServiceImpl::SetDeviceName(const std::string& name) {
if (name.empty()) {
std::string hostname = net::GetHostName();
if (hostname.empty()) {
#if defined(OS_MACOSX)
hostname = std::string("Mac Desktop");
#elif defined(OS_LINUX)
hostname = std::string("Linux Desktop");
#elif defined(OS_WIN)
hostname = std::string("Windows Desktop");
#endif
}
sync_prefs_->SetThisDeviceName(hostname);
} else {
sync_prefs_->SetThisDeviceName(name);
}
}

} // namespace brave_sync
2 changes: 2 additions & 0 deletions components/brave_sync/brave_sync_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ class BraveSyncServiceImpl

void ResetSyncInternal();

void SetDeviceName(const std::string& name);

std::unique_ptr<BraveSyncClient> sync_client_;

// True when is in active sync chain
Expand Down
23 changes: 23 additions & 0 deletions components/brave_sync/brave_sync_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "content/public/browser/network_service_instance.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "net/base/network_interfaces.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand Down Expand Up @@ -263,6 +264,17 @@ TEST_F(BraveSyncServiceTest, OnSetupSyncHaveCode) {
brave_sync::prefs::kSyncEnabled));
}

TEST_F(BraveSyncServiceTest, OnSetupSyncHaveCode_EmptyDeviceName) {
EXPECT_CALL(*sync_client(), OnSyncEnabledChanged);
// Expecting sync state changed twice: for enabled state and for device name
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(2);
sync_service()->OnSetupSyncHaveCode("word1 word2 word3", "");
EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
brave_sync::prefs::kSyncEnabled));
EXPECT_EQ(profile()->GetPrefs()->GetString(
brave_sync::prefs::kSyncDeviceName), net::GetHostName());
}

TEST_F(BraveSyncServiceTest, OnSetupSyncHaveCode_Offline) {
TestNetworkConnectionTracker* tracker =
TestNetworkConnectionTracker::GetInstance();
Expand Down Expand Up @@ -296,6 +308,17 @@ TEST_F(BraveSyncServiceTest, OnSetupSyncNewToSync_Offline) {
tracker->SetConnectionType(ConnectionType::CONNECTION_UNKNOWN);
}

TEST_F(BraveSyncServiceTest, OnSetupSyncNewToSync_EmptyDeviceName) {
EXPECT_CALL(*sync_client(), OnSyncEnabledChanged);
// Expecting sync state changed twice: for enabled state and for device name
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(2);
sync_service()->OnSetupSyncNewToSync("");
EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
brave_sync::prefs::kSyncEnabled));
EXPECT_EQ(profile()->GetPrefs()->GetString(
brave_sync::prefs::kSyncDeviceName), net::GetHostName());
}

TEST_F(BraveSyncServiceTest, OnConnectionChanged_After_OnSetupSyncNewToSync) {
content::GetNetworkConnectionTracker()->AddNetworkConnectionObserver(this);
TestNetworkConnectionTracker* tracker =
Expand Down
5 changes: 2 additions & 3 deletions components/brave_sync/ui/components/modals/deviceType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import ScanCodeModal from './scanCode'

// Utils
import { getLocale } from '../../../../common/locale'
import { getDefaultDeviceName } from '../../helpers'

// Images
import { SyncDesktopIcon, SyncMobileIcon } from 'brave-ui/features/sync/images'
Expand Down Expand Up @@ -54,7 +53,7 @@ export default class DeviceTypeModal extends React.PureComponent<Props, State> {
// this allow us to request the qr code and sync words immediately
const { thisDeviceName } = this.props.syncData
if (thisDeviceName === '') {
this.props.actions.onSetupNewToSync(getDefaultDeviceName())
this.props.actions.onSetupNewToSync('')
}
}

Expand Down Expand Up @@ -117,7 +116,7 @@ export default class DeviceTypeModal extends React.PureComponent<Props, State> {
}
<ModalHeader>
<div>
<ModalTitle level={1}>{getLocale('letsSync')}{getDefaultDeviceName()}”.</ModalTitle>
<ModalTitle level={1}>{getLocale('letsSync')}{syncData.thisDeviceName}”.</ModalTitle>
<ModalSubTitle>{getLocale('chooseDeviceType')}</ModalSubTitle>
</div>
</ModalHeader>
Expand Down
4 changes: 1 addition & 3 deletions components/brave_sync/ui/components/modals/enterSyncCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {

// Utils
import { getLocale } from '../../../../common/locale'
import { getDefaultDeviceName } from '../../helpers'

interface Props {
syncData: Sync.State
Expand Down Expand Up @@ -53,8 +52,7 @@ interface Props {
return
}
const { passphrase } = this.state
const deviceName = getDefaultDeviceName()
this.props.actions.onSetupSyncHaveCode(passphrase, deviceName)
this.props.actions.onSetupSyncHaveCode(passphrase, '')
}

render () {
Expand Down
28 changes: 0 additions & 28 deletions components/brave_sync/ui/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,3 @@ export const generateQRCodeImageSource = (seed: string) => {
console.error('[SYNC] QR image error:', error.toString())
}
}

/**
* Gets the user's operating system name to ne ised as a default device name
* @returns {string} - A string including the OS + the desktop suffix
*/
export const getDefaultDeviceName = () => {
const userAgent = window.navigator.userAgent
const currentPlatform = window.navigator.platform
const macosVariants = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
const windowsVariants = ['Win32', 'Win64', 'Windows', 'WinCE']
const iOSVariants = ['iPhone', 'iPad', 'iPod']
const androidVariants = ['Android']

let system

if (macosVariants.includes(currentPlatform)) {
system = 'Mac'
} else if (windowsVariants.includes(currentPlatform)) {
system = 'Windows'
} else if (iOSVariants.includes(currentPlatform)) {
system = 'iOS'
} else if (androidVariants.includes(userAgent)) {
system = 'Android'
} else {
system = 'Linux'
}
return `${system} Desktop`
}
3 changes: 0 additions & 3 deletions components/brave_sync/ui/reducers/sync_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ const syncReducer: Reducer<Sync.State | undefined> = (state: Sync.State | undefi
break

case types.SYNC_ON_SETUP_NEW_TO_SYNC:
if (!payload.thisDeviceName) {
break
}
chrome.send('setupSyncNewToSync', [payload.thisDeviceName])
break

Expand Down
1 change: 0 additions & 1 deletion components/definitions/sync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export type SetupErrorType =
'ERR_SYNC_NO_INTERNET' |
'ERR_SYNC_MISSING_WORDS' |
'ERR_SYNC_WRONG_WORDS' |
'ERR_SYNC_NO_DEVICE_NAME' |
'ERR_SYNC_INIT_FAILED' |
undefined

Expand Down

0 comments on commit ba02f2d

Please sign in to comment.