diff options
author | mathp@chromium.org <mathp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-31 06:44:42 +0000 |
---|---|---|
committer | mathp@chromium.org <mathp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-31 06:44:42 +0000 |
commit | d76376d62052dc23d8234d682a57ced2f6627282 (patch) | |
tree | c678926a61ea9a9b6382067339de8dedcc55aaa5 | |
parent | df5abeccc74f10c274118562147ad50375f59794 (diff) | |
download | chromium_src-d76376d62052dc23d8234d682a57ced2f6627282.zip chromium_src-d76376d62052dc23d8234d682a57ced2f6627282.tar.gz chromium_src-d76376d62052dc23d8234d682a57ced2f6627282.tar.bz2 |
[NTP] Provide a check for Chrome identity to NTP
Now part of SearchBox API, the ability to test against the user's Chrome identity.
Will make the actual check in the browser process, and callback to the NTP.
BUG=280169
TEST=unit,manual:
1) Run chrome with --enable-instant-extended-api
2) In the dev console, while on the NTP:
*Signed-out of Chrome*
chrome.embeddedSearch.newTabPage.onsignedincheckdone = function(identityMatch, identity) {
window.console.log(identityMatch);
window.console.log(identity);
};
chrome.embeddedSearch.newTabPage.checkIsUserSignedIntoChromeAs("foo@bar.com")
-> false
-> (empty string)
chrome.embeddedSearch.newTabPage.checkIsUserSignedIntoChromeAs("")
-> true
-> (empty string)
*Sign in to Chrome as foo@bar.com*
chrome.embeddedSearch.newTabPage.checkIsUserSignedIntoChromeAs("foo@bar.com")
-> true
-> foo@bar.com
Review URL: https://codereview.chromium.org/49723003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@232022 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/ui/search/search_ipc_router.cc | 26 | ||||
-rw-r--r-- | chrome/browser/ui/search/search_ipc_router.h | 17 | ||||
-rw-r--r-- | chrome/browser/ui/search/search_ipc_router_policy_impl.cc | 4 | ||||
-rw-r--r-- | chrome/browser/ui/search/search_ipc_router_policy_impl.h | 1 | ||||
-rw-r--r-- | chrome/browser/ui/search/search_ipc_router_policy_unittest.cc | 13 | ||||
-rw-r--r-- | chrome/browser/ui/search/search_ipc_router_unittest.cc | 44 | ||||
-rw-r--r-- | chrome/browser/ui/search/search_tab_helper.cc | 12 | ||||
-rw-r--r-- | chrome/browser/ui/search/search_tab_helper.h | 15 | ||||
-rw-r--r-- | chrome/browser/ui/search/search_tab_helper_unittest.cc | 99 | ||||
-rw-r--r-- | chrome/common/render_messages.h | 9 | ||||
-rw-r--r-- | chrome/renderer/resources/extensions/searchbox_api.js | 6 | ||||
-rw-r--r-- | chrome/renderer/searchbox/searchbox.cc | 15 | ||||
-rw-r--r-- | chrome/renderer/searchbox/searchbox.h | 5 | ||||
-rw-r--r-- | chrome/renderer/searchbox/searchbox_extension.cc | 43 | ||||
-rw-r--r-- | chrome/renderer/searchbox/searchbox_extension.h | 4 |
15 files changed, 313 insertions, 0 deletions
diff --git a/chrome/browser/ui/search/search_ipc_router.cc b/chrome/browser/ui/search/search_ipc_router.cc index 292efa7..ea639ee 100644 --- a/chrome/browser/ui/search/search_ipc_router.cc +++ b/chrome/browser/ui/search/search_ipc_router.cc @@ -60,6 +60,18 @@ void SearchIPCRouter::SendMostVisitedItems( routing_id(), items)); } +void SearchIPCRouter::SendChromeIdentityCheckResult( + const string16& identity, + bool identity_match) { + if (!policy_->ShouldProcessChromeIdentityCheck()) + return; + + Send(new ChromeViewMsg_ChromeIdentityCheckResult( + routing_id(), + identity, + identity_match)); +} + void SearchIPCRouter::SetSuggestionToPrefetch( const InstantSuggestion& suggestion) { if (!policy_->ShouldSendSetSuggestionToPrefetch()) @@ -103,6 +115,8 @@ bool SearchIPCRouter::OnMessageReceived(const IPC::Message& message) { IPC_MESSAGE_HANDLER(ChromeViewHostMsg_LogEvent, OnLogEvent); IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PasteAndOpenDropdown, OnPasteAndOpenDropDown); + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ChromeIdentityCheck, + OnChromeIdentityCheck); IPC_MESSAGE_UNHANDLED(handled = false) IPC_END_MESSAGE_MAP() return handled; @@ -214,6 +228,18 @@ void SearchIPCRouter::OnPasteAndOpenDropDown(int page_id, delegate_->PasteIntoOmnibox(text); } +void SearchIPCRouter::OnChromeIdentityCheck(int page_id, + const string16& identity) const { + if (!web_contents()->IsActiveEntry(page_id)) + return; + + delegate_->OnInstantSupportDetermined(true); + if (!policy_->ShouldProcessChromeIdentityCheck()) + return; + + delegate_->OnChromeIdentityCheck(identity); +} + void SearchIPCRouter::set_delegate(Delegate* delegate) { DCHECK(delegate); delegate_ = delegate; diff --git a/chrome/browser/ui/search/search_ipc_router.h b/chrome/browser/ui/search/search_ipc_router.h index 13c57ce..dce4eb3 100644 --- a/chrome/browser/ui/search/search_ipc_router.h +++ b/chrome/browser/ui/search/search_ipc_router.h @@ -65,6 +65,11 @@ class SearchIPCRouter : public content::WebContentsObserver { // Called when the page wants to paste the |text| (or the clipboard contents // if the |text| is empty) into the omnibox. virtual void PasteIntoOmnibox(const string16& text) = 0; + + // Called when the SearchBox wants to verify the signed-in Chrome identity + // against the provided |identity|. Will make a round-trip to the browser + // and eventually return the result through SendChromeIdentityCheckResult. + virtual void OnChromeIdentityCheck(const string16& identity) = 0; }; // An interface to be implemented by consumers of SearchIPCRouter objects to @@ -84,6 +89,7 @@ class SearchIPCRouter : public content::WebContentsObserver { virtual bool ShouldProcessUndoAllMostVisitedDeletions() = 0; virtual bool ShouldProcessLogEvent() = 0; virtual bool ShouldProcessPasteIntoOmnibox(bool is_active_tab) = 0; + virtual bool ShouldProcessChromeIdentityCheck() = 0; virtual bool ShouldSendSetPromoInformation() = 0; virtual bool ShouldSendSetDisplayInstantResults() = 0; virtual bool ShouldSendSetSuggestionToPrefetch() = 0; @@ -110,6 +116,10 @@ class SearchIPCRouter : public content::WebContentsObserver { // Tells the renderer about the most visited items. void SendMostVisitedItems(const std::vector<InstantMostVisitedItem>& items); + // Tells the renderer about the result of the Chrome identity check. + void SendChromeIdentityCheckResult(const string16& identity, + bool identity_match); + // Tells the renderer about the current theme background. void SendThemeBackgroundInfo(const ThemeBackgroundInfo& theme_info); @@ -138,6 +148,10 @@ class SearchIPCRouter : public content::WebContentsObserver { FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, ProcessLogEvent); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, DoNotProcessLogEvent); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, + ProcessChromeIdentityCheck); + FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, + DoNotProcessChromeIdentityCheck); + FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, SendSetDisplayInstantResults); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, SendSetSuggestionToPrefetch); @@ -182,6 +196,8 @@ class SearchIPCRouter : public content::WebContentsObserver { DoNotSendSetPromoInformationMsg); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, ProcessLogEventMsg); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, IgnoreLogEventMsg); + FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, ProcessChromeIdentityCheckMsg); + FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, IgnoreChromeIdentityCheckMsg); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, ProcessNavigateToURLMsg); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, IgnoreNavigateToURLMsg); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, @@ -217,6 +233,7 @@ class SearchIPCRouter : public content::WebContentsObserver { void OnUndoAllMostVisitedDeletions(int page_id) const; void OnLogEvent(int page_id, NTPLoggingEventType event) const; void OnPasteAndOpenDropDown(int page_id, const string16& text) const; + void OnChromeIdentityCheck(int page_id, const string16& identity) const; // Used by unit tests to set a fake delegate. void set_delegate(Delegate* delegate); diff --git a/chrome/browser/ui/search/search_ipc_router_policy_impl.cc b/chrome/browser/ui/search/search_ipc_router_policy_impl.cc index 2dfb5b3..952d975 100644 --- a/chrome/browser/ui/search/search_ipc_router_policy_impl.cc +++ b/chrome/browser/ui/search/search_ipc_router_policy_impl.cc @@ -55,6 +55,10 @@ bool SearchIPCRouterPolicyImpl::ShouldProcessPasteIntoOmnibox( return is_active_tab && !is_incognito_ && chrome::IsInstantNTP(web_contents_); } +bool SearchIPCRouterPolicyImpl::ShouldProcessChromeIdentityCheck() { + return !is_incognito_ && chrome::IsInstantNTP(web_contents_); +} + bool SearchIPCRouterPolicyImpl::ShouldSendSetPromoInformation() { return !is_incognito_ && chrome::IsInstantNTP(web_contents_); } diff --git a/chrome/browser/ui/search/search_ipc_router_policy_impl.h b/chrome/browser/ui/search/search_ipc_router_policy_impl.h index 1a9eae6..7d5c92d 100644 --- a/chrome/browser/ui/search/search_ipc_router_policy_impl.h +++ b/chrome/browser/ui/search/search_ipc_router_policy_impl.h @@ -39,6 +39,7 @@ class SearchIPCRouterPolicyImpl : public SearchIPCRouter::Policy { virtual bool ShouldProcessUndoAllMostVisitedDeletions() OVERRIDE; virtual bool ShouldProcessLogEvent() OVERRIDE; virtual bool ShouldProcessPasteIntoOmnibox(bool is_active_tab) OVERRIDE; + virtual bool ShouldProcessChromeIdentityCheck() OVERRIDE; virtual bool ShouldSendSetPromoInformation() OVERRIDE; virtual bool ShouldSendSetDisplayInstantResults() OVERRIDE; virtual bool ShouldSendSetSuggestionToPrefetch() OVERRIDE; diff --git a/chrome/browser/ui/search/search_ipc_router_policy_unittest.cc b/chrome/browser/ui/search/search_ipc_router_policy_unittest.cc index 877541d..dacba35 100644 --- a/chrome/browser/ui/search/search_ipc_router_policy_unittest.cc +++ b/chrome/browser/ui/search/search_ipc_router_policy_unittest.cc @@ -102,6 +102,19 @@ TEST_F(SearchIPCRouterPolicyTest, DoNotProcessLogEvent) { ShouldProcessLogEvent()); } +TEST_F(SearchIPCRouterPolicyTest, ProcessChromeIdentityCheck) { + NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl)); + EXPECT_TRUE(GetSearchTabHelper()->ipc_router().policy()-> + ShouldProcessChromeIdentityCheck()); +} + +TEST_F(SearchIPCRouterPolicyTest, DoNotProcessChromeIdentityCheck) { + // Process message only if the underlying page is an InstantNTP. + NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar")); + EXPECT_FALSE(GetSearchTabHelper()->ipc_router().policy()-> + ShouldProcessChromeIdentityCheck()); +} + TEST_F(SearchIPCRouterPolicyTest, ProcessNavigateToURL) { NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl)); EXPECT_TRUE(GetSearchTabHelper()->ipc_router().policy()-> diff --git a/chrome/browser/ui/search/search_ipc_router_unittest.cc b/chrome/browser/ui/search/search_ipc_router_unittest.cc index 39bec6d..65be6b4 100644 --- a/chrome/browser/ui/search/search_ipc_router_unittest.cc +++ b/chrome/browser/ui/search/search_ipc_router_unittest.cc @@ -10,6 +10,7 @@ #include "base/memory/scoped_ptr.h" #include "base/metrics/field_trial.h" #include "base/strings/string16.h" +#include "base/strings/utf_string_conversions.h" #include "base/tuple.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search/search.h" @@ -52,6 +53,7 @@ class MockSearchIPCRouterDelegate : public SearchIPCRouter::Delegate { MOCK_METHOD0(OnUndoAllMostVisitedDeletions, void()); MOCK_METHOD1(OnLogEvent, void(NTPLoggingEventType event)); MOCK_METHOD1(PasteIntoOmnibox, void(const string16&)); + MOCK_METHOD1(OnChromeIdentityCheck, void(const string16& identity)); }; class MockSearchIPCRouterPolicy : public SearchIPCRouter::Policy { @@ -66,6 +68,7 @@ class MockSearchIPCRouterPolicy : public SearchIPCRouter::Policy { MOCK_METHOD0(ShouldProcessUndoAllMostVisitedDeletions, bool()); MOCK_METHOD0(ShouldProcessLogEvent, bool()); MOCK_METHOD1(ShouldProcessPasteIntoOmnibox, bool(bool)); + MOCK_METHOD0(ShouldProcessChromeIdentityCheck, bool()); MOCK_METHOD0(ShouldSendSetPromoInformation, bool()); MOCK_METHOD0(ShouldSendSetDisplayInstantResults, bool()); MOCK_METHOD0(ShouldSendSetSuggestionToPrefetch, bool()); @@ -366,6 +369,47 @@ TEST_F(SearchIPCRouterTest, IgnoreLogEventMsg) { GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message); } +TEST_F(SearchIPCRouterTest, ProcessChromeIdentityCheckMsg) { + const string16 test_identity = ASCIIToUTF16("foo@bar.com"); + NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl)); + process()->sink().ClearMessages(); + EXPECT_CALL(*mock_delegate(), OnChromeIdentityCheck(test_identity)).Times(1); + + content::WebContents* contents = web_contents(); + SetupMockDelegateAndPolicy(contents); + MockSearchIPCRouterPolicy* policy = + GetSearchIPCRouterPolicy(contents); + + EXPECT_CALL(*policy, ShouldProcessChromeIdentityCheck()).Times(1) + .WillOnce(testing::Return(true)); + + scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_ChromeIdentityCheck( + contents->GetRoutingID(), + contents->GetController().GetVisibleEntry()->GetPageID(), + test_identity)); + GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message); +} + +TEST_F(SearchIPCRouterTest, IgnoreChromeIdentityCheckMsg) { + const string16 test_identity = ASCIIToUTF16("foo@bar.com"); + NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar")); + process()->sink().ClearMessages(); + EXPECT_CALL(*mock_delegate(), OnChromeIdentityCheck(test_identity)).Times(0); + + content::WebContents* contents = web_contents(); + SetupMockDelegateAndPolicy(contents); + MockSearchIPCRouterPolicy* policy = + GetSearchIPCRouterPolicy(contents); + EXPECT_CALL(*policy, ShouldProcessChromeIdentityCheck()).Times(1) + .WillOnce(testing::Return(false)); + + scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_ChromeIdentityCheck( + contents->GetRoutingID(), + contents->GetController().GetVisibleEntry()->GetPageID(), + test_identity)); + GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message); +} + TEST_F(SearchIPCRouterTest, ProcessDeleteMostVisitedItemMsg) { NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar")); process()->sink().ClearMessages(); diff --git a/chrome/browser/ui/search/search_tab_helper.cc b/chrome/browser/ui/search/search_tab_helper.cc index 47e9aa5..f9dc05a 100644 --- a/chrome/browser/ui/search/search_tab_helper.cc +++ b/chrome/browser/ui/search/search_tab_helper.cc @@ -9,6 +9,7 @@ #include "base/memory/scoped_ptr.h" #include "base/metrics/histogram.h" #include "base/strings/string16.h" +#include "base/strings/string_util.h" #include "build/build_config.h" #include "chrome/browser/history/most_visited_tiles_experiment.h" #include "chrome/browser/history/top_sites.h" @@ -16,6 +17,8 @@ #include "chrome/browser/search/instant_service.h" #include "chrome/browser/search/instant_service_factory.h" #include "chrome/browser/search/search.h" +#include "chrome/browser/signin/signin_manager.h" +#include "chrome/browser/signin/signin_manager_factory.h" #include "chrome/browser/ui/app_list/app_list_util.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_finder.h" @@ -507,6 +510,15 @@ void SearchTabHelper::PasteIntoOmnibox(const string16& text) { #endif } +void SearchTabHelper::OnChromeIdentityCheck(const string16& identity) { + SigninManagerBase* manager = SigninManagerFactory::GetForProfile(profile()); + if (manager) { + const string16 username = UTF8ToUTF16(manager->GetAuthenticatedUsername()); + ipc_router_.SendChromeIdentityCheckResult(identity, + identity == username); + } +} + void SearchTabHelper::UpdateMode(bool update_origin, bool is_preloaded_ntp) { SearchMode::Type type = SearchMode::MODE_DEFAULT; SearchMode::Origin origin = SearchMode::ORIGIN_DEFAULT; diff --git a/chrome/browser/ui/search/search_tab_helper.h b/chrome/browser/ui/search/search_tab_helper.h index 516266a..fb4c54f7 100644 --- a/chrome/browser/ui/search/search_tab_helper.h +++ b/chrome/browser/ui/search/search_tab_helper.h @@ -92,12 +92,24 @@ class SearchTabHelper : public content::NotificationObserver, DetermineIfPageSupportsInstant_NonLocal); FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, PageURLDoesntBelongToInstantRenderer); + FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, + OnChromeIdentityCheckMatch); + FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, + OnChromeIdentityCheckMismatch); + FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, + OnChromeIdentityCheckSignedOutMatch); + FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, + OnChromeIdentityCheckSignedOutMismatch); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, ProcessVoiceSearchSupportMsg); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, ProcessFocusOmnibox); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, DoNotProcessFocusOmnibox); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, ProcessLogEvent); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, DoNotProcessLogEvent); + FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, + ProcessChromeIdentityCheck); + FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, + DoNotProcessChromeIdentityCheck); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, SendSetPromoInformation); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterPolicyTest, DoNotSendSetPromoInformation); @@ -141,6 +153,8 @@ class SearchTabHelper : public content::NotificationObserver, DoNotSendSetPromoInformationMsg); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, ProcessLogEventMsg); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, IgnoreLogEventMsg); + FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, ProcessChromeIdentityCheckMsg); + FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, IgnoreChromeIdentityCheckMsg); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, ProcessNavigateToURLMsg); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, IgnoreNavigateToURLMsg); FRIEND_TEST_ALL_PREFIXES(SearchIPCRouterTest, @@ -215,6 +229,7 @@ class SearchTabHelper : public content::NotificationObserver, virtual void OnUndoAllMostVisitedDeletions() OVERRIDE; virtual void OnLogEvent(NTPLoggingEventType event) OVERRIDE; virtual void PasteIntoOmnibox(const string16& text) OVERRIDE; + virtual void OnChromeIdentityCheck(const string16& identity) OVERRIDE; // Overridden from InstantServiceObserver: virtual void ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) OVERRIDE; diff --git a/chrome/browser/ui/search/search_tab_helper_unittest.cc b/chrome/browser/ui/search/search_tab_helper_unittest.cc index 22e4e53..f6d6ab97 100644 --- a/chrome/browser/ui/search/search_tab_helper_unittest.cc +++ b/chrome/browser/ui/search/search_tab_helper_unittest.cc @@ -6,6 +6,9 @@ #include "base/command_line.h" #include "base/memory/scoped_ptr.h" +#include "base/strings/utf_string_conversions.h" +#include "chrome/browser/signin/fake_signin_manager.h" +#include "chrome/browser/signin/signin_manager_factory.h" #include "chrome/browser/ui/search/search_ipc_router.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/ntp_logging_events.h" @@ -13,6 +16,7 @@ #include "chrome/common/render_messages.h" #include "chrome/common/url_constants.h" #include "chrome/test/base/chrome_render_view_host_test_harness.h" +#include "chrome/test/base/testing_profile.h" #include "content/public/browser/navigation_controller.h" #include "content/public/browser/navigation_entry.h" #include "content/public/browser/web_contents.h" @@ -38,6 +42,7 @@ class MockSearchIPCRouterDelegate : public SearchIPCRouter::Delegate { MOCK_METHOD0(OnUndoAllMostVisitedDeletions, void()); MOCK_METHOD1(OnLogEvent, void(NTPLoggingEventType event)); MOCK_METHOD1(PasteIntoOmnibox, void(const string16&)); + MOCK_METHOD1(OnChromeIdentityCheck, void(const string16& identity)); }; } // namespace @@ -51,6 +56,20 @@ class SearchTabHelperTest : public ChromeRenderViewHostTestHarness { SearchTabHelper::CreateForWebContents(web_contents()); } + // Creates a sign-in manager for tests. If |username| is not empty, the + // testing profile of the WebContents will be connected to the given account. + void CreateSigninManager(const std::string& username) { + SigninManagerBase* signin_manager = static_cast<SigninManagerBase*>( + SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( + profile(), FakeSigninManagerBase::Build)); + signin_manager->Initialize(profile(), NULL); + + if (!username.empty()) { + ASSERT_TRUE(signin_manager); + signin_manager->SetAuthenticatedUsername(username); + } + } + bool MessageWasSent(uint32 id) { return process()->sink().GetFirstMessageMatching(id) != NULL; } @@ -108,3 +127,83 @@ TEST_F(SearchTabHelperTest, PageURLDoesntBelongToInstantRenderer) { ASSERT_FALSE(MessageWasSent( ChromeViewMsg_DetermineIfPageSupportsInstant::ID)); } + +TEST_F(SearchTabHelperTest, OnChromeIdentityCheckMatch) { + NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); + CreateSigninManager(std::string("foo@bar.com")); + SearchTabHelper* search_tab_helper = + SearchTabHelper::FromWebContents(web_contents()); + ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); + + const string16 test_identity = ASCIIToUTF16("foo@bar.com"); + search_tab_helper->OnChromeIdentityCheck(test_identity); + + const IPC::Message* message = process()->sink().GetUniqueMessageMatching( + ChromeViewMsg_ChromeIdentityCheckResult::ID); + ASSERT_TRUE(message != NULL); + + ChromeViewMsg_ChromeIdentityCheckResult::Param params; + ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); + EXPECT_EQ(test_identity, params.a); + ASSERT_TRUE(params.b); +} + +TEST_F(SearchTabHelperTest, OnChromeIdentityCheckMismatch) { + NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); + CreateSigninManager(std::string("foo@bar.com")); + SearchTabHelper* search_tab_helper = + SearchTabHelper::FromWebContents(web_contents()); + ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); + + const string16 test_identity = ASCIIToUTF16("bar@foo.com"); + search_tab_helper->OnChromeIdentityCheck(test_identity); + + const IPC::Message* message = process()->sink().GetUniqueMessageMatching( + ChromeViewMsg_ChromeIdentityCheckResult::ID); + ASSERT_TRUE(message != NULL); + + ChromeViewMsg_ChromeIdentityCheckResult::Param params; + ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); + EXPECT_EQ(test_identity, params.a); + ASSERT_FALSE(params.b); +} + +TEST_F(SearchTabHelperTest, OnChromeIdentityCheckSignedOutMatch) { + NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); + // This test does not sign in. + SearchTabHelper* search_tab_helper = + SearchTabHelper::FromWebContents(web_contents()); + ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); + + const string16 test_identity; + search_tab_helper->OnChromeIdentityCheck(test_identity); + + const IPC::Message* message = process()->sink().GetUniqueMessageMatching( + ChromeViewMsg_ChromeIdentityCheckResult::ID); + ASSERT_TRUE(message != NULL); + + ChromeViewMsg_ChromeIdentityCheckResult::Param params; + ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); + EXPECT_EQ(test_identity, params.a); + ASSERT_TRUE(params.b); +} + +TEST_F(SearchTabHelperTest, OnChromeIdentityCheckSignedOutMismatch) { + NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); + // This test does not sign in. + SearchTabHelper* search_tab_helper = + SearchTabHelper::FromWebContents(web_contents()); + ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); + + const string16 test_identity = ASCIIToUTF16("bar@foo.com"); + search_tab_helper->OnChromeIdentityCheck(test_identity); + + const IPC::Message* message = process()->sink().GetUniqueMessageMatching( + ChromeViewMsg_ChromeIdentityCheckResult::ID); + ASSERT_TRUE(message != NULL); + + ChromeViewMsg_ChromeIdentityCheckResult::Param params; + ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); + EXPECT_EQ(test_identity, params.a); + ASSERT_FALSE(params.b); +} diff --git a/chrome/common/render_messages.h b/chrome/common/render_messages.h index 2913e86..2d44c62 100644 --- a/chrome/common/render_messages.h +++ b/chrome/common/render_messages.h @@ -333,6 +333,10 @@ IPC_MESSAGE_ROUTED1(ChromeViewMsg_SearchBoxSubmit, IPC_MESSAGE_ROUTED1(ChromeViewMsg_SearchBoxThemeChanged, ThemeBackgroundInfo /* value */) +IPC_MESSAGE_ROUTED2(ChromeViewMsg_ChromeIdentityCheckResult, + string16 /* identity */, + bool /* identity_match */) + IPC_MESSAGE_ROUTED0(ChromeViewMsg_SearchBoxToggleVoiceSearch) // Toggles visual muting of the render view area. This is on when a constrained @@ -712,6 +716,11 @@ IPC_MESSAGE_ROUTED2(ChromeViewHostMsg_LogEvent, int /* page_id */, NTPLoggingEventType /* event */) +// The Instant page asks for Chrome identity check against |identity|. +IPC_MESSAGE_ROUTED2(ChromeViewHostMsg_ChromeIdentityCheck, + int /* page_id */, + string16 /* identity */) + // Tells InstantExtended to set the omnibox focus state. IPC_MESSAGE_ROUTED2(ChromeViewHostMsg_FocusOmnibox, int /* page_id */, diff --git a/chrome/renderer/resources/extensions/searchbox_api.js b/chrome/renderer/resources/extensions/searchbox_api.js index a6541e7..5b2d441 100644 --- a/chrome/renderer/resources/extensions/searchbox_api.js +++ b/chrome/renderer/resources/extensions/searchbox_api.js @@ -80,6 +80,7 @@ if (!chrome.embeddedSearch) { // ======================================================================= // Private functions // ======================================================================= + native function CheckIsUserSignedInToChromeAs(); native function DeleteMostVisitedItem(); native function GetAppLauncherEnabled(); native function GetMostVisitedItems(); @@ -123,6 +124,10 @@ if (!chrome.embeddedSearch) { DeleteMostVisitedItem(restrictedId); }; + this.checkIsUserSignedIntoChromeAs = function(identity) { + CheckIsUserSignedInToChromeAs(identity); + }; + // This method is restricted to chrome-search://most-visited pages by // checking the invoking context's origin in searchbox_extension.cc. this.logEvent = function(histogram_name) { @@ -141,6 +146,7 @@ if (!chrome.embeddedSearch) { UndoMostVisitedDeletion(restrictedId); }; + this.onsignedincheckdone = null; this.oninputcancel = null; this.oninputstart = null; this.onmostvisitedchange = null; diff --git a/chrome/renderer/searchbox/searchbox.cc b/chrome/renderer/searchbox/searchbox.cc index b6f3cab..874232c 100644 --- a/chrome/renderer/searchbox/searchbox.cc +++ b/chrome/renderer/searchbox/searchbox.cc @@ -162,6 +162,11 @@ void SearchBox::LogEvent(NTPLoggingEventType event) { render_view()->GetRoutingID(), render_view()->GetPageId(), event)); } +void SearchBox::CheckIsUserSignedInToChromeAs(const string16& identity) { + render_view()->Send(new ChromeViewHostMsg_ChromeIdentityCheck( + render_view()->GetRoutingID(), render_view()->GetPageId(), identity)); +} + void SearchBox::DeleteMostVisitedItem( InstantRestrictedID most_visited_item_id) { render_view()->Send(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem( @@ -273,6 +278,8 @@ void SearchBox::UndoMostVisitedDeletion( bool SearchBox::OnMessageReceived(const IPC::Message& message) { bool handled = true; IPC_BEGIN_MESSAGE_MAP(SearchBox, message) + IPC_MESSAGE_HANDLER(ChromeViewMsg_ChromeIdentityCheckResult, + OnChromeIdentityCheckResult) IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, OnDetermineIfPageSupportsInstant) IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxFocusChanged, OnFocusChanged) @@ -297,6 +304,14 @@ bool SearchBox::OnMessageReceived(const IPC::Message& message) { return handled; } +void SearchBox::OnChromeIdentityCheckResult(const string16& identity, + bool identity_match) { + if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { + extensions_v8::SearchBoxExtension::DispatchChromeIdentityCheckResult( + render_view()->GetWebView()->mainFrame(), identity, identity_match); + } +} + void SearchBox::OnDetermineIfPageSupportsInstant() { if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( diff --git a/chrome/renderer/searchbox/searchbox.h b/chrome/renderer/searchbox/searchbox.h index 51247db..8ca4de3 100644 --- a/chrome/renderer/searchbox/searchbox.h +++ b/chrome/renderer/searchbox/searchbox.h @@ -31,6 +31,9 @@ class SearchBox : public content::RenderViewObserver, // Sends ChromeViewHostMsg_LogEvent to the browser. void LogEvent(NTPLoggingEventType event); + // Sends ChromeViewHostMsg_ChromeIdentityCheck to the browser. + void CheckIsUserSignedInToChromeAs(const string16& identity); + // Sends ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem to the browser. void DeleteMostVisitedItem(InstantRestrictedID most_visited_item_id); @@ -107,6 +110,8 @@ class SearchBox : public content::RenderViewObserver, // Overridden from content::RenderViewObserver: virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; + void OnChromeIdentityCheckResult(const string16& identity, + bool identity_match); void OnDetermineIfPageSupportsInstant(); void OnFocusChanged(OmniboxFocusState new_focus_state, OmniboxFocusChangeReason reason); diff --git a/chrome/renderer/searchbox/searchbox_extension.cc b/chrome/renderer/searchbox/searchbox_extension.cc index 9dc6ca1..dc5d046 100644 --- a/chrome/renderer/searchbox/searchbox_extension.cc +++ b/chrome/renderer/searchbox/searchbox_extension.cc @@ -5,6 +5,7 @@ #include "chrome/renderer/searchbox/searchbox_extension.h" #include "base/i18n/rtl.h" +#include "base/json/string_escape.h" #include "base/strings/string_number_conversions.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" @@ -193,6 +194,18 @@ static const char kSupportsInstantScript[] = " false;" "}"; +static const char kDispatchChromeIdentityCheckResult[] = + "if (window.chrome &&" + " window.chrome.embeddedSearch &&" + " window.chrome.embeddedSearch.newTabPage &&" + " window.chrome.embeddedSearch.newTabPage.onsignedincheckdone &&" + " typeof window.chrome.embeddedSearch.newTabPage" + " .onsignedincheckdone === 'function') {" + " window.chrome.embeddedSearch.newTabPage.onsignedincheckdone(%s, %s);" + " true;" + "}"; + + static const char kDispatchFocusChangedScript[] = "if (window.chrome &&" " window.chrome.embeddedSearch &&" @@ -317,6 +330,10 @@ class SearchBoxExtensionWrapper : public v8::Extension { // Helper function to find the RenderView. May return NULL. static content::RenderView* GetRenderView(); + // Sends a Chrome identity check to the browser. + static void CheckIsUserSignedInToChromeAs( + const v8::FunctionCallbackInfo<v8::Value>& args); + // Deletes a Most Visited item. static void DeleteMostVisitedItem( const v8::FunctionCallbackInfo<v8::Value>& args); @@ -422,6 +439,18 @@ bool SearchBoxExtension::PageSupportsInstant(WebKit::WebFrame* frame) { } // static +void SearchBoxExtension::DispatchChromeIdentityCheckResult( + WebKit::WebFrame* frame, const string16& identity, bool identity_match) { + std::string escaped_identity; + base::JsonDoubleQuote(identity, true, &escaped_identity); + WebKit::WebString script(UTF8ToUTF16(base::StringPrintf( + kDispatchChromeIdentityCheckResult, + escaped_identity.c_str(), + identity_match ? "true" : "false"))); + Dispatch(frame, script); +} + +// static void SearchBoxExtension::DispatchFocusChange(WebKit::WebFrame* frame) { Dispatch(frame, kDispatchFocusChangedScript); } @@ -480,6 +509,8 @@ SearchBoxExtensionWrapper::SearchBoxExtensionWrapper( v8::Handle<v8::FunctionTemplate> SearchBoxExtensionWrapper::GetNativeFunction( v8::Handle<v8::String> name) { + if (name->Equals(v8::String::New("CheckIsUserSignedInToChromeAs"))) + return v8::FunctionTemplate::New(CheckIsUserSignedInToChromeAs); if (name->Equals(v8::String::New("DeleteMostVisitedItem"))) return v8::FunctionTemplate::New(DeleteMostVisitedItem); if (name->Equals(v8::String::New("Focus"))) @@ -539,6 +570,18 @@ content::RenderView* SearchBoxExtensionWrapper::GetRenderView() { } // static +void SearchBoxExtensionWrapper::CheckIsUserSignedInToChromeAs( + const v8::FunctionCallbackInfo<v8::Value>& args) { + content::RenderView* render_view = GetRenderView(); + if (!render_view || args.Length() == 0 || args[0]->IsUndefined()) return; + + DVLOG(1) << render_view << " CheckIsUserSignedInToChromeAs"; + + SearchBox::Get(render_view)->CheckIsUserSignedInToChromeAs( + V8ValueToUTF16(args[0])); +} + +// static void SearchBoxExtensionWrapper::DeleteMostVisitedItem( const v8::FunctionCallbackInfo<v8::Value>& args) { content::RenderView* render_view = GetRenderView(); diff --git a/chrome/renderer/searchbox/searchbox_extension.h b/chrome/renderer/searchbox/searchbox_extension.h index 5135d64..682b7e1 100644 --- a/chrome/renderer/searchbox/searchbox_extension.h +++ b/chrome/renderer/searchbox/searchbox_extension.h @@ -6,6 +6,7 @@ #define CHROME_RENDERER_SEARCHBOX_SEARCHBOX_EXTENSION_H_ #include "base/basictypes.h" +#include "base/strings/string16.h" namespace v8 { class Extension; @@ -30,6 +31,9 @@ class SearchBoxExtension { static bool PageSupportsInstant(WebKit::WebFrame* frame); // Helpers to dispatch Javascript events. + static void DispatchChromeIdentityCheckResult(WebKit::WebFrame* frame, + const string16& identity, + bool identity_match); static void DispatchFocusChange(WebKit::WebFrame* frame); static void DispatchInputCancel(WebKit::WebFrame* frame); static void DispatchInputStart(WebKit::WebFrame* frame); |