Commit c2fe512c authored by Jeffrey Cohen's avatar Jeffrey Cohen Committed by Commit Bot

[SendTabToSelf] create util functions for IOS

add utility functions to determine when to display the STTS UI

Bug: 959475
Change-Id: I0318090e1b018bb4be874f967bbee0500dcff39b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1597909Reviewed-by: default avatarPeter Lee <pkl@chromium.org>
Reviewed-by: default avatarsebsg <sebsg@chromium.org>
Commit-Queue: Jeffrey Cohen <jeffreycohen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#659122}
parent 61b65cd8
......@@ -11,6 +11,8 @@ source_set("send_tab_to_self") {
"send_tab_to_self_client_service_factory.mm",
"send_tab_to_self_client_service_ios.h",
"send_tab_to_self_client_service_ios.mm",
"send_tab_to_self_util.h",
"send_tab_to_self_util.mm",
]
deps = [
"//base",
......@@ -19,6 +21,7 @@ source_set("send_tab_to_self") {
"//components/keyed_service/ios",
"//components/send_tab_to_self",
"//components/sync",
"//components/sync_device_info",
"//ios/chrome/app/strings",
"//ios/chrome/app/theme:theme_grit",
"//ios/chrome/browser",
......@@ -36,3 +39,22 @@ source_set("send_tab_to_self") {
"//ui/strings:ui_strings_grit",
]
}
source_set("unit_tests") {
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
"send_tab_to_self_util_unittest.mm",
]
deps = [
":send_tab_to_self",
"//base",
"//base/test:test_support",
"//components/send_tab_to_self",
"//components/sync",
"//ios/chrome/browser/browser_state:test_support",
"//testing/gtest",
"//third_party/ocmock",
"//url",
]
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_SEND_TAB_TO_SELF_SEND_TAB_TO_SELF_UTIL_H_
#define IOS_CHROME_BROWSER_SEND_TAB_TO_SELF_SEND_TAB_TO_SELF_UTIL_H_
class GURL;
namespace ios {
class ChromeBrowserState;
}
namespace send_tab_to_self {
// Returns true if the 'send tab to self' flag is enabled.
bool IsReceivingEnabled();
// Returns true if the 'send-tab-to-self' and 'send-tab-to-self-show-sending-ui'
// flags are enabled.
bool IsSendingEnabled();
// Returns true if the SendTabToSelf sync datatype is active.
bool IsUserSyncTypeActive(ios::ChromeBrowserState* browser_state);
// Returns true if the user syncing on two or more devices.
bool IsSyncingOnMultipleDevices(ios::ChromeBrowserState* browser_state);
// Returns true if the tab and web content requirements are met:
// User is viewing an HTTP or HTTPS page.
// User is not on a native page.
// User is not in Incongnito mode.
bool IsContentRequirementsMet(const GURL& gurl,
ios::ChromeBrowserState* browser_state);
// Returns true if all conditions are true and shows the option onto the menu.
bool ShouldOfferFeature();
// Add a new entry to SendTabToSelfModel when user click "Share to your
// devices" option.
void CreateNewEntry(ios::ChromeBrowserState* browser_state);
} // namespace send_tab_to_self
#endif // IOS_CHROME_BROWSER_SEND_TAB_TO_SELF_SEND_TAB_TO_SELF_UTIL_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/send_tab_to_self/send_tab_to_self_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/send_tab_to_self/features.h"
#include "components/send_tab_to_self/send_tab_to_self_model.h"
#include "components/send_tab_to_self/send_tab_to_self_sync_service.h"
#include "components/sync/driver/sync_driver_switches.h"
#include "components/sync/driver/sync_service.h"
#include "components/sync/driver/sync_user_settings.h"
#include "components/sync_device_info/device_info.h"
#include "components/sync_device_info/device_info_sync_service.h"
#include "components/sync_device_info/device_info_tracker.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state_manager.h"
#include "ios/chrome/browser/chrome_url_constants.h"
#include "ios/chrome/browser/send_tab_to_self/send_tab_to_self_client_service_factory.h"
#include "ios/chrome/browser/sync/device_info_sync_service_factory.h"
#include "ios/chrome/browser/sync/send_tab_to_self_sync_service_factory.h"
#import "ios/chrome/browser/tabs/tab_model.h"
#import "ios/chrome/browser/tabs/tab_model_list.h"
#import "ios/chrome/browser/web_state_list/web_state_list.h"
#import "ios/web/public/navigation_item.h"
#import "ios/web/public/web_state/web_state.h"
#include "url/gurl.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace send_tab_to_self {
bool IsReceivingEnabled() {
return base::FeatureList::IsEnabled(switches::kSyncSendTabToSelf);
}
bool IsSendingEnabled() {
return IsReceivingEnabled() &&
base::FeatureList::IsEnabled(kSendTabToSelfShowSendingUI);
}
bool IsUserSyncTypeActive(ios::ChromeBrowserState* browser_state) {
SendTabToSelfSyncService* service =
SendTabToSelfSyncServiceFactory::GetForBrowserState(browser_state);
// The service will be null if the user is in incognito mode so better to
// check for that.
return service && service->GetSendTabToSelfModel() &&
service->GetSendTabToSelfModel()->IsReady();
}
bool IsSyncingOnMultipleDevices(ios::ChromeBrowserState* browser_state) {
syncer::DeviceInfoSyncService* device_sync_service =
DeviceInfoSyncServiceFactory::GetForBrowserState(browser_state);
return device_sync_service && device_sync_service->GetDeviceInfoTracker() &&
device_sync_service->GetDeviceInfoTracker()->CountActiveDevices() > 1;
}
bool IsContentRequirementsMet(const GURL& url,
ios::ChromeBrowserState* browser_state) {
bool is_http_or_https = url.SchemeIsHTTPOrHTTPS();
bool is_native_page = url.SchemeIs(kChromeUIScheme);
bool is_incognito_mode = browser_state->IsOffTheRecord();
return is_http_or_https && !is_native_page && !is_incognito_mode;
}
bool ShouldOfferFeature() {
ios::ChromeBrowserStateManager* browser_state_manager =
GetApplicationContext()->GetChromeBrowserStateManager();
if (!browser_state_manager) {
// Can happen in tests.
return false;
}
ios::ChromeBrowserState* browser_state =
browser_state_manager->GetLastUsedBrowserState();
if (!browser_state) {
return false;
}
// If there is no web state or it is not visible then nothing should be
// shared.
TabModel* tab_model =
TabModelList::GetLastActiveTabModelForChromeBrowserState(browser_state);
if (!tab_model) {
return false;
}
WebStateList* web_state_list = tab_model.webStateList;
if (!web_state_list) {
return false;
}
web::WebState* web_state = web_state_list->GetActiveWebState();
// If sending is enabled, then so is receiving.
return IsSendingEnabled() && IsUserSyncTypeActive(browser_state) &&
IsSyncingOnMultipleDevices(browser_state) &&
IsContentRequirementsMet(web_state->GetVisibleURL(), browser_state);
}
void CreateNewEntry(ios::ChromeBrowserState* browser_state) {
// If there is no web state or it is not visible then nothing should be
// shared.
TabModel* tab_model =
TabModelList::GetLastActiveTabModelForChromeBrowserState(browser_state);
WebStateList* web_state_list = tab_model.webStateList;
if (!web_state_list) {
return;
}
web::WebState* web_state = web_state_list->GetActiveWebState();
if (!web_state) {
return;
}
web::NavigationItem* cur_item =
web_state->GetNavigationManager()->GetLastCommittedItem();
if (!cur_item) {
return;
}
GURL url = cur_item->GetURL();
std::string title = base::UTF16ToUTF8(cur_item->GetTitle());
base::Time navigation_time = cur_item->GetTimestamp();
// TODO(crbug.com/946804) Add target device.
std::string target_device;
SendTabToSelfModel* model =
SendTabToSelfSyncServiceFactory::GetForBrowserState(browser_state)
->GetSendTabToSelfModel();
model->AddEntry(url, title, navigation_time, target_device);
}
} // namespace send_tab_to_self
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/send_tab_to_self/send_tab_to_self_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_task_environment.h"
#include "components/send_tab_to_self/features.h"
#include "components/sync/driver/sync_driver_switches.h"
#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace send_tab_to_self {
class SendTabToSelfUtilTest : public PlatformTest {
public:
SendTabToSelfUtilTest() {
TestChromeBrowserState::Builder builder;
browser_state_ = builder.Build();
}
~SendTabToSelfUtilTest() override {}
ios::ChromeBrowserState* browser_state() { return browser_state_.get(); }
ios::ChromeBrowserState* OffTheRecordChromeBrowserState() {
return browser_state_->GetOffTheRecordChromeBrowserState();
}
protected:
base::test::ScopedFeatureList scoped_feature_list_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
private:
std::unique_ptr<ios::ChromeBrowserState> browser_state_;
};
TEST_F(SendTabToSelfUtilTest, AreFlagsEnabled) {
scoped_task_environment_.RunUntilIdle();
scoped_feature_list_.InitWithFeatures(
{switches::kSyncSendTabToSelf, kSendTabToSelfShowSendingUI}, {});
EXPECT_TRUE(IsSendingEnabled());
EXPECT_TRUE(IsReceivingEnabled());
}
TEST_F(SendTabToSelfUtilTest, AreFlagsDisabled) {
scoped_task_environment_.RunUntilIdle();
scoped_feature_list_.InitWithFeatures(
{}, {switches::kSyncSendTabToSelf, kSendTabToSelfShowSendingUI});
EXPECT_FALSE(IsSendingEnabled());
EXPECT_FALSE(IsReceivingEnabled());
}
TEST_F(SendTabToSelfUtilTest, IsReceivingEnabled) {
scoped_task_environment_.RunUntilIdle();
scoped_feature_list_.InitWithFeatures({switches::kSyncSendTabToSelf},
{kSendTabToSelfShowSendingUI});
EXPECT_FALSE(IsSendingEnabled());
EXPECT_TRUE(IsReceivingEnabled());
}
TEST_F(SendTabToSelfUtilTest, IsOnlySendingEnabled) {
scoped_task_environment_.RunUntilIdle();
scoped_feature_list_.InitWithFeatures({kSendTabToSelfShowSendingUI},
{switches::kSyncSendTabToSelf});
EXPECT_FALSE(IsSendingEnabled());
EXPECT_FALSE(IsReceivingEnabled());
}
TEST_F(SendTabToSelfUtilTest, NotHTTPOrHTTPS) {
GURL url = GURL("192.168.0.0");
EXPECT_FALSE(IsContentRequirementsMet(url, browser_state()));
}
TEST_F(SendTabToSelfUtilTest, WebUIPage) {
GURL url = GURL("chrome://flags");
EXPECT_FALSE(IsContentRequirementsMet(url, browser_state()));
}
TEST_F(SendTabToSelfUtilTest, IncognitoMode) {
GURL url = GURL("https://www.google.com");
EXPECT_FALSE(IsContentRequirementsMet(url, OffTheRecordChromeBrowserState()));
}
TEST_F(SendTabToSelfUtilTest, ValidUrl) {
GURL url = GURL("https://www.google.com");
EXPECT_TRUE(IsContentRequirementsMet(url, browser_state()));
}
// TODO(crbug.com/961897) Add test for CreateNewEntry.
} // namespace send_tab_to_self
......@@ -170,6 +170,7 @@ test("ios_chrome_unittests") {
"//ios/chrome/browser/reading_list:unit_tests",
"//ios/chrome/browser/safe_mode:unit_tests",
"//ios/chrome/browser/search_engines:unit_tests",
"//ios/chrome/browser/send_tab_to_self:unit_tests",
"//ios/chrome/browser/sessions:unit_tests",
"//ios/chrome/browser/signin:unit_tests",
"//ios/chrome/browser/snapshots:unit_tests",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment