Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: sky/viewer/platform/platform_impl.cc

Issue 886263002: Move SharedTimer implementation into sky/engine (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: style Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/viewer/platform/platform_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "sky/viewer/platform/platform_impl.h" 5 #include "sky/viewer/platform/platform_impl.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "base/debug/trace_event.h" 9 #include "base/debug/trace_event.h"
10 #include "base/rand_util.h" 10 #include "base/rand_util.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "mojo/public/cpp/application/application_impl.h" 14 #include "mojo/public/cpp/application/application_impl.h"
15 #include "net/base/data_url.h" 15 #include "net/base/data_url.h"
16 #include "net/base/mime_util.h" 16 #include "net/base/mime_util.h"
17 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
18 #include "sky/viewer/platform/weburlloader_impl.h" 18 #include "sky/viewer/platform/weburlloader_impl.h"
19 19
20 namespace sky { 20 namespace sky {
21 21
22 PlatformImpl::PlatformImpl(mojo::ApplicationImpl* app) 22 PlatformImpl::PlatformImpl(mojo::ApplicationImpl* app)
23 : main_loop_(base::MessageLoop::current()), 23 : main_loop_(base::MessageLoop::current()),
24 main_thread_task_runner_(base::MessageLoop::current()->task_runner()), 24 main_thread_task_runner_(base::MessageLoop::current()->task_runner()) {
25 shared_timer_func_(NULL),
26 shared_timer_fire_time_(0.0),
27 shared_timer_fire_time_was_set_while_suspended_(false),
28 shared_timer_suspended_(0) {
29 app->ConnectToService("mojo:network_service", &network_service_); 25 app->ConnectToService("mojo:network_service", &network_service_);
30
31 mojo::CookieStorePtr cookie_store;
32 network_service_->GetCookieStore(GetProxy(&cookie_store));
33 } 26 }
34 27
35 PlatformImpl::~PlatformImpl() { 28 PlatformImpl::~PlatformImpl() {
36 } 29 }
37 30
38 blink::WebString PlatformImpl::defaultLocale() { 31 blink::WebString PlatformImpl::defaultLocale() {
39 return blink::WebString::fromUTF8("en-US"); 32 return blink::WebString::fromUTF8("en-US");
40 } 33 }
41 34
42 void PlatformImpl::setSharedTimerFiredFunction(void (*func)()) {
43 shared_timer_func_ = func;
44 }
45
46 void PlatformImpl::setSharedTimerFireInterval(
47 double interval_seconds) {
48 double now = base::TimeTicks::Now().ToInternalValue() /
49 static_cast<double>(base::Time::kMicrosecondsPerSecond);
50
51 shared_timer_fire_time_ = interval_seconds + now;
52 if (shared_timer_suspended_) {
53 shared_timer_fire_time_was_set_while_suspended_ = true;
54 return;
55 }
56
57 // By converting between double and int64 representation, we run the risk
58 // of losing precision due to rounding errors. Performing computations in
59 // microseconds reduces this risk somewhat. But there still is the potential
60 // of us computing a fire time for the timer that is shorter than what we
61 // need.
62 // As the event loop will check event deadlines prior to actually firing
63 // them, there is a risk of needlessly rescheduling events and of
64 // needlessly looping if sleep times are too short even by small amounts.
65 // This results in measurable performance degradation unless we use ceil() to
66 // always round up the sleep times.
67 int64 interval = static_cast<int64>(
68 ceil(interval_seconds * base::Time::kMillisecondsPerSecond)
69 * base::Time::kMicrosecondsPerMillisecond);
70
71 if (interval < 0)
72 interval = 0;
73
74 shared_timer_.Stop();
75 shared_timer_.Start(FROM_HERE, base::TimeDelta::FromMicroseconds(interval),
76 this, &PlatformImpl::DoTimeout);
77 }
78
79 void PlatformImpl::stopSharedTimer() {
80 shared_timer_.Stop();
81 }
82
83 base::SingleThreadTaskRunner* PlatformImpl::mainThreadTaskRunner() { 35 base::SingleThreadTaskRunner* PlatformImpl::mainThreadTaskRunner() {
84 return main_thread_task_runner_.get(); 36 return main_thread_task_runner_.get();
85 } 37 }
86 38
87 mojo::NetworkService* PlatformImpl::networkService() { 39 mojo::NetworkService* PlatformImpl::networkService() {
88 return network_service_.get(); 40 return network_service_.get();
89 } 41 }
90 42
91 blink::WebURLLoader* PlatformImpl::createURLLoader() { 43 blink::WebURLLoader* PlatformImpl::createURLLoader() {
92 return new WebURLLoaderImpl(network_service_.get()); 44 return new WebURLLoaderImpl(network_service_.get());
93 } 45 }
94 46
95 blink::WebURLError PlatformImpl::cancelledError(const blink::WebURL& url) 47 blink::WebURLError PlatformImpl::cancelledError(const blink::WebURL& url)
96 const { 48 const {
97 blink::WebURLError error; 49 blink::WebURLError error;
98 error.domain = blink::WebString::fromUTF8(net::kErrorDomain); 50 error.domain = blink::WebString::fromUTF8(net::kErrorDomain);
99 error.reason = net::ERR_ABORTED; 51 error.reason = net::ERR_ABORTED;
100 error.unreachableURL = url; 52 error.unreachableURL = url;
101 error.staleCopyInCache = false; 53 error.staleCopyInCache = false;
102 error.isCancellation = true; 54 error.isCancellation = true;
103 return error; 55 return error;
104 } 56 }
105 57
106 } // namespace sky 58 } // namespace sky
OLDNEW
« no previous file with comments | « sky/viewer/platform/platform_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698