2
* Copyright 2010 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Author: jmarantz@google.com (Joshua Marantz)
19
// Wraps an asynchronous fetcher, but keeps track of success/failure count.
21
#ifndef NET_INSTAWEB_HTTP_PUBLIC_COUNTING_URL_ASYNC_FETCHER_H_
22
#define NET_INSTAWEB_HTTP_PUBLIC_COUNTING_URL_ASYNC_FETCHER_H_
24
#include "net/instaweb/http/public/url_async_fetcher.h"
25
#include "net/instaweb/util/public/basictypes.h"
26
#include "net/instaweb/util/public/platform.h"
27
#include "net/instaweb/util/public/scoped_ptr.h"
28
#include "net/instaweb/util/public/string.h"
29
#include "net/instaweb/util/public/thread_system.h"
31
namespace net_instaweb {
36
class CountingUrlAsyncFetcher : public UrlAsyncFetcher {
38
// TODO(hujie): We should pass in the mutex at all call-sites instead of
39
// creating a new mutex here.
40
explicit CountingUrlAsyncFetcher(UrlAsyncFetcher* fetcher)
42
thread_system_(Platform::CreateThreadSystem()),
43
mutex_(thread_system_->NewMutex()) {
46
virtual ~CountingUrlAsyncFetcher();
48
void set_fetcher(UrlAsyncFetcher* fetcher) { fetcher_ = fetcher; }
50
virtual bool SupportsHttps() const { return fetcher_->SupportsHttps(); }
52
virtual void Fetch(const GoogleString& url,
53
MessageHandler* message_handler,
56
// number of completed fetches.
57
int fetch_count() const {
58
ScopedMutex lock(mutex_.get());
62
// number of started fetches
63
int fetch_start_count() const {
64
ScopedMutex lock(mutex_.get());
65
return fetch_start_count_;
67
int byte_count() const {
68
ScopedMutex lock(mutex_.get());
71
int failure_count() const {
72
ScopedMutex lock(mutex_.get());
73
return failure_count_;
75
GoogleString most_recent_fetched_url() const {
76
ScopedMutex lock(mutex_.get());
77
return most_recent_fetched_url_;
83
friend class CountingFetch;
86
UrlAsyncFetcher* fetcher_;
88
int fetch_start_count_;
91
GoogleString most_recent_fetched_url_;
92
scoped_ptr<ThreadSystem> thread_system_; // Thread system for mutex.
93
scoped_ptr<AbstractMutex> mutex_; // Mutex Protect.
95
DISALLOW_COPY_AND_ASSIGN(CountingUrlAsyncFetcher);
98
} // namespace net_instaweb
100
#endif // NET_INSTAWEB_HTTP_PUBLIC_COUNTING_URL_ASYNC_FETCHER_H_