Skip to content

Commit

Permalink
[Search] Add a policy to allow disabling a search feature
Browse files Browse the repository at this point in the history
Specifically, Contextual Search can now be controlled by policy.

BUG=432631

Review URL: https://codereview.chromium.org/720933002

Cr-Commit-Position: refs/heads/master@{#304662}
  • Loading branch information
mathp authored and Commit bot committed Nov 18, 2014
1 parent b4c4604 commit cc5d84f
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,14 @@ public boolean isContextualSearchDisabled() {
return getContextualSearchPreference().equals(CONTEXTUAL_SEARCH_DISABLED);
}

/**
* @return whether the Contextual Search feature is disabled by policy.
*/
public boolean isContextualSearchDisabledByPolicy() {
return nativeGetContextualSearchPreferenceIsManaged()
&& isContextualSearchDisabled();
}

/**
* @return whether the Contextual Search feature is uninitialized (preference unset by the
* user).
Expand Down Expand Up @@ -734,6 +742,7 @@ private native void nativeClearBrowsingData(boolean history, boolean cache,
private native void nativeGetProfilePath(ProfilePathCallback callback);
private native void nativeSetContextualSearchPreference(String preference);
private native String nativeGetContextualSearchPreference();
private native boolean nativeGetContextualSearchPreferenceIsManaged();
private native boolean nativeGetSearchSuggestEnabled();
private native void nativeSetSearchSuggestEnabled(boolean enabled);
private native boolean nativeGetSearchSuggestManaged();
Expand Down
5 changes: 5 additions & 0 deletions chrome/browser/android/preferences/pref_service_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,11 @@ static jstring GetContextualSearchPreference(JNIEnv* env, jobject obj) {
Release();
}

static jboolean GetContextualSearchPreferenceIsManaged(JNIEnv* env,
jobject obj) {
return GetPrefService()->IsManagedPreference(prefs::kContextualSearchEnabled);
}

static void SetContextualSearchPreference(JNIEnv* env, jobject obj,
jstring pref) {
GetPrefService()->SetString(prefs::kContextualSearchEnabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include "components/translate/core/common/translate_pref_names.h"
#include "policy/policy_constants.h"

#if defined(OS_ANDROID)
#include "chrome/browser/search/contextual_search_policy_handler_android.h"
#endif

#if !defined(OS_IOS)
#include "chrome/browser/net/disk_cache_dir_policy_handler.h"
#include "chrome/browser/policy/file_selection_dialogs_policy_handler.h"
Expand Down Expand Up @@ -575,6 +579,11 @@ scoped_ptr<ConfigurationPolicyHandlerList> BuildHandlerList(
handlers->AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>(
new URLBlacklistPolicyHandler()));

#if defined(OS_ANDROID)
handlers->AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>(
new ContextualSearchPolicyHandlerAndroid()));
#endif

#if !defined(OS_IOS)
handlers->AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>(
new FileSelectionDialogsPolicyHandler()));
Expand Down
39 changes: 39 additions & 0 deletions chrome/browser/search/contextual_search_policy_handler_android.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2014 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 "chrome/browser/search/contextual_search_policy_handler_android.h"

#include "base/prefs/pref_value_map.h"
#include "base/values.h"
#include "chrome/common/pref_names.h"
#include "components/policy/core/common/policy_map.h"
#include "policy/policy_constants.h"

namespace policy {

ContextualSearchPolicyHandlerAndroid::ContextualSearchPolicyHandlerAndroid()
: TypeCheckingPolicyHandler(key::kContextualSearchEnabled,
base::Value::TYPE_BOOLEAN) {}

ContextualSearchPolicyHandlerAndroid::~ContextualSearchPolicyHandlerAndroid() {
}

void ContextualSearchPolicyHandlerAndroid::ApplyPolicySettings(
const PolicyMap& policies,
PrefValueMap* prefs) {
const base::Value* value = policies.GetValue(policy_name());
bool contextual_search_enabled = true;
// From a Contextual Search preference point of view, "false" means the
// feature is turned off completely. "" means the feature is uninitialized and
// an opt-in screen is presented to the user, after which the preference is
// either "true" or "false", depending on their choice. Here a false policy
// explicitly disables Contextual Search.
if (value &&
value->GetAsBoolean(&contextual_search_enabled) &&
!contextual_search_enabled) {
prefs->SetString(prefs::kContextualSearchEnabled, "false");
}
}

} // namespace policy
31 changes: 31 additions & 0 deletions chrome/browser/search/contextual_search_policy_handler_android.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2014 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 CHROME_BROWSER_SEARCH_CONTEXTUAL_SEARCH_POLICY_HANDLER_ANDROID_H_
#define CHROME_BROWSER_SEARCH_CONTEXTUAL_SEARCH_POLICY_HANDLER_ANDROID_H_

#include "components/policy/core/browser/configuration_policy_handler.h"

namespace policy {

class PolicyMap;

// ConfigurationPolicyHandler for the ContextualSearchEnabled policy.
class ContextualSearchPolicyHandlerAndroid
: public TypeCheckingPolicyHandler {
public:
ContextualSearchPolicyHandlerAndroid();
~ContextualSearchPolicyHandlerAndroid() override;

// ConfigurationPolicyHandler methods:
void ApplyPolicySettings(const PolicyMap& policies,
PrefValueMap* prefs) override;

private:
DISALLOW_COPY_AND_ASSIGN(ContextualSearchPolicyHandlerAndroid);
};

} // namespace policy

#endif // CHROME_BROWSER_SEARCH_CONTEXTUAL_SEARCH_POLICY_HANDLER_ANDROID_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2014 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 "base/prefs/pref_value_map.h"
#include "base/values.h"
#include "chrome/browser/search/contextual_search_policy_handler_android.h"
#include "chrome/common/pref_names.h"
#include "components/policy/core/common/policy_map.h"
#include "policy/policy_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace policy {

TEST(ContextualSearchPolicyHandlerAndroidTest, Default) {
PolicyMap policy;
PrefValueMap prefs;
ContextualSearchPolicyHandlerAndroid handler;
handler.ApplyPolicySettings(policy, &prefs);
std::string pref_value;
EXPECT_FALSE(prefs.GetString(prefs::kContextualSearchEnabled, &pref_value));
EXPECT_EQ("", pref_value);
}

TEST(ContextualSearchPolicyHandlerAndroidTest, Enabled) {
PolicyMap policy;
policy.Set(key::kContextualSearchEnabled,
POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
new base::FundamentalValue(true),
NULL);
PrefValueMap prefs;
ContextualSearchPolicyHandlerAndroid handler;
handler.ApplyPolicySettings(policy, &prefs);

// Enabling Contextual Search policy should not set the pref.
std::string pref_value;
EXPECT_FALSE(prefs.GetString(prefs::kContextualSearchEnabled, &pref_value));
EXPECT_EQ("", pref_value);
}

TEST(ContextualSearchPolicyHandlerAndroidTest, Disabled) {
PolicyMap policy;
policy.Set(key::kContextualSearchEnabled,
POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
new base::FundamentalValue(false),
NULL);
PrefValueMap prefs;
ContextualSearchPolicyHandlerAndroid handler;
handler.ApplyPolicySettings(policy, &prefs);

// Disabling Contextual Search should switch the pref to managed.
std::string pref_value;
EXPECT_TRUE(prefs.GetString(prefs::kContextualSearchEnabled, &pref_value));
EXPECT_EQ("false", pref_value);
}

} // namespace policy
2 changes: 2 additions & 0 deletions chrome/chrome_browser.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@
'browser/safe_browsing/safe_browsing_tab_observer.h',
'browser/safe_browsing/srt_global_error_win.cc',
'browser/safe_browsing/srt_global_error_win.h',
'browser/search/contextual_search_policy_handler_android.cc',
'browser/search/contextual_search_policy_handler_android.h',
'browser/search/contextual_search_promo_source_android.cc',
'browser/search/contextual_search_promo_source_android.h',
'browser/search/iframe_source.cc',
Expand Down
1 change: 1 addition & 0 deletions chrome/chrome_tests_unit.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@
'browser/safe_browsing/safe_browsing_store_unittest.cc',
'browser/safe_browsing/safe_browsing_util_unittest.cc',
'browser/safe_browsing/two_phase_uploader_unittest.cc',
'browser/search/contextual_search_policy_handler_android_unittest.cc',
'browser/search/hotword_service_unittest.cc',
'browser/search/iframe_source_unittest.cc',
'browser/search/instant_service_unittest.cc',
Expand Down
4 changes: 4 additions & 0 deletions chrome/test/data/policy/policy_test_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@
]
},

"ContextualSearchEnabled": {
"os": ["android"]
},

"AutoFillEnabled": {
"os": ["win", "linux", "mac", "chromeos"],
"can_be_recommended": true,
Expand Down
24 changes: 23 additions & 1 deletion components/policy/resources/policy_templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
# persistent IDs for all fields (but not for groups!) are needed. These are
# specified by the 'id' keys of each policy. NEVER CHANGE EXISTING IDs,
# because doing so would break the deployed wire format!
# For your editing convenience: highest ID currently used: 280
# For your editing convenience: highest ID currently used: 281
#
# Placeholders:
# The following placeholder strings are automatically substituted:
Expand Down Expand Up @@ -6955,6 +6955,28 @@

Note that, despite the number, "sslv3" is an earier version than "tls1".''',
},
{
'name': 'ContextualSearchEnabled',
'type': 'main',
'schema': { 'type': 'boolean' },
'supported_on': [
'android:40-',
],
'features': {
'dynamic_refresh': True,
'per_profile': True,
},
'example_value': True,
'id': 281,
'caption': '''Enable Touch to Search''',
'desc': '''Enables the availability of Touch to Search in <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph>'s content view.

If you enable this setting, Touch to Search will be available to the user and they can choose to turn the feature on or off.

If you disable this setting, Touch to Search will be disabled completely.

If this policy is left not set, it is equivalent to being enabled, see description above.''',
},
],
'messages': {
# Messages that are not associated to any policies.
Expand Down
2 changes: 2 additions & 0 deletions tools/metrics/histograms/histograms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43774,6 +43774,8 @@ Therefore, the affected-histogram name has to have at least one dot in it.
<int value="278" label="Extension Settings"/>
<int value="279" label="SSL minimum version"/>
<int value="280" label="SSL fallback minimum version"/>
<int value="281"
label="Control the availability of the Contextual Search feature."/>
</enum>

<enum name="EnterprisePolicyInvalidations" type="int">
Expand Down

0 comments on commit cc5d84f

Please sign in to comment.