| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <map> | 5 #include <map> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 public: | 98 public: |
| 99 ~FakeUpdateURLFetcherFactory() override {} | 99 ~FakeUpdateURLFetcherFactory() override {} |
| 100 | 100 |
| 101 void RegisterFakeExtension(const std::string& id, | 101 void RegisterFakeExtension(const std::string& id, |
| 102 const std::string& contents) { | 102 const std::string& contents) { |
| 103 CHECK_EQ(32u, id.size()); | 103 CHECK_EQ(32u, id.size()); |
| 104 fake_extensions_.insert(std::make_pair(id, contents)); | 104 fake_extensions_.insert(std::make_pair(id, contents)); |
| 105 } | 105 } |
| 106 | 106 |
| 107 // net::URLFetcherFactory: | 107 // net::URLFetcherFactory: |
| 108 net::URLFetcher* CreateURLFetcher( | 108 scoped_ptr<net::URLFetcher> CreateURLFetcher( |
| 109 int id, | 109 int id, |
| 110 const GURL& url, | 110 const GURL& url, |
| 111 net::URLFetcher::RequestType request_type, | 111 net::URLFetcher::RequestType request_type, |
| 112 net::URLFetcherDelegate* delegate) override { | 112 net::URLFetcherDelegate* delegate) override { |
| 113 if (url.spec().find(extension_urls::GetWebstoreUpdateUrl().spec()) == 0) { | 113 if (url.spec().find(extension_urls::GetWebstoreUpdateUrl().spec()) == 0) { |
| 114 // Handle fake Omaha requests. | 114 // Handle fake Omaha requests. |
| 115 return CreateUpdateManifestFetcher(url, delegate); | 115 return CreateUpdateManifestFetcher(url, delegate); |
| 116 } else if (url.spec().find("https://fake-omaha-hostname") == 0) { | 116 } else if (url.spec().find("https://fake-omaha-hostname") == 0) { |
| 117 // Handle a fake CRX request. | 117 // Handle a fake CRX request. |
| 118 return CreateCrxFetcher(url, delegate); | 118 return CreateCrxFetcher(url, delegate); |
| 119 } | 119 } |
| 120 NOTREACHED(); | 120 NOTREACHED(); |
| 121 return nullptr; | 121 return nullptr; |
| 122 } | 122 } |
| 123 | 123 |
| 124 private: | 124 private: |
| 125 net::URLFetcher* CreateUpdateManifestFetcher( | 125 scoped_ptr<net::URLFetcher> CreateUpdateManifestFetcher( |
| 126 const GURL& url, | 126 const GURL& url, |
| 127 net::URLFetcherDelegate* delegate) { | 127 net::URLFetcherDelegate* delegate) { |
| 128 // If we have a fake CRX for the ID, return a fake update blob for it. | 128 // If we have a fake CRX for the ID, return a fake update blob for it. |
| 129 // Otherwise return an invalid-ID response. | 129 // Otherwise return an invalid-ID response. |
| 130 FakeResponse response; | 130 FakeResponse response; |
| 131 std::string extension_id; | 131 std::string extension_id; |
| 132 if (!ExtractIdFromUpdateQuery(url.query(), &extension_id)) { | 132 if (!ExtractIdFromUpdateQuery(url.query(), &extension_id)) { |
| 133 response = CreateFakeUpdateNotFoundResponse(); | 133 response = CreateFakeUpdateNotFoundResponse(); |
| 134 } else { | 134 } else { |
| 135 const auto& iter = fake_extensions_.find(extension_id); | 135 const auto& iter = fake_extensions_.find(extension_id); |
| 136 if (iter == fake_extensions_.end()) | 136 if (iter == fake_extensions_.end()) |
| 137 response = CreateFakeUpdateNotFoundResponse(); | 137 response = CreateFakeUpdateNotFoundResponse(); |
| 138 else | 138 else |
| 139 response = CreateFakeUpdateResponse(extension_id, iter->second.size()); | 139 response = CreateFakeUpdateResponse(extension_id, iter->second.size()); |
| 140 } | 140 } |
| 141 return new net::FakeURLFetcher(url, delegate, response.first, | 141 return scoped_ptr<net::URLFetcher>( |
| 142 response.second, | 142 new net::FakeURLFetcher(url, delegate, response.first, response.second, |
| 143 net::URLRequestStatus::SUCCESS); | 143 net::URLRequestStatus::SUCCESS)); |
| 144 } | 144 } |
| 145 | 145 |
| 146 net::URLFetcher* CreateCrxFetcher(const GURL& url, | 146 scoped_ptr<net::URLFetcher> CreateCrxFetcher( |
| 147 net::URLFetcherDelegate* delegate) { | 147 const GURL& url, |
| 148 net::URLFetcherDelegate* delegate) { |
| 148 FakeResponse response; | 149 FakeResponse response; |
| 149 std::string extension_id = url.path().substr(1, 32); | 150 std::string extension_id = url.path().substr(1, 32); |
| 150 const auto& iter = fake_extensions_.find(extension_id); | 151 const auto& iter = fake_extensions_.find(extension_id); |
| 151 if (iter == fake_extensions_.end()) | 152 if (iter == fake_extensions_.end()) |
| 152 response = std::make_pair(std::string(), net::HTTP_NOT_FOUND); | 153 response = std::make_pair(std::string(), net::HTTP_NOT_FOUND); |
| 153 else | 154 else |
| 154 response = std::make_pair(iter->second, net::HTTP_OK); | 155 response = std::make_pair(iter->second, net::HTTP_OK); |
| 155 net::TestURLFetcher* fetcher = | 156 net::TestURLFetcher* fetcher = |
| 156 new net::FakeURLFetcher(url, delegate, response.first, response.second, | 157 new net::FakeURLFetcher(url, delegate, response.first, response.second, |
| 157 net::URLRequestStatus::SUCCESS); | 158 net::URLRequestStatus::SUCCESS); |
| 158 fetcher->SetResponseFilePath(base::FilePath::FromUTF8Unsafe(url.path())); | 159 fetcher->SetResponseFilePath(base::FilePath::FromUTF8Unsafe(url.path())); |
| 159 return fetcher; | 160 return scoped_ptr<net::URLFetcher>(fetcher); |
| 160 } | 161 } |
| 161 | 162 |
| 162 std::map<std::string, std::string> fake_extensions_; | 163 std::map<std::string, std::string> fake_extensions_; |
| 163 }; | 164 }; |
| 164 | 165 |
| 165 } // namespace | 166 } // namespace |
| 166 | 167 |
| 167 class UpdateServiceTest : public AppShellTest { | 168 class UpdateServiceTest : public AppShellTest { |
| 168 public: | 169 public: |
| 169 UpdateServiceTest() {} | 170 UpdateServiceTest() {} |
| (...skipping 29 matching lines...) Expand all Loading... |
| 199 const char kCrxContents[] = "Hello, world!"; | 200 const char kCrxContents[] = "Hello, world!"; |
| 200 RegisterFakeExtension(kCrxId, kCrxContents); | 201 RegisterFakeExtension(kCrxId, kCrxContents); |
| 201 | 202 |
| 202 base::RunLoop run_loop; | 203 base::RunLoop run_loop; |
| 203 update_service()->DownloadAndInstall( | 204 update_service()->DownloadAndInstall( |
| 204 kCrxId, base::Bind(ExpectDownloadSuccess, run_loop.QuitClosure())); | 205 kCrxId, base::Bind(ExpectDownloadSuccess, run_loop.QuitClosure())); |
| 205 run_loop.Run(); | 206 run_loop.Run(); |
| 206 } | 207 } |
| 207 | 208 |
| 208 } // namespace extensions | 209 } // namespace extensions |
| OLD | NEW |