Commit 8c5f8763 authored by Shakti Sahu's avatar Shakti Sahu Committed by Commit Bot

Video Tutorials : Hooked up dependencies for the service

This CL adds :
1 - Sending Country code, and accept language to the server.
2 - Provides dependencies for video tutorial service creation.

TBR=dtrainor@chromium.org

Bug: 1133657
Change-Id: I04ed5cfecb481810e8e24102aff2d4468b44f6cf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2438780Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Reviewed-by: default avatarBrian White <bcwhite@chromium.org>
Reviewed-by: default avatarssid <ssid@chromium.org>
Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Commit-Queue: Shakti Sahu <shaktisahu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812552}
parent a163a67b
...@@ -26,6 +26,8 @@ constexpr char kPreferredLocaleConfigKey[] = "default_locale"; ...@@ -26,6 +26,8 @@ constexpr char kPreferredLocaleConfigKey[] = "default_locale";
constexpr char kFetchFrequencyKey[] = "fetch_frequency"; constexpr char kFetchFrequencyKey[] = "fetch_frequency";
constexpr char kExperimentTagKey[] = "experiment_tag";
// Default frequency in days for fetching tutorial metatada from server. // Default frequency in days for fetching tutorial metatada from server.
constexpr int kDefaultFetchFrequencyDays = 15; constexpr int kDefaultFetchFrequencyDays = 15;
...@@ -64,4 +66,10 @@ base::TimeDelta Config::GetFetchFrequency() { ...@@ -64,4 +66,10 @@ base::TimeDelta Config::GetFetchFrequency() {
return base::TimeDelta::FromDays(frequency_in_days); return base::TimeDelta::FromDays(frequency_in_days);
} }
// static
std::string Config::GetExperimentTag() {
return base::GetFieldTrialParamValueByFeature(features::kVideoTutorials,
kExperimentTagKey);
}
} // namespace video_tutorials } // namespace video_tutorials
...@@ -41,6 +41,9 @@ class Config { ...@@ -41,6 +41,9 @@ class Config {
// Get the default fetch frequency. // Get the default fetch frequency.
static base::TimeDelta GetFetchFrequency(); static base::TimeDelta GetFetchFrequency();
// Gets the experiment tag to be passed to server.
static std::string GetExperimentTag();
}; };
} // namespace video_tutorials } // namespace video_tutorials
......
...@@ -50,12 +50,16 @@ class TutorialFetcherImpl : public TutorialFetcher { ...@@ -50,12 +50,16 @@ class TutorialFetcherImpl : public TutorialFetcher {
public: public:
TutorialFetcherImpl( TutorialFetcherImpl(
const GURL& url, const GURL& url,
const std::string& country_code,
const std::string& accept_languages,
const std::string& api_key, const std::string& api_key,
const std::string& experiment_tag, const std::string& experiment_tag,
const std::string& client_version, const std::string& client_version,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: url_loader_factory_(url_loader_factory), : url_loader_factory_(url_loader_factory),
url_(url), url_(url),
country_code_(country_code),
accept_languages_(accept_languages),
api_key_(api_key), api_key_(api_key),
experiment_tag_(experiment_tag), experiment_tag_(experiment_tag),
client_version_(client_version) {} client_version_(client_version) {}
...@@ -80,11 +84,16 @@ class TutorialFetcherImpl : public TutorialFetcher { ...@@ -80,11 +84,16 @@ class TutorialFetcherImpl : public TutorialFetcher {
request->headers.SetHeader("X-Client-Version", client_version_); request->headers.SetHeader("X-Client-Version", client_version_);
request->headers.SetHeader(net::HttpRequestHeaders::kContentType, request->headers.SetHeader(net::HttpRequestHeaders::kContentType,
kRequestContentType); kRequestContentType);
request->url = url_; request->url =
net::AppendOrReplaceQueryParameter(url_, "country_code", country_code_);
if (!experiment_tag_.empty()) { if (!experiment_tag_.empty()) {
request->url = net::AppendOrReplaceQueryParameter( request->url = net::AppendOrReplaceQueryParameter(
request->url, "experiment_tag", experiment_tag_); request->url, "experiment_tag", experiment_tag_);
} }
if (!accept_languages_.empty()) {
request->headers.SetHeader(net::HttpRequestHeaders::kAcceptLanguage,
accept_languages_);
}
if (!g_override_url_for_testing.Get().is_empty()) if (!g_override_url_for_testing.Get().is_empty())
request->url = g_override_url_for_testing.Get(); request->url = g_override_url_for_testing.Get();
...@@ -114,6 +123,8 @@ class TutorialFetcherImpl : public TutorialFetcher { ...@@ -114,6 +123,8 @@ class TutorialFetcherImpl : public TutorialFetcher {
// Params of resource request. // Params of resource request.
GURL url_; GURL url_;
std::string country_code_;
std::string accept_languages_;
std::string api_key_; std::string api_key_;
std::string experiment_tag_; std::string experiment_tag_;
std::string client_version_; std::string client_version_;
...@@ -126,12 +137,15 @@ class TutorialFetcherImpl : public TutorialFetcher { ...@@ -126,12 +137,15 @@ class TutorialFetcherImpl : public TutorialFetcher {
// static // static
std::unique_ptr<TutorialFetcher> TutorialFetcher::Create( std::unique_ptr<TutorialFetcher> TutorialFetcher::Create(
const GURL& url, const GURL& url,
const std::string& country_code,
const std::string& accept_languages,
const std::string& api_key, const std::string& api_key,
const std::string& experiment_tag, const std::string& experiment_tag,
const std::string& client_version, const std::string& client_version,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) { scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
return std::make_unique<TutorialFetcherImpl>( return std::make_unique<TutorialFetcherImpl>(
url, api_key, experiment_tag, client_version, url_loader_factory); url, country_code, accept_languages, api_key, experiment_tag,
client_version, url_loader_factory);
} }
// static // static
......
...@@ -30,6 +30,8 @@ class TutorialFetcher { ...@@ -30,6 +30,8 @@ class TutorialFetcher {
// Method to create a TutorialFetcher. // Method to create a TutorialFetcher.
static std::unique_ptr<TutorialFetcher> Create( static std::unique_ptr<TutorialFetcher> Create(
const GURL& url, const GURL& url,
const std::string& country_code,
const std::string& accept_languages,
const std::string& api_key, const std::string& api_key,
const std::string& experiment_tag, const std::string& experiment_tag,
const std::string& client_version, const std::string& client_version,
......
...@@ -95,8 +95,9 @@ TutorialFetcher::FinishedCallback TutorialFetcherTest::StoreResult() { ...@@ -95,8 +95,9 @@ TutorialFetcher::FinishedCallback TutorialFetcherTest::StoreResult() {
} }
std::unique_ptr<TutorialFetcher> TutorialFetcherTest::CreateFetcher() { std::unique_ptr<TutorialFetcher> TutorialFetcherTest::CreateFetcher() {
std::unique_ptr<TutorialFetcher> fetcher = TutorialFetcher::Create( std::unique_ptr<TutorialFetcher> fetcher =
GURL(kServerURL), kGoogleApiKey, "", "", test_shared_url_loader_factory_); TutorialFetcher::Create(GURL(kServerURL), "", "", kGoogleApiKey, "", "",
test_shared_url_loader_factory_);
return fetcher; return fetcher;
} }
...@@ -188,7 +189,8 @@ TEST_F(TutorialFetcherTest, Success) { ...@@ -188,7 +189,8 @@ TEST_F(TutorialFetcherTest, Success) {
EXPECT_EQ(true, RunFetcherWithData(kVideoTutorialsMessage, &data)); EXPECT_EQ(true, RunFetcherWithData(kVideoTutorialsMessage, &data));
EXPECT_EQ(kVideoTutorialsMessage, data); EXPECT_EQ(kVideoTutorialsMessage, data);
EXPECT_EQ(last_resource_request().url.spec(), "https://www.test.com/"); EXPECT_EQ(last_resource_request().url.spec(),
"https://www.test.com/?country_code=");
} }
} // namespace video_tutorials } // namespace video_tutorials
...@@ -6,13 +6,55 @@ ...@@ -6,13 +6,55 @@
#include <utility> #include <utility>
#include "base/sequenced_task_runner.h"
#include "base/task/post_task.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/video_tutorials/internal/config.h"
#include "chrome/browser/video_tutorials/internal/tutorial_fetcher.h"
#include "chrome/browser/video_tutorials/internal/tutorial_manager_impl.h"
#include "chrome/browser/video_tutorials/internal/tutorial_service_impl.h" #include "chrome/browser/video_tutorials/internal/tutorial_service_impl.h"
#include "chrome/browser/video_tutorials/internal/tutorial_store.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "components/leveldb_proto/public/shared_proto_database_client_list.h"
#include "components/prefs/pref_service.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace video_tutorials { namespace video_tutorials {
namespace {
const base::FilePath::CharType kVideoTutorialsDbName[] =
FILE_PATH_LITERAL("VideoTutorialsDatabase");
} // namespace
std::unique_ptr<VideoTutorialService> CreateVideoTutorialService() { std::unique_ptr<VideoTutorialService> CreateVideoTutorialService(
// TODO(shaktisahu): Pass correct values. leveldb_proto::ProtoDatabaseProvider* db_provider,
return std::make_unique<TutorialServiceImpl>(nullptr, nullptr, nullptr); const base::FilePath& storage_dir,
const std::string& accepted_language,
const std::string& country_code,
const std::string& api_key,
const std::string& client_version,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
PrefService* pref_service) {
// Create tutorial store and manager.
auto task_runner = base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE});
base::FilePath database_dir = storage_dir.Append(kVideoTutorialsDbName);
auto tutorial_db =
db_provider
->GetDB<video_tutorials::proto::VideoTutorialGroup, TutorialGroup>(
leveldb_proto::ProtoDbType::VIDEO_TUTORIALS_DATABASE,
database_dir, task_runner);
auto tutorial_store = std::make_unique<TutorialStore>(std::move(tutorial_db));
auto tutorial_manager = std::make_unique<TutorialManagerImpl>(
std::move(tutorial_store), pref_service);
// Create fetcher.
auto fetcher = TutorialFetcher::Create(
Config::GetTutorialsServerURL(), country_code, accepted_language, api_key,
Config::GetExperimentTag(), client_version, url_loader_factory);
auto tutorial_service_impl = std::make_unique<TutorialServiceImpl>(
std::move(tutorial_manager), std::move(fetcher), pref_service);
return std::move(tutorial_service_impl);
} }
} // namespace video_tutorials } // namespace video_tutorials
...@@ -6,12 +6,34 @@ ...@@ -6,12 +6,34 @@
#define CHROME_BROWSER_VIDEO_TUTORIALS_TUTORIAL_FACTORY_HELPER_H_ #define CHROME_BROWSER_VIDEO_TUTORIALS_TUTORIAL_FACTORY_HELPER_H_
#include <memory> #include <memory>
#include <string>
#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
namespace leveldb_proto {
class ProtoDatabaseProvider;
} // namespace leveldb_proto
namespace network {
class SharedURLLoaderFactory;
} // namespace network
class PrefService;
namespace video_tutorials { namespace video_tutorials {
class VideoTutorialService; class VideoTutorialService;
std::unique_ptr<VideoTutorialService> CreateVideoTutorialService(); std::unique_ptr<VideoTutorialService> CreateVideoTutorialService(
leveldb_proto::ProtoDatabaseProvider* db_provider,
const base::FilePath& storage_dir,
const std::string& accepted_language,
const std::string& country_code,
const std::string& api_key,
const std::string& client_version,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
PrefService* pref_service);
} // namespace video_tutorials } // namespace video_tutorials
......
...@@ -5,13 +5,46 @@ ...@@ -5,13 +5,46 @@
#include "chrome/browser/video_tutorials/video_tutorial_service_factory.h" #include "chrome/browser/video_tutorials/video_tutorial_service_factory.h"
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/image_fetcher/image_fetcher_service_factory.h" #include "chrome/browser/image_fetcher/image_fetcher_service_factory.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile_key.h" #include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/video_tutorials/tutorial_factory_helper.h" #include "chrome/browser/video_tutorials/tutorial_factory_helper.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/chrome_constants.h"
#include "components/background_task_scheduler/background_task_scheduler_factory.h" #include "components/background_task_scheduler/background_task_scheduler_factory.h"
#include "components/keyed_service/core/simple_dependency_manager.h" #include "components/keyed_service/core/simple_dependency_manager.h"
#include "components/language/core/browser/locale_util.h"
#include "components/language/core/browser/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/variations/service/variations_service.h"
#include "components/version_info/version_info.h"
#include "google_apis/google_api_keys.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace video_tutorials { namespace video_tutorials {
namespace {
std::string GetCountryCode() {
std::string country_code;
auto* variations_service = g_browser_process->variations_service();
if (variations_service) {
country_code = variations_service->GetStoredPermanentCountry();
if (!country_code.empty())
return country_code;
country_code = variations_service->GetLatestCountry();
}
return country_code;
}
std::string GetGoogleAPIKey() {
bool is_stable_channel =
chrome::GetChannel() == version_info::Channel::STABLE;
return is_stable_channel ? google_apis::GetAPIKey()
: google_apis::GetNonStableAPIKey();
}
} // namespace
// static // static
VideoTutorialServiceFactory* VideoTutorialServiceFactory::GetInstance() { VideoTutorialServiceFactory* VideoTutorialServiceFactory::GetInstance() {
...@@ -35,7 +68,33 @@ VideoTutorialServiceFactory::VideoTutorialServiceFactory() ...@@ -35,7 +68,33 @@ VideoTutorialServiceFactory::VideoTutorialServiceFactory()
std::unique_ptr<KeyedService> std::unique_ptr<KeyedService>
VideoTutorialServiceFactory::BuildServiceInstanceFor( VideoTutorialServiceFactory::BuildServiceInstanceFor(
SimpleFactoryKey* key) const { SimpleFactoryKey* key) const {
return CreateVideoTutorialService(); auto* db_provider =
ProfileKey::FromSimpleFactoryKey(key)->GetProtoDatabaseProvider();
// |storage_dir| is not actually used since we are using the shared leveldb.
base::FilePath storage_dir =
ProfileKey::FromSimpleFactoryKey(key)->GetPath().Append(
chrome::kVideoTutorialsStorageDirname);
std::string accept_languanges =
ProfileKey::FromSimpleFactoryKey(key)->GetPrefs()->GetString(
language::prefs::kAcceptLanguages);
auto url_loader_factory =
SystemNetworkContextManager::GetInstance()->GetSharedURLLoaderFactory();
base::Version version = version_info::GetVersion();
std::string channel_name = chrome::GetChannelName();
std::string client_version =
base::StringPrintf("%d.%d.%d.%s.chrome",
version.components()[0], // Major
version.components()[2], // Build
version.components()[3], // Patch
channel_name.c_str());
return CreateVideoTutorialService(
db_provider, storage_dir, accept_languanges, GetCountryCode(),
GetGoogleAPIKey(), client_version, url_loader_factory,
ProfileKey::FromSimpleFactoryKey(key)->GetPrefs());
} }
} // namespace video_tutorials } // namespace video_tutorials
...@@ -170,6 +170,8 @@ const base::FilePath::CharType kPreferencesFilename[] = FPL("Preferences"); ...@@ -170,6 +170,8 @@ const base::FilePath::CharType kPreferencesFilename[] = FPL("Preferences");
const base::FilePath::CharType kPreviewsOptOutDBFilename[] = const base::FilePath::CharType kPreviewsOptOutDBFilename[] =
FPL("previews_opt_out.db"); FPL("previews_opt_out.db");
const base::FilePath::CharType kQueryTileStorageDirname[] = FPL("Query Tiles"); const base::FilePath::CharType kQueryTileStorageDirname[] = FPL("Query Tiles");
const base::FilePath::CharType kVideoTutorialsStorageDirname[] =
FPL("Video Tutorials");
const base::FilePath::CharType kReadmeFilename[] = FPL("README"); const base::FilePath::CharType kReadmeFilename[] = FPL("README");
const base::FilePath::CharType kSecurePreferencesFilename[] = const base::FilePath::CharType kSecurePreferencesFilename[] =
FPL("Secure Preferences"); FPL("Secure Preferences");
......
...@@ -65,6 +65,7 @@ extern const base::FilePath::CharType kOfflinePageRequestQueueDirname[]; ...@@ -65,6 +65,7 @@ extern const base::FilePath::CharType kOfflinePageRequestQueueDirname[];
extern const base::FilePath::CharType kPreferencesFilename[]; extern const base::FilePath::CharType kPreferencesFilename[];
extern const base::FilePath::CharType kPreviewsOptOutDBFilename[]; extern const base::FilePath::CharType kPreviewsOptOutDBFilename[];
extern const base::FilePath::CharType kQueryTileStorageDirname[]; extern const base::FilePath::CharType kQueryTileStorageDirname[];
extern const base::FilePath::CharType kVideoTutorialsStorageDirname[];
extern const base::FilePath::CharType kReadmeFilename[]; extern const base::FilePath::CharType kReadmeFilename[];
extern const base::FilePath::CharType kSecurePreferencesFilename[]; extern const base::FilePath::CharType kSecurePreferencesFilename[];
extern const base::FilePath::CharType kServiceStateFileName[]; extern const base::FilePath::CharType kServiceStateFileName[];
......
...@@ -86,6 +86,8 @@ std::string SharedProtoDatabaseClientList::ProtoDbTypeToString( ...@@ -86,6 +86,8 @@ std::string SharedProtoDatabaseClientList::ProtoDbTypeToString(
return "UpboardingQueryTileStore"; return "UpboardingQueryTileStore";
case ProtoDbType::NEARBY_SHARE_PUBLIC_CERTIFICATE_DATABASE: case ProtoDbType::NEARBY_SHARE_PUBLIC_CERTIFICATE_DATABASE:
return "NearbySharePublicCertificateDatabase"; return "NearbySharePublicCertificateDatabase";
case ProtoDbType::VIDEO_TUTORIALS_DATABASE:
return "VideoTutorialsDatabase";
case ProtoDbType::LAST: case ProtoDbType::LAST:
NOTREACHED(); NOTREACHED();
return std::string(); return std::string();
......
...@@ -52,6 +52,7 @@ enum class ProtoDbType { ...@@ -52,6 +52,7 @@ enum class ProtoDbType {
TAB_STATE_DATABASE = 27, TAB_STATE_DATABASE = 27,
UPBOARDING_QUERY_TILE_STORE = 28, UPBOARDING_QUERY_TILE_STORE = 28,
NEARBY_SHARE_PUBLIC_CERTIFICATE_DATABASE = 29, NEARBY_SHARE_PUBLIC_CERTIFICATE_DATABASE = 29,
VIDEO_TUTORIALS_DATABASE = 30,
LAST, LAST,
}; };
...@@ -66,6 +67,7 @@ constexpr ProtoDbType kWhitelistedDbForSharedImpl[]{ ...@@ -66,6 +67,7 @@ constexpr ProtoDbType kWhitelistedDbForSharedImpl[]{
ProtoDbType::TAB_STATE_DATABASE, ProtoDbType::TAB_STATE_DATABASE,
ProtoDbType::UPBOARDING_QUERY_TILE_STORE, ProtoDbType::UPBOARDING_QUERY_TILE_STORE,
ProtoDbType::NEARBY_SHARE_PUBLIC_CERTIFICATE_DATABASE, ProtoDbType::NEARBY_SHARE_PUBLIC_CERTIFICATE_DATABASE,
ProtoDbType::VIDEO_TUTORIALS_DATABASE,
ProtoDbType::LAST, // Marks the end of list. ProtoDbType::LAST, // Marks the end of list.
}; };
......
...@@ -8207,6 +8207,7 @@ reviews. Googlers can read more about this at go/gwsq-gerrit. ...@@ -8207,6 +8207,7 @@ reviews. Googlers can read more about this at go/gwsq-gerrit.
<suffix name="UsageStatsWebsiteEvent" <suffix name="UsageStatsWebsiteEvent"
label="UsageStats database for WebsiteEvents."/> label="UsageStats database for WebsiteEvents."/>
<suffix name="VideoDecodeStatsDB" label="Database for video decode stats"/> <suffix name="VideoDecodeStatsDB" label="Database for video decode stats"/>
<suffix name="VideoTutorialsDatabase" label="Database for video tutorials."/>
<affected-histogram name="LevelDB.ApproximateMemoryUse"/> <affected-histogram name="LevelDB.ApproximateMemoryUse"/>
<affected-histogram name="LevelDB.ApproximateMemTableMemoryUse"/> <affected-histogram name="LevelDB.ApproximateMemTableMemoryUse"/>
<affected-histogram name="LevelDB.Open"/> <affected-histogram name="LevelDB.Open"/>
......
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