Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / app_mode / fake_cws.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/app_mode/fake_cws.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/files/file_util.h"
10 #include "base/path_service.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "crypto/sha2.h"
17 #include "net/test/embedded_test_server/embedded_test_server.h"
18
19 using net::test_server::BasicHttpResponse;
20 using net::test_server::EmbeddedTestServer;
21 using net::test_server::HttpRequest;
22 using net::test_server::HttpResponse;
23
24 namespace chromeos {
25
26 namespace {
27
28 const char kWebstoreDomain[] = "cws.com";
29 // Kiosk app crx file download path under web store site.
30 const char kCrxDownloadPath[] = "/chromeos/app_mode/webstore/downloads/";
31
32 }  // namespace
33
34 FakeCWS::FakeCWS() : update_check_count_(0) {
35 }
36
37 FakeCWS::~FakeCWS() {
38 }
39
40 void FakeCWS::Init(EmbeddedTestServer* embedded_test_server) {
41   has_update_template_ =
42       "chromeos/app_mode/webstore/update_check/has_update.xml";
43   no_update_template_ = "chromeos/app_mode/webstore/update_check/no_update.xml";
44   update_check_end_point_ = "/update_check.xml";
45
46   SetupWebStoreURL(embedded_test_server->base_url());
47   OverrideGalleryCommandlineSwitches();
48   embedded_test_server->RegisterRequestHandler(
49       base::Bind(&FakeCWS::HandleRequest, base::Unretained(this)));
50 }
51
52 void FakeCWS::InitAsPrivateStore(EmbeddedTestServer* embedded_test_server,
53                                  const std::string& update_check_end_point) {
54   has_update_template_ =
55       "chromeos/app_mode/webstore/update_check/has_update_private_store.xml";
56   no_update_template_ = "chromeos/app_mode/webstore/update_check/no_update.xml";
57   update_check_end_point_ = update_check_end_point;
58
59   SetupWebStoreURL(embedded_test_server->base_url());
60   embedded_test_server->RegisterRequestHandler(
61       base::Bind(&FakeCWS::HandleRequest, base::Unretained(this)));
62 }
63
64 void FakeCWS::SetUpdateCrx(const std::string& app_id,
65                            const std::string& crx_file,
66                            const std::string& version) {
67   GURL crx_download_url = web_store_url_.Resolve(kCrxDownloadPath + crx_file);
68
69   base::FilePath test_data_dir;
70   PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
71   base::FilePath crx_file_path =
72       test_data_dir.AppendASCII("chromeos/app_mode/webstore/downloads")
73           .AppendASCII(crx_file);
74   std::string crx_content;
75   ASSERT_TRUE(base::ReadFileToString(crx_file_path, &crx_content));
76
77   const std::string sha256 = crypto::SHA256HashString(crx_content);
78   const std::string sha256_hex = base::HexEncode(sha256.c_str(), sha256.size());
79
80   SetUpdateCheckContent(
81       has_update_template_,
82       crx_download_url,
83       app_id,
84       sha256_hex,
85       base::UintToString(crx_content.size()),
86       version,
87       &update_check_content_);
88 }
89
90 void FakeCWS::SetNoUpdate(const std::string& app_id) {
91   SetUpdateCheckContent(no_update_template_,
92                         GURL(),
93                         app_id,
94                         "",
95                         "",
96                         "",
97                         &update_check_content_);
98 }
99
100 int FakeCWS::GetUpdateCheckCountAndReset() {
101   int current_count = update_check_count_;
102   update_check_count_ = 0;
103   return current_count;
104 }
105
106 void FakeCWS::SetupWebStoreURL(const GURL& test_server_url) {
107   std::string webstore_host(kWebstoreDomain);
108   GURL::Replacements replace_webstore_host;
109   replace_webstore_host.SetHostStr(webstore_host);
110   web_store_url_ = test_server_url.ReplaceComponents(replace_webstore_host);
111 }
112
113 void FakeCWS::OverrideGalleryCommandlineSwitches() {
114   DCHECK(web_store_url_.is_valid());
115
116   CommandLine* command_line = CommandLine::ForCurrentProcess();
117
118   command_line->AppendSwitchASCII(
119       ::switches::kAppsGalleryURL,
120       web_store_url_.Resolve("/chromeos/app_mode/webstore").spec());
121
122   std::string downloads_path = std::string(kCrxDownloadPath).append("%s.crx");
123   GURL downloads_url = web_store_url_.Resolve(downloads_path);
124   command_line->AppendSwitchASCII(::switches::kAppsGalleryDownloadURL,
125                                   downloads_url.spec());
126
127   GURL update_url = web_store_url_.Resolve(update_check_end_point_);
128   command_line->AppendSwitchASCII(::switches::kAppsGalleryUpdateURL,
129                                   update_url.spec());
130 }
131
132 void FakeCWS::SetUpdateCheckContent(const std::string& update_check_file,
133                                     const GURL& crx_download_url,
134                                     const std::string& app_id,
135                                     const std::string& crx_fp,
136                                     const std::string& crx_size,
137                                     const std::string& version,
138                                     std::string* update_check_content) {
139   base::FilePath test_data_dir;
140   PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
141   base::FilePath update_file =
142       test_data_dir.AppendASCII(update_check_file.c_str());
143   ASSERT_TRUE(base::ReadFileToString(update_file, update_check_content));
144
145   ReplaceSubstringsAfterOffset(update_check_content, 0, "$AppId", app_id);
146   ReplaceSubstringsAfterOffset(
147       update_check_content, 0, "$CrxDownloadUrl", crx_download_url.spec());
148   ReplaceSubstringsAfterOffset(update_check_content, 0, "$FP", crx_fp);
149   ReplaceSubstringsAfterOffset(update_check_content, 0, "$Size", crx_size);
150   ReplaceSubstringsAfterOffset(update_check_content, 0, "$Version", version);
151 }
152
153 scoped_ptr<HttpResponse> FakeCWS::HandleRequest(const HttpRequest& request) {
154   GURL request_url = GURL("http://localhost").Resolve(request.relative_url);
155   std::string request_path = request_url.path();
156   if (!update_check_content_.empty() &&
157       request_path.find(update_check_end_point_) != std::string::npos) {
158     ++update_check_count_;
159     scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse());
160     http_response->set_code(net::HTTP_OK);
161     http_response->set_content_type("text/xml");
162     http_response->set_content(update_check_content_);
163     return http_response.Pass();
164   }
165
166   return scoped_ptr<HttpResponse>();
167 }
168
169 }  // namespace chromeos