Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef DataConsumerHandleTestUtil_h | 5 #ifndef DataConsumerHandleTestUtil_h |
| 6 #define DataConsumerHandleTestUtil_h | 6 #define DataConsumerHandleTestUtil_h |
| 7 | 7 |
| 8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
| 9 #include "core/testing/NullExecutionContext.h" | 9 #include "core/testing/NullExecutionContext.h" |
| 10 #include "gin/public/isolate_holder.h" | 10 #include "gin/public/isolate_holder.h" |
| 11 #include "modules/fetch/DataConsumerHandleUtil.h" | 11 #include "modules/fetch/DataConsumerHandleUtil.h" |
| 12 #include "modules/fetch/FetchDataConsumerHandle.h" | 12 #include "modules/fetch/FetchDataConsumerHandle.h" |
| 13 #include "modules/fetch/FetchDataLoader.h" | |
| 13 #include "platform/Task.h" | 14 #include "platform/Task.h" |
| 14 #include "platform/ThreadSafeFunctional.h" | 15 #include "platform/ThreadSafeFunctional.h" |
| 15 #include "platform/WebThreadSupportingGC.h" | 16 #include "platform/WebThreadSupportingGC.h" |
| 16 #include "platform/heap/Handle.h" | 17 #include "platform/heap/Handle.h" |
| 17 #include "public/platform/Platform.h" | 18 #include "public/platform/Platform.h" |
| 18 #include "public/platform/WebDataConsumerHandle.h" | 19 #include "public/platform/WebDataConsumerHandle.h" |
| 19 #include "public/platform/WebTraceLocation.h" | 20 #include "public/platform/WebTraceLocation.h" |
| 20 #include "public/platform/WebWaitableEvent.h" | 21 #include "public/platform/WebWaitableEvent.h" |
| 21 #include "wtf/Locker.h" | 22 #include "wtf/Locker.h" |
|
yhirano
2015/06/25 04:31:03
+Deque
+Vector
+ThreadingPrimitives
+ThreadSafeRef
hiroshige
2015/06/25 05:33:57
Done.
| |
| 22 | 23 |
| 23 #include <gmock/gmock.h> | 24 #include <gmock/gmock.h> |
| 24 #include <gtest/gtest.h> | 25 #include <gtest/gtest.h> |
| 25 #include <v8.h> | 26 #include <v8.h> |
| 26 | 27 |
| 27 namespace blink { | 28 namespace blink { |
| 28 | 29 |
| 29 class DataConsumerHandleTestUtil { | 30 class DataConsumerHandleTestUtil { |
| 30 public: | 31 public: |
| 31 class NoopClient final : public WebDataConsumerHandle::Client { | 32 class NoopClient final : public WebDataConsumerHandle::Client { |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 m_reader = nullptr; | 205 m_reader = nullptr; |
| 205 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self:: signalDone, this))); | 206 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self:: signalDone, this))); |
| 206 } | 207 } |
| 207 void didGetReadable() override | 208 void didGetReadable() override |
| 208 { | 209 { |
| 209 ASSERT_NOT_REACHED(); | 210 ASSERT_NOT_REACHED(); |
| 210 } | 211 } |
| 211 | 212 |
| 212 OwnPtr<WebDataConsumerHandle> m_handle; | 213 OwnPtr<WebDataConsumerHandle> m_handle; |
| 213 }; | 214 }; |
| 215 | |
| 216 class MockFetchDataLoaderClient : public GarbageCollectedFinalized<MockFetch DataLoaderClient>, public FetchDataLoader::Client { | |
| 217 USING_GARBAGE_COLLECTED_MIXIN(MockFetchDataLoaderClient); | |
| 218 public: | |
| 219 static ::testing::StrictMock<MockFetchDataLoaderClient>* create() { retu rn new ::testing::StrictMock<MockFetchDataLoaderClient>; } | |
| 220 | |
| 221 DEFINE_INLINE_VIRTUAL_TRACE() | |
| 222 { | |
| 223 FetchDataLoader::Client::trace(visitor); | |
| 224 } | |
| 225 | |
| 226 MOCK_METHOD1(didFetchDataLoadedBlobHandleMock, void(RefPtr<BlobDataHandl e>)); | |
| 227 MOCK_METHOD1(didFetchDataLoadedArrayBufferMock, void(RefPtr<DOMArrayBuff er>)); | |
| 228 MOCK_METHOD1(didFetchDataLoadedString, void(const String&)); | |
| 229 MOCK_METHOD0(didFetchDataLoadFailed, void()); | |
| 230 | |
| 231 // In mock methods we use RefPtr<> rather than PassRefPtr<>. | |
| 232 void didFetchDataLoadedArrayBuffer(PassRefPtr<DOMArrayBuffer> arrayBuffe r) override | |
| 233 { | |
| 234 didFetchDataLoadedArrayBufferMock(arrayBuffer); | |
| 235 } | |
| 236 void didFetchDataLoadedBlobHandle(PassRefPtr<BlobDataHandle> blobDataHan dle) override | |
| 237 { | |
| 238 didFetchDataLoadedBlobHandleMock(blobDataHandle); | |
| 239 } | |
| 240 }; | |
| 241 | |
| 242 class Command final { | |
| 243 public: | |
| 244 enum Name { | |
| 245 Data, | |
| 246 Done, | |
| 247 Error, | |
| 248 Wait, | |
| 249 }; | |
| 250 | |
| 251 Command(Name name) : m_name(name) { } | |
| 252 Command(Name name, const Vector<char>& body) : m_name(name), m_body(body ) { } | |
| 253 Command(Name name, const char* body, size_t size) : m_name(name) | |
| 254 { | |
| 255 m_body.append(body, size); | |
| 256 } | |
| 257 Command(Name name, const char* body) : Command(name, body, strlen(body)) { } | |
| 258 Name name() const { return m_name; } | |
| 259 const Vector<char>& body() const { return m_body; } | |
| 260 | |
| 261 private: | |
| 262 const Name m_name; | |
| 263 Vector<char> m_body; | |
| 264 }; | |
| 265 | |
| 266 // ReplayingHandle stores commands via |add| and replays the stored commends when read. | |
| 267 class ReplayingHandle final : public WebDataConsumerHandle { | |
| 268 public: | |
| 269 static PassOwnPtr<ReplayingHandle> create() { return adoptPtr(new Replay ingHandle()); } | |
| 270 ~ReplayingHandle(); | |
| 271 | |
| 272 // Add a command to this handle. This function must be called on the | |
| 273 // creator thread. This function must be called BEFORE any reader is | |
| 274 // obtained. | |
| 275 void add(const Command&); | |
| 276 | |
| 277 class Context final : public ThreadSafeRefCounted<Context> { | |
| 278 public: | |
| 279 static PassRefPtr<Context> create() { return adoptRef(new Context); } | |
| 280 | |
| 281 // This function cannot be called after creating a tee. | |
| 282 void add(const Command& command) | |
|
yhirano
2015/06/25 04:31:03
Please move function definitions to the cpp file.
hiroshige
2015/06/25 05:33:57
Done.
| |
| 283 { | |
| 284 MutexLocker locker(m_mutex); | |
| 285 m_commands.append(command); | |
| 286 } | |
| 287 | |
| 288 void attachReader(WebDataConsumerHandle::Client* client) | |
| 289 { | |
| 290 MutexLocker locker(m_mutex); | |
| 291 ASSERT(!m_readerThread); | |
| 292 ASSERT(!m_client); | |
| 293 m_readerThread = Platform::current()->currentThread(); | |
| 294 m_client = client; | |
| 295 | |
| 296 if (m_client && !(isEmpty() && m_result == ShouldWait)) | |
| 297 notify(); | |
| 298 } | |
| 299 void detachReader() | |
| 300 { | |
| 301 MutexLocker locker(m_mutex); | |
| 302 ASSERT(m_readerThread && m_readerThread->isCurrentThread()); | |
| 303 m_readerThread = nullptr; | |
| 304 m_client = nullptr; | |
| 305 if (!m_isHandleAttached) | |
| 306 m_detached->signal(); | |
| 307 } | |
| 308 | |
| 309 void detachHandle() | |
| 310 { | |
| 311 MutexLocker locker(m_mutex); | |
| 312 m_isHandleAttached = false; | |
| 313 if (!m_readerThread) | |
| 314 m_detached->signal(); | |
| 315 } | |
| 316 | |
| 317 Result beginRead(const void** buffer, Flags, size_t* available) | |
| 318 { | |
| 319 MutexLocker locker(m_mutex); | |
| 320 *buffer = nullptr; | |
| 321 *available = 0; | |
| 322 if (isEmpty()) | |
| 323 return m_result; | |
| 324 | |
| 325 const Command& command = top(); | |
| 326 Result result = Ok; | |
| 327 switch (command.name()) { | |
| 328 case Command::Data: { | |
| 329 auto& body = command.body(); | |
| 330 *available = body.size() - offset(); | |
| 331 *buffer = body.data() + offset(); | |
| 332 result = Ok; | |
| 333 break; | |
| 334 } | |
| 335 case Command::Done: | |
| 336 m_result = result = Done; | |
| 337 consume(0); | |
| 338 break; | |
| 339 case Command::Wait: | |
| 340 consume(0); | |
| 341 result = ShouldWait; | |
| 342 notify(); | |
| 343 break; | |
| 344 case Command::Error: | |
| 345 m_result = result = UnexpectedError; | |
| 346 consume(0); | |
| 347 break; | |
| 348 } | |
| 349 return result; | |
| 350 } | |
| 351 Result endRead(size_t readSize) | |
| 352 { | |
| 353 MutexLocker locker(m_mutex); | |
| 354 consume(readSize); | |
| 355 return Ok; | |
| 356 } | |
| 357 | |
| 358 WebWaitableEvent* detached() { return m_detached.get(); } | |
| 359 | |
| 360 private: | |
| 361 Context() | |
| 362 : m_offset(0) | |
| 363 , m_readerThread(nullptr) | |
| 364 , m_client(nullptr) | |
| 365 , m_result(ShouldWait) | |
| 366 , m_isHandleAttached(true) | |
| 367 , m_detached(adoptPtr(Platform::current()->createWaitableEvent() )) | |
| 368 { | |
| 369 } | |
| 370 | |
| 371 bool isEmpty() const { return m_commands.isEmpty(); } | |
| 372 const Command& top() | |
| 373 { | |
| 374 ASSERT(!isEmpty()); | |
| 375 return m_commands.first(); | |
| 376 } | |
| 377 | |
| 378 void consume(size_t size) | |
| 379 { | |
| 380 ASSERT(!isEmpty()); | |
| 381 ASSERT(size + m_offset <= top().body().size()); | |
| 382 bool fullyConsumed = (size + m_offset >= top().body().size()); | |
| 383 if (fullyConsumed) { | |
| 384 m_offset = 0; | |
| 385 m_commands.removeFirst(); | |
| 386 } else { | |
| 387 m_offset += size; | |
| 388 } | |
| 389 } | |
| 390 | |
| 391 size_t offset() const { return m_offset; } | |
| 392 | |
| 393 void notify() | |
| 394 { | |
| 395 if (!m_client) | |
| 396 return; | |
| 397 ASSERT(m_readerThread); | |
| 398 m_readerThread->postTask(FROM_HERE, new Task(threadSafeBind(&Con text::notifyInternal, this))); | |
| 399 } | |
| 400 | |
| 401 void notifyInternal() | |
| 402 { | |
| 403 { | |
| 404 MutexLocker locker(m_mutex); | |
| 405 if (!m_client || !m_readerThread->isCurrentThread()) { | |
| 406 // There is no client, or a new reader is attached. | |
| 407 return; | |
| 408 } | |
| 409 } | |
| 410 // The reading thread is the current thread. | |
| 411 m_client->didGetReadable(); | |
| 412 } | |
| 413 | |
| 414 Deque<Command> m_commands; | |
| 415 size_t m_offset; | |
| 416 WebThread* m_readerThread; | |
| 417 Client* m_client; | |
| 418 Result m_result; | |
| 419 bool m_isHandleAttached; | |
| 420 Mutex m_mutex; | |
| 421 OwnPtr<WebWaitableEvent> m_detached; | |
| 422 }; | |
| 423 | |
| 424 Context* context() { return m_context.get(); } | |
| 425 | |
| 426 private: | |
| 427 class ReaderImpl; | |
| 428 | |
| 429 ReplayingHandle(); | |
| 430 Reader* obtainReaderInternal(Client*) override; | |
| 431 | |
| 432 RefPtr<Context> m_context; | |
| 433 }; | |
| 434 | |
| 214 }; | 435 }; |
| 215 | 436 |
| 216 } // namespace blink | 437 } // namespace blink |
| 217 | 438 |
| 218 #endif // DataConsumerHandleTestUtil_h | 439 #endif // DataConsumerHandleTestUtil_h |
| OLD | NEW |