diff options
author | groby@chromium.org <groby@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-06 01:27:06 +0000 |
---|---|---|
committer | groby@chromium.org <groby@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-06 01:27:06 +0000 |
commit | 35851fd06ea6468e9a5ac07c7e3a8f668b5accf4 (patch) | |
tree | 88d926862391fc4c3e8f6d393373470524e3d060 | |
parent | 18703054288785e443a9051ff74f0418eea6e9e9 (diff) | |
download | chromium_src-35851fd06ea6468e9a5ac07c7e3a8f668b5accf4.zip chromium_src-35851fd06ea6468e9a5ac07c7e3a8f668b5accf4.tar.gz chromium_src-35851fd06ea6468e9a5ac07c7e3a8f668b5accf4.tar.bz2 |
Initial version of web intents registry
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/7511005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@95710 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/intents/web_intent_data.h | 19 | ||||
-rw-r--r-- | chrome/browser/intents/web_intents_registry.cc | 80 | ||||
-rw-r--r-- | chrome/browser/intents/web_intents_registry.h | 73 | ||||
-rw-r--r-- | chrome/browser/intents/web_intents_registry_unittest.cc | 101 | ||||
-rw-r--r-- | chrome/browser/webdata/web_intents_table.h | 11 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 2 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 |
7 files changed, 278 insertions, 9 deletions
diff --git a/chrome/browser/intents/web_intent_data.h b/chrome/browser/intents/web_intent_data.h new file mode 100644 index 0000000..9d4b006 --- /dev/null +++ b/chrome/browser/intents/web_intent_data.h @@ -0,0 +1,19 @@ +// Copyright (c) 2011 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_INTENTS_WEB_INTENT_DATA_H_ +#define CHROME_BROWSER_INTENTS_WEB_INTENT_DATA_H_ +#pragma once + +#include "base/string16.h" +#include "googleurl/src/gurl.h" + +// Describes the relevant elements of a WebIntent. +struct WebIntentData { + GURL service_url; // URL for service invocation. + string16 action; // Name of action provided by service. + string16 type; // MIME type of data accepted by service. +}; + +#endif // CHROME_BROWSER_INTENTS_WEB_INTENT_DATA_H_ diff --git a/chrome/browser/intents/web_intents_registry.cc b/chrome/browser/intents/web_intents_registry.cc new file mode 100644 index 0000000..444bcca --- /dev/null +++ b/chrome/browser/intents/web_intents_registry.cc @@ -0,0 +1,80 @@ +// Copyright (c) 2011 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/intents/web_intents_registry.h" +#include "chrome/browser/webdata/web_data_service.h" + +// Internal object representing all data associated with a single query. +struct WebIntentsRegistry::IntentsQuery { + // Unique query identifier. + QueryID query_id_; + + // Underlying data query. + WebDataService::Handle pending_query_; + + // the consumer for this particular query. + Consumer* consumer_; + + // TODO(groby): Additional filter data will go here - filtering is handled + // per query. +}; + +WebIntentsRegistry::WebIntentsRegistry() : next_query_id_(0) {} + +WebIntentsRegistry::~WebIntentsRegistry() { + // Cancel all pending queries, since we can't handle them any more. + for (QueryMap::iterator it(queries.begin()); it != queries.end(); ++it) { + wds_->CancelRequest(it->first); + delete it->second; + } +} + +void WebIntentsRegistry::Initialize(scoped_refptr<WebDataService> wds) { + wds_ = wds; +} + +void WebIntentsRegistry::OnWebDataServiceRequestDone( + WebDataService::Handle h, + const WDTypedResult* result) { + DCHECK(result); + DCHECK(result->GetType() == WEB_INTENTS_RESULT); + + QueryMap::iterator it = queries.find(h); + DCHECK(it != queries.end()); + + IntentsQuery* query(it->second); + DCHECK(query); + queries.erase(it); + + // TODO(groby): Filtering goes here. + std::vector<WebIntentData> intents = static_cast< + const WDResult<std::vector<WebIntentData> >*>(result)->GetValue(); + + query->consumer_->OnIntentsQueryDone(query->query_id_, intents); +} + +WebIntentsRegistry::QueryID WebIntentsRegistry::GetIntentProviders( + const string16& action, + Consumer* consumer) { + DCHECK(consumer); + DCHECK(wds_.get()); + + IntentsQuery* query = new IntentsQuery; + query->query_id_ = next_query_id_++; + query->consumer_ = consumer; + query->pending_query_ = wds_->GetWebIntents(action, this); + queries[query->pending_query_] = query; + + return query->query_id_; +} + +void WebIntentsRegistry::RegisterIntentProvider(const WebIntentData& intent) { + DCHECK(wds_.get()); + wds_->AddWebIntent(intent); +} + +void WebIntentsRegistry::UnregisterIntentProvider(const WebIntentData& intent) { + DCHECK(wds_.get()); + wds_->RemoveWebIntent(intent); +} diff --git a/chrome/browser/intents/web_intents_registry.h b/chrome/browser/intents/web_intents_registry.h new file mode 100644 index 0000000..955a282 --- /dev/null +++ b/chrome/browser/intents/web_intents_registry.h @@ -0,0 +1,73 @@ +// Copyright (c) 2011 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_INTENTS_WEB_INTENTS_REGISTRY_H_ +#define CHROME_BROWSER_INTENTS_WEB_INTENTS_REGISTRY_H_ +#pragma once + +#include "base/hash_tables.h" +#include "base/memory/ref_counted.h" +#include "chrome/browser/intents/web_intent_data.h" +#include "chrome/browser/webdata/web_data_service.h" + +// Handles storing and retrieving of web intents in the web database. +// The registry provides filtering logic to retrieve specific types of intents. +class WebIntentsRegistry : public WebDataServiceConsumer { + public: + // Unique identifier for intent queries. + typedef int QueryID; + + // An interface the WebIntentsRegistry uses to notify its clients when + // it has finished loading intents data from the web database. + class Consumer { + public: + // Notifies the observer that the intents request has been completed. + virtual void OnIntentsQueryDone( + QueryID query_id, + const std::vector<WebIntentData>& intents) = 0; + + protected: + virtual ~Consumer() {} + }; + + WebIntentsRegistry(); + virtual ~WebIntentsRegistry(); + + // Initializes, binds to a valid WebDataService. + void Initialize(scoped_refptr<WebDataService> wds); + + // Registers a web intent provider. + void RegisterIntentProvider(const WebIntentData& intent); + + // Removes a web intent provider from the registry. + void UnregisterIntentProvider(const WebIntentData& intent); + + // Requests all intent providers matching |action|. + // |consumer| must not be NULL. + QueryID GetIntentProviders(const string16& action, Consumer* consumer); + + private: + struct IntentsQuery; + + // Maps web data requests to intents queries. + // Allows OnWebDataServiceRequestDone to forward to appropiate consumer. + typedef base::hash_map<WebDataService::Handle, IntentsQuery*> QueryMap; + + // WebDataServiceConsumer implementation. + virtual void OnWebDataServiceRequestDone(WebDataService::Handle h, + const WDTypedResult* result); + + // Map for all in-flight web data requests/intent queries. + QueryMap queries; + + // Unique identifier for next intent query. + QueryID next_query_id_; + + // Local reference to Web Data Service. + scoped_refptr<WebDataService> wds_; + + DISALLOW_COPY_AND_ASSIGN(WebIntentsRegistry); +}; + +#endif // CHROME_BROWSER_INTENTS_WEB_INTENTS_REGISTRY_H_ diff --git a/chrome/browser/intents/web_intents_registry_unittest.cc b/chrome/browser/intents/web_intents_registry_unittest.cc new file mode 100644 index 0000000..d8aa4cb --- /dev/null +++ b/chrome/browser/intents/web_intents_registry_unittest.cc @@ -0,0 +1,101 @@ +// Copyright (c) 2011 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/file_util.h" +#include "base/scoped_temp_dir.h" +#include "base/utf_string_conversions.h" +#include "chrome/browser/intents/web_intents_registry.h" +#include "chrome/browser/webdata/web_data_service.h" +#include "testing/gtest/include/gtest/gtest.h" + +class WebIntentsRegistryTest : public testing::Test { + public: + WebIntentsRegistryTest() + : ui_thread_(BrowserThread::UI, &message_loop_), + db_thread_(BrowserThread::DB) {} + + protected: + virtual void SetUp() { + db_thread_.Start(); + wds_ = new WebDataService(); + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + wds_->Init(temp_dir_.path()); + registry_.Initialize(wds_); + } + + virtual void TearDown() { + if (wds_.get()) + wds_->Shutdown(); + + db_thread_.Stop(); + MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask); + MessageLoop::current()->Run(); + } + + MessageLoopForUI message_loop_; + BrowserThread ui_thread_; + BrowserThread db_thread_; + scoped_refptr<WebDataService> wds_; + WebIntentsRegistry registry_; + ScopedTempDir temp_dir_; +}; + +// Simple consumer for WebIntentsRegistry notifications. Stores result data and +// terminates UI thread when callback is invoked. +class TestConsumer: public WebIntentsRegistry::Consumer { + public: + virtual void OnIntentsQueryDone(WebIntentsRegistry::QueryID id, + const std::vector<WebIntentData>& intents) { + DCHECK(id == expected_id_); + intents_ = intents; + + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + MessageLoop::current()->Quit(); + } + + // Wait for the UI message loop to terminate - happens when OnIntesQueryDone + // is invoked. + void WaitForData() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + MessageLoop::current()->Run(); + } + + WebIntentsRegistry::QueryID expected_id_; // QueryID callback is tied to. + std::vector<WebIntentData> intents_; // Result data from callback. +}; + +TEST_F(WebIntentsRegistryTest, BasicTests) { + WebIntentData intent; + intent.service_url = GURL("http://google.com"); + intent.action = ASCIIToUTF16("share"); + intent.type = ASCIIToUTF16("image/*"); + + registry_.RegisterIntentProvider(intent); + + intent.type = ASCIIToUTF16("video/*"); + registry_.RegisterIntentProvider(intent); + + intent.action = ASCIIToUTF16("search"); + registry_.RegisterIntentProvider(intent); + + TestConsumer consumer; + consumer.expected_id_ = registry_.GetIntentProviders(ASCIIToUTF16("share"), + &consumer); + consumer.WaitForData(); + EXPECT_EQ(2U, consumer.intents_.size()); + + consumer.expected_id_ = registry_.GetIntentProviders(ASCIIToUTF16("search"), + &consumer); + consumer.WaitForData(); + EXPECT_EQ(1U, consumer.intents_.size()); + + intent.action = ASCIIToUTF16("share"); + intent.type = ASCIIToUTF16("image/*"); + registry_.UnregisterIntentProvider(intent); + + consumer.expected_id_ = registry_.GetIntentProviders(ASCIIToUTF16("share"), + &consumer); + consumer.WaitForData(); + EXPECT_EQ(1U, consumer.intents_.size()); +} diff --git a/chrome/browser/webdata/web_intents_table.h b/chrome/browser/webdata/web_intents_table.h index b52d0d7..5ebf151 100644 --- a/chrome/browser/webdata/web_intents_table.h +++ b/chrome/browser/webdata/web_intents_table.h @@ -10,17 +10,10 @@ #include "base/basictypes.h" #include "base/string16.h" -#include "googleurl/src/gurl.h" +#include "chrome/browser/intents/web_intent_data.h" #include "chrome/browser/webdata/web_database_table.h" -// Describes the relevant elements of a WebIntent. -// TODO(groby): Will need to be moved to different place once more -// infrastructure for WebIntents is in place. -struct WebIntentData { - GURL service_url; // URL for service invocation. - string16 action; // Name of action provided by service. - string16 type; // MIME type of data accepted by service. -}; +class GURL; // This class manages the WebIntents table within the SQLite database passed // to the constructor. It expects the following schema: diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index bc96d2a..1ed1005 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1379,6 +1379,8 @@ 'browser/instant/instant_unload_handler.h', 'browser/instant/promo_counter.cc', 'browser/instant/promo_counter.h', + 'browser/intents/web_intents_registry.cc', + 'browser/intents/web_intents_registry.h', 'browser/intents/register_intent_handler_infobar_delegate.cc', 'browser/intents/register_intent_handler_infobar_delegate.h', 'browser/internal_auth.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 16d2c1c..640a8ab 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1484,6 +1484,7 @@ 'browser/importer/toolbar_importer_unittest.cc', 'browser/instant/instant_loader_manager_unittest.cc', 'browser/instant/promo_counter_unittest.cc', + 'browser/intents/web_intents_registry_unittest.cc', 'browser/internal_auth_unittest.cc', 'browser/language_usage_metrics_unittest.cc', 'browser/mac/keystone_glue_unittest.mm', |