Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / child / blink_platform_impl.cc
blob54e23eca45acdf9dd2142e53fa79b63364ce56f3
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
3 // found in the LICENSE file.
5 #include "content/child/blink_platform_impl.h"
7 #include <math.h>
9 #include <vector>
11 #include "base/allocator/allocator_extension.h"
12 #include "base/bind.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/singleton.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/metrics/histogram.h"
18 #include "base/metrics/sparse_histogram.h"
19 #include "base/metrics/stats_counters.h"
20 #include "base/process/process_metrics.h"
21 #include "base/rand_util.h"
22 #include "base/strings/string_number_conversions.h"
23 #include "base/strings/string_util.h"
24 #include "base/strings/utf_string_conversions.h"
25 #include "base/synchronization/lock.h"
26 #include "base/synchronization/waitable_event.h"
27 #include "base/sys_info.h"
28 #include "base/time/time.h"
29 #include "blink/public/resources/grit/blink_resources.h"
30 #include "content/app/resources/grit/content_resources.h"
31 #include "content/app/strings/grit/content_strings.h"
32 #include "content/child/child_thread.h"
33 #include "content/child/content_child_helpers.h"
34 #include "content/child/fling_curve_configuration.h"
35 #include "content/child/web_discardable_memory_impl.h"
36 #include "content/child/web_socket_stream_handle_impl.h"
37 #include "content/child/web_url_loader_impl.h"
38 #include "content/child/websocket_bridge.h"
39 #include "content/child/webthread_impl.h"
40 #include "content/child/worker_task_runner.h"
41 #include "content/public/common/content_client.h"
42 #include "net/base/data_url.h"
43 #include "net/base/mime_util.h"
44 #include "net/base/net_errors.h"
45 #include "net/base/net_util.h"
46 #include "third_party/WebKit/public/platform/WebConvertableToTraceFormat.h"
47 #include "third_party/WebKit/public/platform/WebData.h"
48 #include "third_party/WebKit/public/platform/WebString.h"
49 #include "third_party/WebKit/public/platform/WebURL.h"
50 #include "third_party/WebKit/public/platform/WebWaitableEvent.h"
51 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
52 #include "ui/base/layout.h"
54 #if defined(OS_ANDROID)
55 #include "content/child/fling_animator_impl_android.h"
56 #endif
58 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
59 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
60 #endif
62 using blink::WebData;
63 using blink::WebFallbackThemeEngine;
64 using blink::WebLocalizedString;
65 using blink::WebString;
66 using blink::WebSocketStreamHandle;
67 using blink::WebThemeEngine;
68 using blink::WebURL;
69 using blink::WebURLError;
70 using blink::WebURLLoader;
72 namespace content {
74 namespace {
76 class WebWaitableEventImpl : public blink::WebWaitableEvent {
77 public:
78 WebWaitableEventImpl() : impl_(new base::WaitableEvent(false, false)) {}
79 virtual ~WebWaitableEventImpl() {}
81 virtual void wait() { impl_->Wait(); }
82 virtual void signal() { impl_->Signal(); }
84 base::WaitableEvent* impl() {
85 return impl_.get();
88 private:
89 scoped_ptr<base::WaitableEvent> impl_;
90 DISALLOW_COPY_AND_ASSIGN(WebWaitableEventImpl);
93 // A simple class to cache the memory usage for a given amount of time.
94 class MemoryUsageCache {
95 public:
96 // Retrieves the Singleton.
97 static MemoryUsageCache* GetInstance() {
98 return Singleton<MemoryUsageCache>::get();
101 MemoryUsageCache() : memory_value_(0) { Init(); }
102 ~MemoryUsageCache() {}
104 void Init() {
105 const unsigned int kCacheSeconds = 1;
106 cache_valid_time_ = base::TimeDelta::FromSeconds(kCacheSeconds);
109 // Returns true if the cached value is fresh.
110 // Returns false if the cached value is stale, or if |cached_value| is NULL.
111 bool IsCachedValueValid(size_t* cached_value) {
112 base::AutoLock scoped_lock(lock_);
113 if (!cached_value)
114 return false;
115 if (base::Time::Now() - last_updated_time_ > cache_valid_time_)
116 return false;
117 *cached_value = memory_value_;
118 return true;
121 // Setter for |memory_value_|, refreshes |last_updated_time_|.
122 void SetMemoryValue(const size_t value) {
123 base::AutoLock scoped_lock(lock_);
124 memory_value_ = value;
125 last_updated_time_ = base::Time::Now();
128 private:
129 // The cached memory value.
130 size_t memory_value_;
132 // How long the cached value should remain valid.
133 base::TimeDelta cache_valid_time_;
135 // The last time the cached value was updated.
136 base::Time last_updated_time_;
138 base::Lock lock_;
141 class ConvertableToTraceFormatWrapper
142 : public base::debug::ConvertableToTraceFormat {
143 public:
144 explicit ConvertableToTraceFormatWrapper(
145 const blink::WebConvertableToTraceFormat& convertable)
146 : convertable_(convertable) {}
147 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE {
148 *out += convertable_.asTraceFormat().utf8();
151 private:
152 virtual ~ConvertableToTraceFormatWrapper() {}
154 blink::WebConvertableToTraceFormat convertable_;
157 bool isHostnameReservedIPAddress(const std::string& host) {
158 net::IPAddressNumber address;
159 if (!net::ParseURLHostnameToNumber(host, &address))
160 return false;
161 return net::IsIPAddressReserved(address);
164 } // namespace
166 static int ToMessageID(WebLocalizedString::Name name) {
167 switch (name) {
168 case WebLocalizedString::AXAMPMFieldText:
169 return IDS_AX_AM_PM_FIELD_TEXT;
170 case WebLocalizedString::AXButtonActionVerb:
171 return IDS_AX_BUTTON_ACTION_VERB;
172 case WebLocalizedString::AXCheckedCheckBoxActionVerb:
173 return IDS_AX_CHECKED_CHECK_BOX_ACTION_VERB;
174 case WebLocalizedString::AXDateTimeFieldEmptyValueText:
175 return IDS_AX_DATE_TIME_FIELD_EMPTY_VALUE_TEXT;
176 case WebLocalizedString::AXDayOfMonthFieldText:
177 return IDS_AX_DAY_OF_MONTH_FIELD_TEXT;
178 case WebLocalizedString::AXHeadingText:
179 return IDS_AX_ROLE_HEADING;
180 case WebLocalizedString::AXHourFieldText:
181 return IDS_AX_HOUR_FIELD_TEXT;
182 case WebLocalizedString::AXImageMapText:
183 return IDS_AX_ROLE_IMAGE_MAP;
184 case WebLocalizedString::AXLinkActionVerb:
185 return IDS_AX_LINK_ACTION_VERB;
186 case WebLocalizedString::AXLinkText:
187 return IDS_AX_ROLE_LINK;
188 case WebLocalizedString::AXListMarkerText:
189 return IDS_AX_ROLE_LIST_MARKER;
190 case WebLocalizedString::AXMediaDefault:
191 return IDS_AX_MEDIA_DEFAULT;
192 case WebLocalizedString::AXMediaAudioElement:
193 return IDS_AX_MEDIA_AUDIO_ELEMENT;
194 case WebLocalizedString::AXMediaVideoElement:
195 return IDS_AX_MEDIA_VIDEO_ELEMENT;
196 case WebLocalizedString::AXMediaMuteButton:
197 return IDS_AX_MEDIA_MUTE_BUTTON;
198 case WebLocalizedString::AXMediaUnMuteButton:
199 return IDS_AX_MEDIA_UNMUTE_BUTTON;
200 case WebLocalizedString::AXMediaPlayButton:
201 return IDS_AX_MEDIA_PLAY_BUTTON;
202 case WebLocalizedString::AXMediaPauseButton:
203 return IDS_AX_MEDIA_PAUSE_BUTTON;
204 case WebLocalizedString::AXMediaSlider:
205 return IDS_AX_MEDIA_SLIDER;
206 case WebLocalizedString::AXMediaSliderThumb:
207 return IDS_AX_MEDIA_SLIDER_THUMB;
208 case WebLocalizedString::AXMediaCurrentTimeDisplay:
209 return IDS_AX_MEDIA_CURRENT_TIME_DISPLAY;
210 case WebLocalizedString::AXMediaTimeRemainingDisplay:
211 return IDS_AX_MEDIA_TIME_REMAINING_DISPLAY;
212 case WebLocalizedString::AXMediaStatusDisplay:
213 return IDS_AX_MEDIA_STATUS_DISPLAY;
214 case WebLocalizedString::AXMediaEnterFullscreenButton:
215 return IDS_AX_MEDIA_ENTER_FULL_SCREEN_BUTTON;
216 case WebLocalizedString::AXMediaExitFullscreenButton:
217 return IDS_AX_MEDIA_EXIT_FULL_SCREEN_BUTTON;
218 case WebLocalizedString::AXMediaShowClosedCaptionsButton:
219 return IDS_AX_MEDIA_SHOW_CLOSED_CAPTIONS_BUTTON;
220 case WebLocalizedString::AXMediaHideClosedCaptionsButton:
221 return IDS_AX_MEDIA_HIDE_CLOSED_CAPTIONS_BUTTON;
222 case WebLocalizedString::AXMediaAudioElementHelp:
223 return IDS_AX_MEDIA_AUDIO_ELEMENT_HELP;
224 case WebLocalizedString::AXMediaVideoElementHelp:
225 return IDS_AX_MEDIA_VIDEO_ELEMENT_HELP;
226 case WebLocalizedString::AXMediaMuteButtonHelp:
227 return IDS_AX_MEDIA_MUTE_BUTTON_HELP;
228 case WebLocalizedString::AXMediaUnMuteButtonHelp:
229 return IDS_AX_MEDIA_UNMUTE_BUTTON_HELP;
230 case WebLocalizedString::AXMediaPlayButtonHelp:
231 return IDS_AX_MEDIA_PLAY_BUTTON_HELP;
232 case WebLocalizedString::AXMediaPauseButtonHelp:
233 return IDS_AX_MEDIA_PAUSE_BUTTON_HELP;
234 case WebLocalizedString::AXMediaSliderHelp:
235 return IDS_AX_MEDIA_SLIDER_HELP;
236 case WebLocalizedString::AXMediaSliderThumbHelp:
237 return IDS_AX_MEDIA_SLIDER_THUMB_HELP;
238 case WebLocalizedString::AXMediaCurrentTimeDisplayHelp:
239 return IDS_AX_MEDIA_CURRENT_TIME_DISPLAY_HELP;
240 case WebLocalizedString::AXMediaTimeRemainingDisplayHelp:
241 return IDS_AX_MEDIA_TIME_REMAINING_DISPLAY_HELP;
242 case WebLocalizedString::AXMediaStatusDisplayHelp:
243 return IDS_AX_MEDIA_STATUS_DISPLAY_HELP;
244 case WebLocalizedString::AXMediaEnterFullscreenButtonHelp:
245 return IDS_AX_MEDIA_ENTER_FULL_SCREEN_BUTTON_HELP;
246 case WebLocalizedString::AXMediaExitFullscreenButtonHelp:
247 return IDS_AX_MEDIA_EXIT_FULL_SCREEN_BUTTON_HELP;
248 case WebLocalizedString::AXMediaShowClosedCaptionsButtonHelp:
249 return IDS_AX_MEDIA_SHOW_CLOSED_CAPTIONS_BUTTON_HELP;
250 case WebLocalizedString::AXMediaHideClosedCaptionsButtonHelp:
251 return IDS_AX_MEDIA_HIDE_CLOSED_CAPTIONS_BUTTON_HELP;
252 case WebLocalizedString::AXMillisecondFieldText:
253 return IDS_AX_MILLISECOND_FIELD_TEXT;
254 case WebLocalizedString::AXMinuteFieldText:
255 return IDS_AX_MINUTE_FIELD_TEXT;
256 case WebLocalizedString::AXMonthFieldText:
257 return IDS_AX_MONTH_FIELD_TEXT;
258 case WebLocalizedString::AXRadioButtonActionVerb:
259 return IDS_AX_RADIO_BUTTON_ACTION_VERB;
260 case WebLocalizedString::AXSecondFieldText:
261 return IDS_AX_SECOND_FIELD_TEXT;
262 case WebLocalizedString::AXTextFieldActionVerb:
263 return IDS_AX_TEXT_FIELD_ACTION_VERB;
264 case WebLocalizedString::AXUncheckedCheckBoxActionVerb:
265 return IDS_AX_UNCHECKED_CHECK_BOX_ACTION_VERB;
266 case WebLocalizedString::AXWebAreaText:
267 return IDS_AX_ROLE_WEB_AREA;
268 case WebLocalizedString::AXWeekOfYearFieldText:
269 return IDS_AX_WEEK_OF_YEAR_FIELD_TEXT;
270 case WebLocalizedString::AXYearFieldText:
271 return IDS_AX_YEAR_FIELD_TEXT;
272 case WebLocalizedString::CalendarClear:
273 return IDS_FORM_CALENDAR_CLEAR;
274 case WebLocalizedString::CalendarToday:
275 return IDS_FORM_CALENDAR_TODAY;
276 case WebLocalizedString::DateFormatDayInMonthLabel:
277 return IDS_FORM_DATE_FORMAT_DAY_IN_MONTH;
278 case WebLocalizedString::DateFormatMonthLabel:
279 return IDS_FORM_DATE_FORMAT_MONTH;
280 case WebLocalizedString::DateFormatYearLabel:
281 return IDS_FORM_DATE_FORMAT_YEAR;
282 case WebLocalizedString::DetailsLabel:
283 return IDS_DETAILS_WITHOUT_SUMMARY_LABEL;
284 case WebLocalizedString::FileButtonChooseFileLabel:
285 return IDS_FORM_FILE_BUTTON_LABEL;
286 case WebLocalizedString::FileButtonChooseMultipleFilesLabel:
287 return IDS_FORM_MULTIPLE_FILES_BUTTON_LABEL;
288 case WebLocalizedString::FileButtonNoFileSelectedLabel:
289 return IDS_FORM_FILE_NO_FILE_LABEL;
290 case WebLocalizedString::InputElementAltText:
291 return IDS_FORM_INPUT_ALT;
292 case WebLocalizedString::KeygenMenuHighGradeKeySize:
293 return IDS_KEYGEN_HIGH_GRADE_KEY;
294 case WebLocalizedString::KeygenMenuMediumGradeKeySize:
295 return IDS_KEYGEN_MED_GRADE_KEY;
296 case WebLocalizedString::MissingPluginText:
297 return IDS_PLUGIN_INITIALIZATION_ERROR;
298 case WebLocalizedString::MultipleFileUploadText:
299 return IDS_FORM_FILE_MULTIPLE_UPLOAD;
300 case WebLocalizedString::OtherColorLabel:
301 return IDS_FORM_OTHER_COLOR_LABEL;
302 case WebLocalizedString::OtherDateLabel:
303 return IDS_FORM_OTHER_DATE_LABEL;
304 case WebLocalizedString::OtherMonthLabel:
305 return IDS_FORM_OTHER_MONTH_LABEL;
306 case WebLocalizedString::OtherTimeLabel:
307 return IDS_FORM_OTHER_TIME_LABEL;
308 case WebLocalizedString::OtherWeekLabel:
309 return IDS_FORM_OTHER_WEEK_LABEL;
310 case WebLocalizedString::PlaceholderForDayOfMonthField:
311 return IDS_FORM_PLACEHOLDER_FOR_DAY_OF_MONTH_FIELD;
312 case WebLocalizedString::PlaceholderForMonthField:
313 return IDS_FORM_PLACEHOLDER_FOR_MONTH_FIELD;
314 case WebLocalizedString::PlaceholderForYearField:
315 return IDS_FORM_PLACEHOLDER_FOR_YEAR_FIELD;
316 case WebLocalizedString::ResetButtonDefaultLabel:
317 return IDS_FORM_RESET_LABEL;
318 case WebLocalizedString::SearchableIndexIntroduction:
319 return IDS_SEARCHABLE_INDEX_INTRO;
320 case WebLocalizedString::SearchMenuClearRecentSearchesText:
321 return IDS_RECENT_SEARCHES_CLEAR;
322 case WebLocalizedString::SearchMenuNoRecentSearchesText:
323 return IDS_RECENT_SEARCHES_NONE;
324 case WebLocalizedString::SearchMenuRecentSearchesText:
325 return IDS_RECENT_SEARCHES;
326 case WebLocalizedString::SelectMenuListText:
327 return IDS_FORM_SELECT_MENU_LIST_TEXT;
328 case WebLocalizedString::SubmitButtonDefaultLabel:
329 return IDS_FORM_SUBMIT_LABEL;
330 case WebLocalizedString::ThisMonthButtonLabel:
331 return IDS_FORM_THIS_MONTH_LABEL;
332 case WebLocalizedString::ThisWeekButtonLabel:
333 return IDS_FORM_THIS_WEEK_LABEL;
334 case WebLocalizedString::ValidationBadInputForDateTime:
335 return IDS_FORM_VALIDATION_BAD_INPUT_DATETIME;
336 case WebLocalizedString::ValidationBadInputForNumber:
337 return IDS_FORM_VALIDATION_BAD_INPUT_NUMBER;
338 case WebLocalizedString::ValidationPatternMismatch:
339 return IDS_FORM_VALIDATION_PATTERN_MISMATCH;
340 case WebLocalizedString::ValidationRangeOverflow:
341 return IDS_FORM_VALIDATION_RANGE_OVERFLOW;
342 case WebLocalizedString::ValidationRangeOverflowDateTime:
343 return IDS_FORM_VALIDATION_RANGE_OVERFLOW_DATETIME;
344 case WebLocalizedString::ValidationRangeUnderflow:
345 return IDS_FORM_VALIDATION_RANGE_UNDERFLOW;
346 case WebLocalizedString::ValidationRangeUnderflowDateTime:
347 return IDS_FORM_VALIDATION_RANGE_UNDERFLOW_DATETIME;
348 case WebLocalizedString::ValidationStepMismatch:
349 return IDS_FORM_VALIDATION_STEP_MISMATCH;
350 case WebLocalizedString::ValidationStepMismatchCloseToLimit:
351 return IDS_FORM_VALIDATION_STEP_MISMATCH_CLOSE_TO_LIMIT;
352 case WebLocalizedString::ValidationTooLong:
353 return IDS_FORM_VALIDATION_TOO_LONG;
354 case WebLocalizedString::ValidationTypeMismatch:
355 return IDS_FORM_VALIDATION_TYPE_MISMATCH;
356 case WebLocalizedString::ValidationTypeMismatchForEmail:
357 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL;
358 case WebLocalizedString::ValidationTypeMismatchForEmailEmpty:
359 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_EMPTY;
360 case WebLocalizedString::ValidationTypeMismatchForEmailEmptyDomain:
361 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_EMPTY_DOMAIN;
362 case WebLocalizedString::ValidationTypeMismatchForEmailEmptyLocal:
363 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_EMPTY_LOCAL;
364 case WebLocalizedString::ValidationTypeMismatchForEmailInvalidDomain:
365 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_INVALID_DOMAIN;
366 case WebLocalizedString::ValidationTypeMismatchForEmailInvalidDots:
367 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_INVALID_DOTS;
368 case WebLocalizedString::ValidationTypeMismatchForEmailInvalidLocal:
369 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_INVALID_LOCAL;
370 case WebLocalizedString::ValidationTypeMismatchForEmailNoAtSign:
371 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_NO_AT_SIGN;
372 case WebLocalizedString::ValidationTypeMismatchForMultipleEmail:
373 return IDS_FORM_VALIDATION_TYPE_MISMATCH_MULTIPLE_EMAIL;
374 case WebLocalizedString::ValidationTypeMismatchForURL:
375 return IDS_FORM_VALIDATION_TYPE_MISMATCH_URL;
376 case WebLocalizedString::ValidationValueMissing:
377 return IDS_FORM_VALIDATION_VALUE_MISSING;
378 case WebLocalizedString::ValidationValueMissingForCheckbox:
379 return IDS_FORM_VALIDATION_VALUE_MISSING_CHECKBOX;
380 case WebLocalizedString::ValidationValueMissingForFile:
381 return IDS_FORM_VALIDATION_VALUE_MISSING_FILE;
382 case WebLocalizedString::ValidationValueMissingForMultipleFile:
383 return IDS_FORM_VALIDATION_VALUE_MISSING_MULTIPLE_FILE;
384 case WebLocalizedString::ValidationValueMissingForRadio:
385 return IDS_FORM_VALIDATION_VALUE_MISSING_RADIO;
386 case WebLocalizedString::ValidationValueMissingForSelect:
387 return IDS_FORM_VALIDATION_VALUE_MISSING_SELECT;
388 case WebLocalizedString::WeekFormatTemplate:
389 return IDS_FORM_INPUT_WEEK_TEMPLATE;
390 case WebLocalizedString::WeekNumberLabel:
391 return IDS_FORM_WEEK_NUMBER_LABEL;
392 // This "default:" line exists to avoid compile warnings about enum
393 // coverage when we add a new symbol to WebLocalizedString.h in WebKit.
394 // After a planned WebKit patch is landed, we need to add a case statement
395 // for the added symbol here.
396 default:
397 break;
399 return -1;
402 BlinkPlatformImpl::BlinkPlatformImpl()
403 : main_loop_(base::MessageLoop::current()),
404 shared_timer_func_(NULL),
405 shared_timer_fire_time_(0.0),
406 shared_timer_fire_time_was_set_while_suspended_(false),
407 shared_timer_suspended_(0),
408 fling_curve_configuration_(new FlingCurveConfiguration),
409 current_thread_slot_(&DestroyCurrentThread) {}
411 BlinkPlatformImpl::~BlinkPlatformImpl() {
414 WebURLLoader* BlinkPlatformImpl::createURLLoader() {
415 ChildThread* child_thread = ChildThread::current();
416 // There may be no child thread in RenderViewTests. These tests can still use
417 // data URLs to bypass the ResourceDispatcher.
418 return new WebURLLoaderImpl(
419 child_thread ? child_thread->resource_dispatcher() : NULL);
422 WebSocketStreamHandle* BlinkPlatformImpl::createSocketStreamHandle() {
423 return new WebSocketStreamHandleImpl;
426 blink::WebSocketHandle* BlinkPlatformImpl::createWebSocketHandle() {
427 return new WebSocketBridge;
430 WebString BlinkPlatformImpl::userAgent() {
431 return WebString::fromUTF8(GetContentClient()->GetUserAgent());
434 WebData BlinkPlatformImpl::parseDataURL(const WebURL& url,
435 WebString& mimetype_out,
436 WebString& charset_out) {
437 std::string mime_type, char_set, data;
438 if (net::DataURL::Parse(url, &mime_type, &char_set, &data)
439 && net::IsSupportedMimeType(mime_type)) {
440 mimetype_out = WebString::fromUTF8(mime_type);
441 charset_out = WebString::fromUTF8(char_set);
442 return data;
444 return WebData();
447 WebURLError BlinkPlatformImpl::cancelledError(
448 const WebURL& unreachableURL) const {
449 return WebURLLoaderImpl::CreateError(unreachableURL, false, net::ERR_ABORTED);
452 bool BlinkPlatformImpl::isReservedIPAddress(
453 const blink::WebSecurityOrigin& securityOrigin) const {
454 return isHostnameReservedIPAddress(securityOrigin.host().utf8());
457 bool BlinkPlatformImpl::isReservedIPAddress(const blink::WebURL& url) const {
458 return isHostnameReservedIPAddress(GURL(url).host());
461 blink::WebThread* BlinkPlatformImpl::createThread(const char* name) {
462 return new WebThreadImpl(name);
465 blink::WebThread* BlinkPlatformImpl::currentThread() {
466 WebThreadImplForMessageLoop* thread =
467 static_cast<WebThreadImplForMessageLoop*>(current_thread_slot_.Get());
468 if (thread)
469 return (thread);
471 scoped_refptr<base::MessageLoopProxy> message_loop =
472 base::MessageLoopProxy::current();
473 if (!message_loop.get())
474 return NULL;
476 thread = new WebThreadImplForMessageLoop(message_loop.get());
477 current_thread_slot_.Set(thread);
478 return thread;
481 blink::WebWaitableEvent* BlinkPlatformImpl::createWaitableEvent() {
482 return new WebWaitableEventImpl();
485 blink::WebWaitableEvent* BlinkPlatformImpl::waitMultipleEvents(
486 const blink::WebVector<blink::WebWaitableEvent*>& web_events) {
487 std::vector<base::WaitableEvent*> events;
488 for (size_t i = 0; i < web_events.size(); ++i)
489 events.push_back(static_cast<WebWaitableEventImpl*>(web_events[i])->impl());
490 size_t idx = base::WaitableEvent::WaitMany(
491 vector_as_array(&events), events.size());
492 DCHECK_LT(idx, web_events.size());
493 return web_events[idx];
496 void BlinkPlatformImpl::decrementStatsCounter(const char* name) {
497 base::StatsCounter(name).Decrement();
500 void BlinkPlatformImpl::incrementStatsCounter(const char* name) {
501 base::StatsCounter(name).Increment();
504 void BlinkPlatformImpl::histogramCustomCounts(
505 const char* name, int sample, int min, int max, int bucket_count) {
506 // Copied from histogram macro, but without the static variable caching
507 // the histogram because name is dynamic.
508 base::HistogramBase* counter =
509 base::Histogram::FactoryGet(name, min, max, bucket_count,
510 base::HistogramBase::kUmaTargetedHistogramFlag);
511 DCHECK_EQ(name, counter->histogram_name());
512 counter->Add(sample);
515 void BlinkPlatformImpl::histogramEnumeration(
516 const char* name, int sample, int boundary_value) {
517 // Copied from histogram macro, but without the static variable caching
518 // the histogram because name is dynamic.
519 base::HistogramBase* counter =
520 base::LinearHistogram::FactoryGet(name, 1, boundary_value,
521 boundary_value + 1, base::HistogramBase::kUmaTargetedHistogramFlag);
522 DCHECK_EQ(name, counter->histogram_name());
523 counter->Add(sample);
526 void BlinkPlatformImpl::histogramSparse(const char* name, int sample) {
527 // For sparse histograms, we can use the macro, as it does not incorporate a
528 // static.
529 UMA_HISTOGRAM_SPARSE_SLOWLY(name, sample);
532 const unsigned char* BlinkPlatformImpl::getTraceCategoryEnabledFlag(
533 const char* category_group) {
534 return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group);
537 long* BlinkPlatformImpl::getTraceSamplingState(
538 const unsigned thread_bucket) {
539 switch (thread_bucket) {
540 case 0:
541 return reinterpret_cast<long*>(&TRACE_EVENT_API_THREAD_BUCKET(0));
542 case 1:
543 return reinterpret_cast<long*>(&TRACE_EVENT_API_THREAD_BUCKET(1));
544 case 2:
545 return reinterpret_cast<long*>(&TRACE_EVENT_API_THREAD_BUCKET(2));
546 default:
547 NOTREACHED() << "Unknown thread bucket type.";
549 return NULL;
552 COMPILE_ASSERT(
553 sizeof(blink::Platform::TraceEventHandle) ==
554 sizeof(base::debug::TraceEventHandle),
555 TraceEventHandle_types_must_be_same_size);
557 blink::Platform::TraceEventHandle BlinkPlatformImpl::addTraceEvent(
558 char phase,
559 const unsigned char* category_group_enabled,
560 const char* name,
561 unsigned long long id,
562 int num_args,
563 const char** arg_names,
564 const unsigned char* arg_types,
565 const unsigned long long* arg_values,
566 unsigned char flags) {
567 base::debug::TraceEventHandle handle = TRACE_EVENT_API_ADD_TRACE_EVENT(
568 phase, category_group_enabled, name, id,
569 num_args, arg_names, arg_types, arg_values, NULL, flags);
570 blink::Platform::TraceEventHandle result;
571 memcpy(&result, &handle, sizeof(result));
572 return result;
575 blink::Platform::TraceEventHandle BlinkPlatformImpl::addTraceEvent(
576 char phase,
577 const unsigned char* category_group_enabled,
578 const char* name,
579 unsigned long long id,
580 int num_args,
581 const char** arg_names,
582 const unsigned char* arg_types,
583 const unsigned long long* arg_values,
584 const blink::WebConvertableToTraceFormat* convertable_values,
585 unsigned char flags) {
586 scoped_refptr<base::debug::ConvertableToTraceFormat> convertable_wrappers[2];
587 if (convertable_values) {
588 size_t size = std::min(static_cast<size_t>(num_args),
589 arraysize(convertable_wrappers));
590 for (size_t i = 0; i < size; ++i) {
591 if (arg_types[i] == TRACE_VALUE_TYPE_CONVERTABLE) {
592 convertable_wrappers[i] =
593 new ConvertableToTraceFormatWrapper(convertable_values[i]);
597 base::debug::TraceEventHandle handle =
598 TRACE_EVENT_API_ADD_TRACE_EVENT(phase,
599 category_group_enabled,
600 name,
602 num_args,
603 arg_names,
604 arg_types,
605 arg_values,
606 convertable_wrappers,
607 flags);
608 blink::Platform::TraceEventHandle result;
609 memcpy(&result, &handle, sizeof(result));
610 return result;
613 void BlinkPlatformImpl::updateTraceEventDuration(
614 const unsigned char* category_group_enabled,
615 const char* name,
616 TraceEventHandle handle) {
617 base::debug::TraceEventHandle traceEventHandle;
618 memcpy(&traceEventHandle, &handle, sizeof(handle));
619 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
620 category_group_enabled, name, traceEventHandle);
623 namespace {
625 WebData loadAudioSpatializationResource(const char* name) {
626 #ifdef IDR_AUDIO_SPATIALIZATION_COMPOSITE
627 if (!strcmp(name, "Composite")) {
628 base::StringPiece resource = GetContentClient()->GetDataResource(
629 IDR_AUDIO_SPATIALIZATION_COMPOSITE, ui::SCALE_FACTOR_NONE);
630 return WebData(resource.data(), resource.size());
632 #endif
634 #ifdef IDR_AUDIO_SPATIALIZATION_T000_P000
635 const size_t kExpectedSpatializationNameLength = 31;
636 if (strlen(name) != kExpectedSpatializationNameLength) {
637 return WebData();
640 // Extract the azimuth and elevation from the resource name.
641 int azimuth = 0;
642 int elevation = 0;
643 int values_parsed =
644 sscanf(name, "IRC_Composite_C_R0195_T%3d_P%3d", &azimuth, &elevation);
645 if (values_parsed != 2) {
646 return WebData();
649 // The resource index values go through the elevations first, then azimuths.
650 const int kAngleSpacing = 15;
652 // 0 <= elevation <= 90 (or 315 <= elevation <= 345)
653 // in increments of 15 degrees.
654 int elevation_index =
655 elevation <= 90 ? elevation / kAngleSpacing :
656 7 + (elevation - 315) / kAngleSpacing;
657 bool is_elevation_index_good = 0 <= elevation_index && elevation_index < 10;
659 // 0 <= azimuth < 360 in increments of 15 degrees.
660 int azimuth_index = azimuth / kAngleSpacing;
661 bool is_azimuth_index_good = 0 <= azimuth_index && azimuth_index < 24;
663 const int kNumberOfElevations = 10;
664 const int kNumberOfAudioResources = 240;
665 int resource_index = kNumberOfElevations * azimuth_index + elevation_index;
666 bool is_resource_index_good = 0 <= resource_index &&
667 resource_index < kNumberOfAudioResources;
669 if (is_azimuth_index_good && is_elevation_index_good &&
670 is_resource_index_good) {
671 const int kFirstAudioResourceIndex = IDR_AUDIO_SPATIALIZATION_T000_P000;
672 base::StringPiece resource = GetContentClient()->GetDataResource(
673 kFirstAudioResourceIndex + resource_index, ui::SCALE_FACTOR_NONE);
674 return WebData(resource.data(), resource.size());
676 #endif // IDR_AUDIO_SPATIALIZATION_T000_P000
678 NOTREACHED();
679 return WebData();
682 struct DataResource {
683 const char* name;
684 int id;
685 ui::ScaleFactor scale_factor;
688 const DataResource kDataResources[] = {
689 { "missingImage", IDR_BROKENIMAGE, ui::SCALE_FACTOR_100P },
690 { "missingImage@2x", IDR_BROKENIMAGE, ui::SCALE_FACTOR_200P },
691 { "mediaplayerPause", IDR_MEDIAPLAYER_PAUSE_BUTTON, ui::SCALE_FACTOR_100P },
692 { "mediaplayerPauseHover",
693 IDR_MEDIAPLAYER_PAUSE_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
694 { "mediaplayerPauseDown",
695 IDR_MEDIAPLAYER_PAUSE_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
696 { "mediaplayerPlay", IDR_MEDIAPLAYER_PLAY_BUTTON, ui::SCALE_FACTOR_100P },
697 { "mediaplayerPlayHover",
698 IDR_MEDIAPLAYER_PLAY_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
699 { "mediaplayerPlayDown",
700 IDR_MEDIAPLAYER_PLAY_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
701 { "mediaplayerPlayDisabled",
702 IDR_MEDIAPLAYER_PLAY_BUTTON_DISABLED, ui::SCALE_FACTOR_100P },
703 { "mediaplayerSoundLevel3",
704 IDR_MEDIAPLAYER_SOUND_LEVEL3_BUTTON, ui::SCALE_FACTOR_100P },
705 { "mediaplayerSoundLevel3Hover",
706 IDR_MEDIAPLAYER_SOUND_LEVEL3_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
707 { "mediaplayerSoundLevel3Down",
708 IDR_MEDIAPLAYER_SOUND_LEVEL3_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
709 { "mediaplayerSoundLevel2",
710 IDR_MEDIAPLAYER_SOUND_LEVEL2_BUTTON, ui::SCALE_FACTOR_100P },
711 { "mediaplayerSoundLevel2Hover",
712 IDR_MEDIAPLAYER_SOUND_LEVEL2_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
713 { "mediaplayerSoundLevel2Down",
714 IDR_MEDIAPLAYER_SOUND_LEVEL2_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
715 { "mediaplayerSoundLevel1",
716 IDR_MEDIAPLAYER_SOUND_LEVEL1_BUTTON, ui::SCALE_FACTOR_100P },
717 { "mediaplayerSoundLevel1Hover",
718 IDR_MEDIAPLAYER_SOUND_LEVEL1_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
719 { "mediaplayerSoundLevel1Down",
720 IDR_MEDIAPLAYER_SOUND_LEVEL1_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
721 { "mediaplayerSoundLevel0",
722 IDR_MEDIAPLAYER_SOUND_LEVEL0_BUTTON, ui::SCALE_FACTOR_100P },
723 { "mediaplayerSoundLevel0Hover",
724 IDR_MEDIAPLAYER_SOUND_LEVEL0_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
725 { "mediaplayerSoundLevel0Down",
726 IDR_MEDIAPLAYER_SOUND_LEVEL0_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
727 { "mediaplayerSoundDisabled",
728 IDR_MEDIAPLAYER_SOUND_DISABLED, ui::SCALE_FACTOR_100P },
729 { "mediaplayerSliderThumb",
730 IDR_MEDIAPLAYER_SLIDER_THUMB, ui::SCALE_FACTOR_100P },
731 { "mediaplayerSliderThumbHover",
732 IDR_MEDIAPLAYER_SLIDER_THUMB_HOVER, ui::SCALE_FACTOR_100P },
733 { "mediaplayerSliderThumbDown",
734 IDR_MEDIAPLAYER_SLIDER_THUMB_DOWN, ui::SCALE_FACTOR_100P },
735 { "mediaplayerVolumeSliderThumb",
736 IDR_MEDIAPLAYER_VOLUME_SLIDER_THUMB, ui::SCALE_FACTOR_100P },
737 { "mediaplayerVolumeSliderThumbHover",
738 IDR_MEDIAPLAYER_VOLUME_SLIDER_THUMB_HOVER, ui::SCALE_FACTOR_100P },
739 { "mediaplayerVolumeSliderThumbDown",
740 IDR_MEDIAPLAYER_VOLUME_SLIDER_THUMB_DOWN, ui::SCALE_FACTOR_100P },
741 { "mediaplayerVolumeSliderThumbDisabled",
742 IDR_MEDIAPLAYER_VOLUME_SLIDER_THUMB_DISABLED, ui::SCALE_FACTOR_100P },
743 { "mediaplayerClosedCaption",
744 IDR_MEDIAPLAYER_CLOSEDCAPTION_BUTTON, ui::SCALE_FACTOR_100P },
745 { "mediaplayerClosedCaptionHover",
746 IDR_MEDIAPLAYER_CLOSEDCAPTION_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
747 { "mediaplayerClosedCaptionDown",
748 IDR_MEDIAPLAYER_CLOSEDCAPTION_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
749 { "mediaplayerClosedCaptionDisabled",
750 IDR_MEDIAPLAYER_CLOSEDCAPTION_BUTTON_DISABLED, ui::SCALE_FACTOR_100P },
751 { "mediaplayerFullscreen",
752 IDR_MEDIAPLAYER_FULLSCREEN_BUTTON, ui::SCALE_FACTOR_100P },
753 { "mediaplayerFullscreenHover",
754 IDR_MEDIAPLAYER_FULLSCREEN_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
755 { "mediaplayerFullscreenDown",
756 IDR_MEDIAPLAYER_FULLSCREEN_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
757 { "mediaplayerFullscreenDisabled",
758 IDR_MEDIAPLAYER_FULLSCREEN_BUTTON_DISABLED, ui::SCALE_FACTOR_100P },
759 { "mediaplayerOverlayPlay",
760 IDR_MEDIAPLAYER_OVERLAY_PLAY_BUTTON, ui::SCALE_FACTOR_100P },
761 #if defined(OS_MACOSX)
762 { "overhangPattern", IDR_OVERHANG_PATTERN, ui::SCALE_FACTOR_100P },
763 { "overhangShadow", IDR_OVERHANG_SHADOW, ui::SCALE_FACTOR_100P },
764 #endif
765 { "panIcon", IDR_PAN_SCROLL_ICON, ui::SCALE_FACTOR_100P },
766 { "searchCancel", IDR_SEARCH_CANCEL, ui::SCALE_FACTOR_100P },
767 { "searchCancelPressed", IDR_SEARCH_CANCEL_PRESSED, ui::SCALE_FACTOR_100P },
768 { "searchMagnifier", IDR_SEARCH_MAGNIFIER, ui::SCALE_FACTOR_100P },
769 { "searchMagnifierResults",
770 IDR_SEARCH_MAGNIFIER_RESULTS, ui::SCALE_FACTOR_100P },
771 { "textAreaResizeCorner", IDR_TEXTAREA_RESIZER, ui::SCALE_FACTOR_100P },
772 { "textAreaResizeCorner@2x", IDR_TEXTAREA_RESIZER, ui::SCALE_FACTOR_200P },
773 { "generatePassword", IDR_PASSWORD_GENERATION_ICON, ui::SCALE_FACTOR_100P },
774 { "generatePasswordHover",
775 IDR_PASSWORD_GENERATION_ICON_HOVER, ui::SCALE_FACTOR_100P },
776 { "html.css", IDR_UASTYLE_HTML_CSS, ui::SCALE_FACTOR_NONE },
777 { "quirks.css", IDR_UASTYLE_QUIRKS_CSS, ui::SCALE_FACTOR_NONE },
778 { "view-source.css", IDR_UASTYLE_VIEW_SOURCE_CSS, ui::SCALE_FACTOR_NONE },
779 { "themeChromium.css", IDR_UASTYLE_THEME_CHROMIUM_CSS,
780 ui::SCALE_FACTOR_NONE },
781 #if defined(OS_ANDROID)
782 { "themeChromiumAndroid.css", IDR_UASTYLE_THEME_CHROMIUM_ANDROID_CSS,
783 ui::SCALE_FACTOR_NONE },
784 { "mediaControlsAndroid.css", IDR_UASTYLE_MEDIA_CONTROLS_ANDROID_CSS,
785 ui::SCALE_FACTOR_NONE },
786 #endif
787 #if !defined(OS_WIN)
788 { "themeChromiumLinux.css", IDR_UASTYLE_THEME_CHROMIUM_LINUX_CSS,
789 ui::SCALE_FACTOR_NONE },
790 #endif
791 { "themeChromiumSkia.css", IDR_UASTYLE_THEME_CHROMIUM_SKIA_CSS,
792 ui::SCALE_FACTOR_NONE },
793 { "themeInputMultipleFields.css",
794 IDR_UASTYLE_THEME_INPUT_MULTIPLE_FIELDS_CSS, ui::SCALE_FACTOR_NONE },
795 #if defined(OS_MACOSX)
796 { "themeMac.css", IDR_UASTYLE_THEME_MAC_CSS, ui::SCALE_FACTOR_NONE },
797 #endif
798 { "themeWin.css", IDR_UASTYLE_THEME_WIN_CSS, ui::SCALE_FACTOR_NONE },
799 { "themeWinQuirks.css", IDR_UASTYLE_THEME_WIN_QUIRKS_CSS,
800 ui::SCALE_FACTOR_NONE },
801 { "svg.css", IDR_UASTYLE_SVG_CSS, ui::SCALE_FACTOR_NONE},
802 { "navigationTransitions.css", IDR_UASTYLE_NAVIGATION_TRANSITIONS_CSS,
803 ui::SCALE_FACTOR_NONE },
804 { "mathml.css", IDR_UASTYLE_MATHML_CSS, ui::SCALE_FACTOR_NONE},
805 { "mediaControls.css", IDR_UASTYLE_MEDIA_CONTROLS_CSS,
806 ui::SCALE_FACTOR_NONE },
807 { "fullscreen.css", IDR_UASTYLE_FULLSCREEN_CSS, ui::SCALE_FACTOR_NONE},
808 { "xhtmlmp.css", IDR_UASTYLE_XHTMLMP_CSS, ui::SCALE_FACTOR_NONE},
809 { "viewportAndroid.css", IDR_UASTYLE_VIEWPORT_ANDROID_CSS,
810 ui::SCALE_FACTOR_NONE},
811 { "XMLViewer.js", IDR_XML_VIEWER_JS, ui::SCALE_FACTOR_NONE },
812 { "XMLViewer.css", IDR_XML_VIEWER_CSS, ui::SCALE_FACTOR_NONE },
813 { "InspectorOverlayPage.html", IDR_INSPECTOR_OVERLAY_PAGE_HTML,
814 ui::SCALE_FACTOR_NONE },
815 { "InjectedScriptCanvasModuleSource.js",
816 IDR_INSPECTOR_INJECTED_SCRIPT_CANVAS_MODULE_SOURCE_JS,
817 ui::SCALE_FACTOR_NONE },
818 { "InjectedScriptSource.js", IDR_INSPECTOR_INJECTED_SCRIPT_SOURCE_JS,
819 ui::SCALE_FACTOR_NONE },
820 { "DebuggerScriptSource.js", IDR_INSPECTOR_DEBUGGER_SCRIPT_SOURCE_JS,
821 ui::SCALE_FACTOR_NONE },
822 #ifdef IDR_PICKER_COMMON_JS
823 { "pickerCommon.js", IDR_PICKER_COMMON_JS, ui::SCALE_FACTOR_NONE },
824 { "pickerCommon.css", IDR_PICKER_COMMON_CSS, ui::SCALE_FACTOR_NONE },
825 { "calendarPicker.js", IDR_CALENDAR_PICKER_JS, ui::SCALE_FACTOR_NONE },
826 { "calendarPicker.css", IDR_CALENDAR_PICKER_CSS, ui::SCALE_FACTOR_NONE },
827 { "pickerButton.css", IDR_PICKER_BUTTON_CSS, ui::SCALE_FACTOR_NONE },
828 { "suggestionPicker.js", IDR_SUGGESTION_PICKER_JS, ui::SCALE_FACTOR_NONE },
829 { "suggestionPicker.css", IDR_SUGGESTION_PICKER_CSS, ui::SCALE_FACTOR_NONE },
830 { "colorSuggestionPicker.js",
831 IDR_COLOR_SUGGESTION_PICKER_JS, ui::SCALE_FACTOR_NONE },
832 { "colorSuggestionPicker.css",
833 IDR_COLOR_SUGGESTION_PICKER_CSS, ui::SCALE_FACTOR_NONE },
834 #endif
837 } // namespace
839 WebData BlinkPlatformImpl::loadResource(const char* name) {
840 // Some clients will call into this method with an empty |name| when they have
841 // optional resources. For example, the PopupMenuChromium code can have icons
842 // for some Autofill items but not for others.
843 if (!strlen(name))
844 return WebData();
846 // Check the name prefix to see if it's an audio resource.
847 if (StartsWithASCII(name, "IRC_Composite", true) ||
848 StartsWithASCII(name, "Composite", true))
849 return loadAudioSpatializationResource(name);
851 // TODO(flackr): We should use a better than linear search here, a trie would
852 // be ideal.
853 for (size_t i = 0; i < arraysize(kDataResources); ++i) {
854 if (!strcmp(name, kDataResources[i].name)) {
855 base::StringPiece resource = GetContentClient()->GetDataResource(
856 kDataResources[i].id, kDataResources[i].scale_factor);
857 return WebData(resource.data(), resource.size());
861 NOTREACHED() << "Unknown image resource " << name;
862 return WebData();
865 WebString BlinkPlatformImpl::queryLocalizedString(
866 WebLocalizedString::Name name) {
867 int message_id = ToMessageID(name);
868 if (message_id < 0)
869 return WebString();
870 return GetContentClient()->GetLocalizedString(message_id);
873 WebString BlinkPlatformImpl::queryLocalizedString(
874 WebLocalizedString::Name name, int numeric_value) {
875 return queryLocalizedString(name, base::IntToString16(numeric_value));
878 WebString BlinkPlatformImpl::queryLocalizedString(
879 WebLocalizedString::Name name, const WebString& value) {
880 int message_id = ToMessageID(name);
881 if (message_id < 0)
882 return WebString();
883 return ReplaceStringPlaceholders(GetContentClient()->GetLocalizedString(
884 message_id), value, NULL);
887 WebString BlinkPlatformImpl::queryLocalizedString(
888 WebLocalizedString::Name name,
889 const WebString& value1,
890 const WebString& value2) {
891 int message_id = ToMessageID(name);
892 if (message_id < 0)
893 return WebString();
894 std::vector<base::string16> values;
895 values.reserve(2);
896 values.push_back(value1);
897 values.push_back(value2);
898 return ReplaceStringPlaceholders(
899 GetContentClient()->GetLocalizedString(message_id), values, NULL);
902 double BlinkPlatformImpl::currentTime() {
903 return base::Time::Now().ToDoubleT();
906 double BlinkPlatformImpl::monotonicallyIncreasingTime() {
907 return base::TimeTicks::Now().ToInternalValue() /
908 static_cast<double>(base::Time::kMicrosecondsPerSecond);
911 void BlinkPlatformImpl::cryptographicallyRandomValues(
912 unsigned char* buffer, size_t length) {
913 base::RandBytes(buffer, length);
916 void BlinkPlatformImpl::setSharedTimerFiredFunction(void (*func)()) {
917 shared_timer_func_ = func;
920 void BlinkPlatformImpl::setSharedTimerFireInterval(
921 double interval_seconds) {
922 shared_timer_fire_time_ = interval_seconds + monotonicallyIncreasingTime();
923 if (shared_timer_suspended_) {
924 shared_timer_fire_time_was_set_while_suspended_ = true;
925 return;
928 // By converting between double and int64 representation, we run the risk
929 // of losing precision due to rounding errors. Performing computations in
930 // microseconds reduces this risk somewhat. But there still is the potential
931 // of us computing a fire time for the timer that is shorter than what we
932 // need.
933 // As the event loop will check event deadlines prior to actually firing
934 // them, there is a risk of needlessly rescheduling events and of
935 // needlessly looping if sleep times are too short even by small amounts.
936 // This results in measurable performance degradation unless we use ceil() to
937 // always round up the sleep times.
938 int64 interval = static_cast<int64>(
939 ceil(interval_seconds * base::Time::kMillisecondsPerSecond)
940 * base::Time::kMicrosecondsPerMillisecond);
942 if (interval < 0)
943 interval = 0;
945 shared_timer_.Stop();
946 shared_timer_.Start(FROM_HERE, base::TimeDelta::FromMicroseconds(interval),
947 this, &BlinkPlatformImpl::DoTimeout);
948 OnStartSharedTimer(base::TimeDelta::FromMicroseconds(interval));
951 void BlinkPlatformImpl::stopSharedTimer() {
952 shared_timer_.Stop();
955 void BlinkPlatformImpl::callOnMainThread(
956 void (*func)(void*), void* context) {
957 main_loop_->PostTask(FROM_HERE, base::Bind(func, context));
960 blink::WebGestureCurve* BlinkPlatformImpl::createFlingAnimationCurve(
961 blink::WebGestureDevice device_source,
962 const blink::WebFloatPoint& velocity,
963 const blink::WebSize& cumulative_scroll) {
964 #if defined(OS_ANDROID)
965 return FlingAnimatorImpl::CreateAndroidGestureCurve(
966 velocity,
967 cumulative_scroll);
968 #endif
970 if (device_source == blink::WebGestureDeviceTouchscreen)
971 return fling_curve_configuration_->CreateForTouchScreen(velocity,
972 cumulative_scroll);
974 return fling_curve_configuration_->CreateForTouchPad(velocity,
975 cumulative_scroll);
978 void BlinkPlatformImpl::didStartWorkerRunLoop(
979 const blink::WebWorkerRunLoop& runLoop) {
980 WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance();
981 worker_task_runner->OnWorkerRunLoopStarted(runLoop);
984 void BlinkPlatformImpl::didStopWorkerRunLoop(
985 const blink::WebWorkerRunLoop& runLoop) {
986 WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance();
987 worker_task_runner->OnWorkerRunLoopStopped(runLoop);
990 blink::WebCrypto* BlinkPlatformImpl::crypto() {
991 return &web_crypto_;
995 WebThemeEngine* BlinkPlatformImpl::themeEngine() {
996 return &native_theme_engine_;
999 WebFallbackThemeEngine* BlinkPlatformImpl::fallbackThemeEngine() {
1000 return &fallback_theme_engine_;
1003 blink::Platform::FileHandle BlinkPlatformImpl::databaseOpenFile(
1004 const blink::WebString& vfs_file_name, int desired_flags) {
1005 #if defined(OS_WIN)
1006 return INVALID_HANDLE_VALUE;
1007 #elif defined(OS_POSIX)
1008 return -1;
1009 #endif
1012 int BlinkPlatformImpl::databaseDeleteFile(
1013 const blink::WebString& vfs_file_name, bool sync_dir) {
1014 return -1;
1017 long BlinkPlatformImpl::databaseGetFileAttributes(
1018 const blink::WebString& vfs_file_name) {
1019 return 0;
1022 long long BlinkPlatformImpl::databaseGetFileSize(
1023 const blink::WebString& vfs_file_name) {
1024 return 0;
1027 long long BlinkPlatformImpl::databaseGetSpaceAvailableForOrigin(
1028 const blink::WebString& origin_identifier) {
1029 return 0;
1032 blink::WebString BlinkPlatformImpl::signedPublicKeyAndChallengeString(
1033 unsigned key_size_index,
1034 const blink::WebString& challenge,
1035 const blink::WebURL& url) {
1036 return blink::WebString("");
1039 static scoped_ptr<base::ProcessMetrics> CurrentProcessMetrics() {
1040 using base::ProcessMetrics;
1041 #if defined(OS_MACOSX)
1042 return scoped_ptr<ProcessMetrics>(
1043 // The default port provider is sufficient to get data for the current
1044 // process.
1045 ProcessMetrics::CreateProcessMetrics(base::GetCurrentProcessHandle(),
1046 NULL));
1047 #else
1048 return scoped_ptr<ProcessMetrics>(
1049 ProcessMetrics::CreateProcessMetrics(base::GetCurrentProcessHandle()));
1050 #endif
1053 static size_t getMemoryUsageMB(bool bypass_cache) {
1054 size_t current_mem_usage = 0;
1055 MemoryUsageCache* mem_usage_cache_singleton = MemoryUsageCache::GetInstance();
1056 if (!bypass_cache &&
1057 mem_usage_cache_singleton->IsCachedValueValid(&current_mem_usage))
1058 return current_mem_usage;
1060 current_mem_usage = GetMemoryUsageKB() >> 10;
1061 mem_usage_cache_singleton->SetMemoryValue(current_mem_usage);
1062 return current_mem_usage;
1065 size_t BlinkPlatformImpl::memoryUsageMB() {
1066 return getMemoryUsageMB(false);
1069 size_t BlinkPlatformImpl::actualMemoryUsageMB() {
1070 return getMemoryUsageMB(true);
1073 size_t BlinkPlatformImpl::physicalMemoryMB() {
1074 return static_cast<size_t>(base::SysInfo::AmountOfPhysicalMemoryMB());
1077 size_t BlinkPlatformImpl::virtualMemoryLimitMB() {
1078 return static_cast<size_t>(base::SysInfo::AmountOfVirtualMemoryMB());
1081 size_t BlinkPlatformImpl::numberOfProcessors() {
1082 return static_cast<size_t>(base::SysInfo::NumberOfProcessors());
1085 void BlinkPlatformImpl::startHeapProfiling(
1086 const blink::WebString& prefix) {
1087 // FIXME(morrita): Make this built on windows.
1088 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
1089 HeapProfilerStart(prefix.utf8().data());
1090 #endif
1093 void BlinkPlatformImpl::stopHeapProfiling() {
1094 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
1095 HeapProfilerStop();
1096 #endif
1099 void BlinkPlatformImpl::dumpHeapProfiling(
1100 const blink::WebString& reason) {
1101 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
1102 HeapProfilerDump(reason.utf8().data());
1103 #endif
1106 WebString BlinkPlatformImpl::getHeapProfile() {
1107 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
1108 char* data = GetHeapProfile();
1109 WebString result = WebString::fromUTF8(std::string(data));
1110 free(data);
1111 return result;
1112 #else
1113 return WebString();
1114 #endif
1117 bool BlinkPlatformImpl::processMemorySizesInBytes(
1118 size_t* private_bytes,
1119 size_t* shared_bytes) {
1120 return CurrentProcessMetrics()->GetMemoryBytes(private_bytes, shared_bytes);
1123 bool BlinkPlatformImpl::memoryAllocatorWasteInBytes(size_t* size) {
1124 return base::allocator::GetAllocatorWasteSize(size);
1127 blink::WebDiscardableMemory*
1128 BlinkPlatformImpl::allocateAndLockDiscardableMemory(size_t bytes) {
1129 base::DiscardableMemoryType type =
1130 base::DiscardableMemory::GetPreferredType();
1131 if (type == base::DISCARDABLE_MEMORY_TYPE_EMULATED)
1132 return NULL;
1133 return content::WebDiscardableMemoryImpl::CreateLockedMemory(bytes).release();
1136 size_t BlinkPlatformImpl::maxDecodedImageBytes() {
1137 #if defined(OS_ANDROID)
1138 if (base::SysInfo::IsLowEndDevice()) {
1139 // Limit image decoded size to 3M pixels on low end devices.
1140 // 4 is maximum number of bytes per pixel.
1141 return 3 * 1024 * 1024 * 4;
1143 // For other devices, limit decoded image size based on the amount of physical
1144 // memory.
1145 // In some cases all physical memory is not accessible by Chromium, as it can
1146 // be reserved for direct use by certain hardware. Thus, we set the limit so
1147 // that 1.6GB of reported physical memory on a 2GB device is enough to set the
1148 // limit at 16M pixels, which is a desirable value since 4K*4K is a relatively
1149 // common texture size.
1150 return base::SysInfo::AmountOfPhysicalMemory() / 25;
1151 #else
1152 return noDecodedImageByteLimit;
1153 #endif
1156 void BlinkPlatformImpl::SetFlingCurveParameters(
1157 const std::vector<float>& new_touchpad,
1158 const std::vector<float>& new_touchscreen) {
1159 fling_curve_configuration_->SetCurveParameters(new_touchpad, new_touchscreen);
1162 void BlinkPlatformImpl::SuspendSharedTimer() {
1163 ++shared_timer_suspended_;
1166 void BlinkPlatformImpl::ResumeSharedTimer() {
1167 DCHECK_GT(shared_timer_suspended_, 0);
1169 // The shared timer may have fired or been adjusted while we were suspended.
1170 if (--shared_timer_suspended_ == 0 &&
1171 (!shared_timer_.IsRunning() ||
1172 shared_timer_fire_time_was_set_while_suspended_)) {
1173 shared_timer_fire_time_was_set_while_suspended_ = false;
1174 setSharedTimerFireInterval(
1175 shared_timer_fire_time_ - monotonicallyIncreasingTime());
1179 // static
1180 void BlinkPlatformImpl::DestroyCurrentThread(void* thread) {
1181 WebThreadImplForMessageLoop* impl =
1182 static_cast<WebThreadImplForMessageLoop*>(thread);
1183 delete impl;
1186 } // namespace content