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 #include "chrome/browser/sync/sync_stopped_reporter.h" | 5 #include "chrome/browser/sync/sync_stopped_reporter.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
12 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/sync/glue/local_device_info_provider_impl.h" | 14 #include "chrome/browser/sync/glue/local_device_info_provider_impl.h" |
15 #include "chrome/common/chrome_version_info.h" | 15 #include "chrome/common/chrome_version_info.h" |
16 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
17 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
18 #include "net/url_request/url_fetcher.h" | 18 #include "net/url_request/url_fetcher.h" |
19 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
20 #include "sync/protocol/sync.pb.h" | 20 #include "sync/protocol/sync.pb.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 const char kEventEndpoint[] = "event"; | 24 const char kEventEndpoint[] = "event"; |
25 | 25 |
26 // The request is tiny, so even on poor connections 10 seconds should be | 26 // The request is tiny, so even on poor connections 10 seconds should be |
27 // plenty of time. Since sync is off when this request is started, we don't | 27 // plenty of time. Since sync is off when this request is started, we don't |
28 // want anything sync-related hanging around for very long from a human | 28 // want anything sync-related hanging around for very long from a human |
29 // perspective either. This seems like a good compromise. | 29 // perspective either. This seems like a good compromise. |
30 const base::TimeDelta kRequestTimeout = base::TimeDelta::FromSeconds(10); | 30 const int kRequestTimeoutSeconds = 10; |
31 | 31 |
32 std::string GetUserAgent() { | 32 std::string GetUserAgent() { |
33 chrome::VersionInfo version_info; | 33 chrome::VersionInfo version_info; |
34 return browser_sync::LocalDeviceInfoProviderImpl::MakeUserAgentForSyncApi( | 34 return browser_sync::LocalDeviceInfoProviderImpl::MakeUserAgentForSyncApi( |
35 version_info); | 35 version_info); |
36 } | 36 } |
37 | 37 |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 namespace browser_sync { | 40 namespace browser_sync { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 access_token.c_str())); | 78 access_token.c_str())); |
79 fetcher_->AddExtraRequestHeader(base::StringPrintf( | 79 fetcher_->AddExtraRequestHeader(base::StringPrintf( |
80 "%s: %s", net::HttpRequestHeaders::kUserAgent, user_agent_.c_str())); | 80 "%s: %s", net::HttpRequestHeaders::kUserAgent, user_agent_.c_str())); |
81 fetcher_->SetRequestContext(request_context_.get()); | 81 fetcher_->SetRequestContext(request_context_.get()); |
82 fetcher_->SetUploadData("application/octet-stream", msg); | 82 fetcher_->SetUploadData("application/octet-stream", msg); |
83 fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | | 83 fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | |
84 net::LOAD_DISABLE_CACHE | | 84 net::LOAD_DISABLE_CACHE | |
85 net::LOAD_DO_NOT_SAVE_COOKIES | | 85 net::LOAD_DO_NOT_SAVE_COOKIES | |
86 net::LOAD_DO_NOT_SEND_COOKIES); | 86 net::LOAD_DO_NOT_SEND_COOKIES); |
87 fetcher_->Start(); | 87 fetcher_->Start(); |
88 timer_.Start(FROM_HERE, kRequestTimeout, this, | 88 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kRequestTimeoutSeconds), |
89 &SyncStoppedReporter::OnTimeout); | 89 this, &SyncStoppedReporter::OnTimeout); |
90 } | 90 } |
91 | 91 |
92 void SyncStoppedReporter::OnURLFetchComplete(const net::URLFetcher* source) { | 92 void SyncStoppedReporter::OnURLFetchComplete(const net::URLFetcher* source) { |
93 Result result = source->GetResponseCode() == net::HTTP_OK | 93 Result result = source->GetResponseCode() == net::HTTP_OK |
94 ? RESULT_SUCCESS : RESULT_ERROR; | 94 ? RESULT_SUCCESS : RESULT_ERROR; |
95 fetcher_.reset(); | 95 fetcher_.reset(); |
96 timer_.Stop(); | 96 timer_.Stop(); |
97 if (!callback_.is_null()) { | 97 if (!callback_.is_null()) { |
98 base::ThreadTaskRunnerHandle::Get()->PostTask( | 98 base::ThreadTaskRunnerHandle::Get()->PostTask( |
99 FROM_HERE, base::Bind(callback_, result)); | 99 FROM_HERE, base::Bind(callback_, result)); |
(...skipping 19 matching lines...) Expand all Loading... |
119 replacements.SetPathStr(path); | 119 replacements.SetPathStr(path); |
120 return sync_service_url.ReplaceComponents(replacements); | 120 return sync_service_url.ReplaceComponents(replacements); |
121 } | 121 } |
122 | 122 |
123 void SyncStoppedReporter::SetTimerTaskRunnerForTest( | 123 void SyncStoppedReporter::SetTimerTaskRunnerForTest( |
124 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | 124 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
125 timer_.SetTaskRunner(task_runner); | 125 timer_.SetTaskRunner(task_runner); |
126 } | 126 } |
127 | 127 |
128 } // namespace browser_sync | 128 } // namespace browser_sync |
OLD | NEW |