Index: chrome/browser/chromeos/app_mode/fake_cws.cc |
diff --git a/chrome/browser/chromeos/app_mode/fake_cws.cc b/chrome/browser/chromeos/app_mode/fake_cws.cc |
index 401e3f2b7c610d767544ac1799abe77f95f2100e..1406145eaa55711ef0cba26c3e8ef0813620476f 100644 |
--- a/chrome/browser/chromeos/app_mode/fake_cws.cc |
+++ b/chrome/browser/chromeos/app_mode/fake_cws.cc |
@@ -14,6 +14,7 @@ |
#include "chrome/common/chrome_switches.h" |
#include "chrome/test/base/in_process_browser_test.h" |
#include "crypto/sha2.h" |
+#include "net/base/url_util.h" |
#include "net/test/embedded_test_server/embedded_test_server.h" |
using net::test_server::BasicHttpResponse; |
@@ -29,6 +30,40 @@ const char kWebstoreDomain[] = "cws.com"; |
// Kiosk app crx file download path under web store site. |
const char kCrxDownloadPath[] = "/chromeos/app_mode/webstore/downloads/"; |
+const char kAppNoUpdateTemplate[] = |
+ "<app appid=\"$AppId\" status=\"ok\">" |
+ "<updatecheck status=\"noupdate\"/></app>"; |
xiyuan
2015/08/26 18:16:16
nit: indent 2 more spaces and split </app> to the
jennyz
2015/08/28 18:24:08
Done.
|
+ |
+const char kAppHasUpdateTemplate[] = |
+ "<app appid=\"$AppId\" status=\"ok\">" |
+ "<updatecheck codebase=\"$CrxDownloadUrl\" fp=\"1.$FP\" " |
+ "hash=\"\" hash_sha256=\"$FP\" size=\"$Size\" status=\"ok\" " |
+ "version=\"$Version\"/></app>"; |
+ |
+const char kPrivateStoreAppHasUpdateTemplate[] = |
+ "<app appid=\"$AppId\">" |
+ "<updatecheck codebase=\"$CrxDownloadUrl\" version=\"$Version\"/></app>"; |
+ |
+const char kUpdateContentTemplate[] = |
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
+ "<gupdate xmlns=\"http://www.google.com/update2/response\" " |
+ "protocol=\"2.0\" server=\"prod\">" |
+ "<daystart elapsed_days=\"2569\" " |
+ "elapsed_seconds=\"36478\"/>$APPS</gupdate>"; |
+ |
+bool GetAppIdsFromUpdateUrl(const GURL& update_url, |
+ std::vector<std::string>* ids) { |
+ for (net::QueryIterator it(update_url); !it.IsAtEnd(); it.Advance()) { |
+ if (it.GetKey() != "x") |
+ continue; |
+ std::string id; |
+ net::GetValueForKeyInQuery(GURL("http://dummy?" + it.GetUnescapedValue()), |
+ "id", &id); |
+ ids->push_back(id); |
+ } |
+ return !ids->empty(); |
+} |
+ |
} // namespace |
FakeCWS::FakeCWS() : update_check_count_(0) { |
not at google - send to devlin
2015/08/26 17:53:58
Is somebody reviewing this file for correctness?
jennyz
2015/08/28 18:24:08
xiyuan is reviewing this one.
|
@@ -38,9 +73,8 @@ FakeCWS::~FakeCWS() { |
} |
void FakeCWS::Init(EmbeddedTestServer* embedded_test_server) { |
- has_update_template_ = |
- "chromeos/app_mode/webstore/update_check/has_update.xml"; |
- no_update_template_ = "chromeos/app_mode/webstore/update_check/no_update.xml"; |
xiyuan
2015/08/26 18:16:16
nit: remove the two files since we have moved the
|
+ has_update_template_ = std::string(kAppHasUpdateTemplate); |
not at google - send to devlin
2015/08/26 17:53:58
std::string not needed, it will implicitly convert
jennyz
2015/08/28 18:24:08
Done.
|
+ no_update_template_ = std::string(kAppNoUpdateTemplate); |
update_check_end_point_ = "/update_check.xml"; |
SetupWebStoreURL(embedded_test_server->base_url()); |
@@ -51,9 +85,8 @@ void FakeCWS::Init(EmbeddedTestServer* embedded_test_server) { |
void FakeCWS::InitAsPrivateStore(EmbeddedTestServer* embedded_test_server, |
const std::string& update_check_end_point) { |
- has_update_template_ = |
- "chromeos/app_mode/webstore/update_check/has_update_private_store.xml"; |
xiyuan
2015/08/26 18:16:16
nit: remove this file too.
|
- no_update_template_ = "chromeos/app_mode/webstore/update_check/no_update.xml"; |
+ has_update_template_ = std::string(kPrivateStoreAppHasUpdateTemplate); |
+ no_update_template_ = std::string(kAppNoUpdateTemplate); |
update_check_end_point_ = update_check_end_point; |
SetupWebStoreURL(embedded_test_server->base_url()); |
@@ -77,24 +110,25 @@ void FakeCWS::SetUpdateCrx(const std::string& app_id, |
const std::string sha256 = crypto::SHA256HashString(crx_content); |
const std::string sha256_hex = base::HexEncode(sha256.c_str(), sha256.size()); |
- SetUpdateCheckContent( |
- has_update_template_, |
- crx_download_url, |
- app_id, |
- sha256_hex, |
- base::UintToString(crx_content.size()), |
- version, |
- &update_check_content_); |
+ std::string update_check_content(has_update_template_); |
+ base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$AppId", |
+ app_id); |
+ base::ReplaceSubstringsAfterOffset( |
+ &update_check_content, 0, "$CrxDownloadUrl", crx_download_url.spec()); |
+ base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$FP", |
+ sha256_hex); |
+ base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$Size", |
+ base::UintToString(crx_content.size())); |
+ base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$Version", |
+ version); |
+ id_to_update_check_content_map_[app_id] = update_check_content; |
} |
void FakeCWS::SetNoUpdate(const std::string& app_id) { |
- SetUpdateCheckContent(no_update_template_, |
- GURL(), |
- app_id, |
- "", |
- "", |
- "", |
- &update_check_content_); |
+ std::string app_update_check_content(no_update_template_); |
+ base::ReplaceSubstringsAfterOffset(&app_update_check_content, 0, "$AppId", |
+ app_id); |
+ id_to_update_check_content_map_[app_id] = app_update_check_content; |
} |
int FakeCWS::GetUpdateCheckCountAndReset() { |
@@ -128,40 +162,42 @@ void FakeCWS::OverrideGalleryCommandlineSwitches() { |
update_url.spec()); |
} |
-void FakeCWS::SetUpdateCheckContent(const std::string& update_check_file, |
- const GURL& crx_download_url, |
- const std::string& app_id, |
- const std::string& crx_fp, |
- const std::string& crx_size, |
- const std::string& version, |
+bool FakeCWS::GetUpdateCheckContent(const std::vector<std::string>& ids, |
std::string* update_check_content) { |
- base::FilePath test_data_dir; |
- PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); |
- base::FilePath update_file = |
- test_data_dir.AppendASCII(update_check_file.c_str()); |
- ASSERT_TRUE(base::ReadFileToString(update_file, update_check_content)); |
+ std::string apps_content; |
+ for (size_t i = 0; i < ids.size(); ++i) { |
+ std::string app_update_content; |
+ if (id_to_update_check_content_map_.find(ids[i]) == |
+ id_to_update_check_content_map_.end()) |
+ return false; |
+ apps_content.append(id_to_update_check_content_map_[ids[i]]); |
xiyuan
2015/08/26 18:16:16
nit: use iterate to save a bit
auto it = id_to_up
jennyz
2015/08/28 18:24:08
Done.
|
+ } |
+ if (apps_content.empty()) |
+ return false; |
- base::ReplaceSubstringsAfterOffset(update_check_content, 0, "$AppId", app_id); |
- base::ReplaceSubstringsAfterOffset( |
- update_check_content, 0, "$CrxDownloadUrl", crx_download_url.spec()); |
- base::ReplaceSubstringsAfterOffset(update_check_content, 0, "$FP", crx_fp); |
- base::ReplaceSubstringsAfterOffset(update_check_content, 0, |
- "$Size", crx_size); |
- base::ReplaceSubstringsAfterOffset(update_check_content, 0, |
- "$Version", version); |
+ *update_check_content = std::string(kUpdateContentTemplate); |
+ base::ReplaceSubstringsAfterOffset(update_check_content, 0, "$APPS", |
+ apps_content); |
+ return true; |
} |
scoped_ptr<HttpResponse> FakeCWS::HandleRequest(const HttpRequest& request) { |
GURL request_url = GURL("http://localhost").Resolve(request.relative_url); |
std::string request_path = request_url.path(); |
- if (!update_check_content_.empty() && |
- request_path.find(update_check_end_point_) != std::string::npos) { |
- ++update_check_count_; |
- scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); |
- http_response->set_code(net::HTTP_OK); |
- http_response->set_content_type("text/xml"); |
- http_response->set_content(update_check_content_); |
- return http_response.Pass(); |
+ if (request_path.find(update_check_end_point_) != std::string::npos && |
+ !id_to_update_check_content_map_.empty()) { |
+ std::vector<std::string> ids; |
+ if (GetAppIdsFromUpdateUrl(request_url, &ids)) { |
+ std::string update_check_content; |
+ if (GetUpdateCheckContent(ids, &update_check_content)) { |
+ ++update_check_count_; |
+ scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); |
+ http_response->set_code(net::HTTP_OK); |
+ http_response->set_content_type("text/xml"); |
+ http_response->set_content(update_check_content); |
+ return http_response.Pass(); |
+ } |
+ } |
} |
return scoped_ptr<HttpResponse>(); |