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

Side by Side Diff: content/browser/renderer_host/render_view_host_impl.cc

Issue 11746019: Introduce callback interface for running Javascript asynchronously in the renderer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: .insert Created 7 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/renderer_host/render_view_host_impl.h" 5 #include "content/browser/renderer_host/render_view_host_impl.h"
6 6
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/callback.h"
13 #include "base/command_line.h"
12 #include "base/i18n/rtl.h" 14 #include "base/i18n/rtl.h"
13 #include "base/json/json_reader.h" 15 #include "base/json/json_reader.h"
14 #include "base/json/json_writer.h" 16 #include "base/json/json_writer.h"
15 #include "base/command_line.h"
16 #include "base/message_loop.h" 17 #include "base/message_loop.h"
17 #include "base/metrics/histogram.h" 18 #include "base/metrics/histogram.h"
18 #include "base/stl_util.h" 19 #include "base/stl_util.h"
19 #include "base/string_util.h" 20 #include "base/string_util.h"
20 #include "base/time.h" 21 #include "base/time.h"
21 #include "base/utf_string_conversions.h" 22 #include "base/utf_string_conversions.h"
22 #include "base/values.h" 23 #include "base/values.h"
23 #include "content/browser/accessibility/browser_accessibility_state_impl.h" 24 #include "content/browser/accessibility/browser_accessibility_state_impl.h"
24 #include "content/browser/child_process_security_policy_impl.h" 25 #include "content/browser/child_process_security_policy_impl.h"
25 #include "content/browser/cross_site_request_manager.h" 26 #include "content/browser/cross_site_request_manager.h"
(...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 712
712 int RenderViewHostImpl::ExecuteJavascriptInWebFrameNotifyResult( 713 int RenderViewHostImpl::ExecuteJavascriptInWebFrameNotifyResult(
713 const string16& frame_xpath, 714 const string16& frame_xpath,
714 const string16& jscript) { 715 const string16& jscript) {
715 static int next_id = 1; 716 static int next_id = 1;
716 Send(new ViewMsg_ScriptEvalRequest(GetRoutingID(), frame_xpath, jscript, 717 Send(new ViewMsg_ScriptEvalRequest(GetRoutingID(), frame_xpath, jscript,
717 next_id, true)); 718 next_id, true));
718 return next_id++; 719 return next_id++;
719 } 720 }
720 721
721 typedef std::pair<int, Value*> ExecuteDetailType; 722 void RenderViewHostImpl::ExecuteJavascriptInWebFrameCallbackResult(
723 const string16& frame_xpath,
724 const string16& jscript,
725 const JavascriptResultCallback& callback) {
726 int key = ExecuteJavascriptInWebFrameNotifyResult(frame_xpath, jscript);
727 javascript_callbacks_.insert(std::make_pair(key, callback));
728 }
729
730 typedef std::pair<int, base::Value*> ExecuteDetailType;
722 731
723 ExecuteNotificationObserver::ExecuteNotificationObserver(int id) 732 ExecuteNotificationObserver::ExecuteNotificationObserver(int id)
724 : id_(id) { 733 : id_(id) {
725 } 734 }
726 735
727 ExecuteNotificationObserver::~ExecuteNotificationObserver() { 736 ExecuteNotificationObserver::~ExecuteNotificationObserver() {
728 } 737 }
729 738
730 void ExecuteNotificationObserver::Observe(int type, 739 void ExecuteNotificationObserver::Observe(int type,
731 const NotificationSource& source, 740 const NotificationSource& source,
732 const NotificationDetails& details) { 741 const NotificationDetails& details) {
733 Details<ExecuteDetailType> execute_details = 742 Details<ExecuteDetailType> execute_details =
734 static_cast<Details<ExecuteDetailType> >(details); 743 static_cast<Details<ExecuteDetailType> >(details);
735 int id = execute_details->first; 744 int id = execute_details->first;
736 if (id != id_) 745 if (id != id_)
737 return; 746 return;
738 Value* value = execute_details->second; 747 base::Value* value = execute_details->second;
739 if (value) 748 if (value)
740 value_.reset(value->DeepCopy()); 749 value_.reset(value->DeepCopy());
741 MessageLoop::current()->Quit(); 750 MessageLoop::current()->Quit();
742 } 751 }
743 752
744 Value* RenderViewHostImpl::ExecuteJavascriptAndGetValue( 753 base::Value* RenderViewHostImpl::ExecuteJavascriptAndGetValue(
745 const string16& frame_xpath, 754 const string16& frame_xpath,
746 const string16& jscript) { 755 const string16& jscript) {
747 int id = ExecuteJavascriptInWebFrameNotifyResult(frame_xpath, jscript); 756 int id = ExecuteJavascriptInWebFrameNotifyResult(frame_xpath, jscript);
748 ExecuteNotificationObserver observer(id); 757 ExecuteNotificationObserver observer(id);
749 NotificationRegistrar notification_registrar; 758 NotificationRegistrar notification_registrar;
750 notification_registrar.Add( 759 notification_registrar.Add(
751 &observer, NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, 760 &observer, NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT,
752 Source<RenderViewHost>(this)); 761 Source<RenderViewHost>(this));
753 MessageLoop* loop = MessageLoop::current(); 762 MessageLoop* loop = MessageLoop::current();
754 loop->Run(); 763 loop->Run();
(...skipping 1155 matching lines...) Expand 10 before | Expand all | Expand 10 after
1910 dst_type = NOTIFICATION_ACCESSIBILITY_OTHER; 1919 dst_type = NOTIFICATION_ACCESSIBILITY_OTHER;
1911 NotificationService::current()->Notify( 1920 NotificationService::current()->Notify(
1912 dst_type, 1921 dst_type,
1913 Source<RenderViewHost>(this), 1922 Source<RenderViewHost>(this),
1914 NotificationService::NoDetails()); 1923 NotificationService::NoDetails());
1915 } 1924 }
1916 1925
1917 Send(new AccessibilityMsg_Notifications_ACK(GetRoutingID())); 1926 Send(new AccessibilityMsg_Notifications_ACK(GetRoutingID()));
1918 } 1927 }
1919 1928
1920 void RenderViewHostImpl::OnScriptEvalResponse(int id, const ListValue& result) { 1929 void RenderViewHostImpl::OnScriptEvalResponse(int id,
1921 const Value* result_value; 1930 const base::ListValue& result) {
1931 const base::Value* result_value;
1922 if (!result.Get(0, &result_value)) { 1932 if (!result.Get(0, &result_value)) {
1923 // Programming error or rogue renderer. 1933 // Programming error or rogue renderer.
1924 NOTREACHED() << "Got bad arguments for OnScriptEvalResponse"; 1934 NOTREACHED() << "Got bad arguments for OnScriptEvalResponse";
1925 return; 1935 return;
1926 } 1936 }
1927 std::pair<int, const Value*> details(id, result_value); 1937
1928 NotificationService::current()->Notify( 1938 std::map<int, JavascriptResultCallback>::iterator it =
1929 NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, 1939 javascript_callbacks_.find(id);
1930 Source<RenderViewHost>(this), 1940 if (it != javascript_callbacks_.end()) {
1931 Details<std::pair<int, const Value*> >(&details)); 1941 // ExecuteJavascriptInWebFrameCallbackResult was used; do callback.
1942 it->second.Run(result_value);
1943 javascript_callbacks_.erase(it);
1944 } else {
1945 // ExecuteJavascriptInWebFrameNotifyResult was used; broadcast result.
1946 std::pair<int, const base::Value*> details(id, result_value);
1947 NotificationService::current()->Notify(
1948 NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT,
1949 Source<RenderViewHost>(this),
1950 Details<std::pair<int, const base::Value*> >(&details));
1951 }
1932 } 1952 }
1933 1953
1934 void RenderViewHostImpl::OnDidZoomURL(double zoom_level, 1954 void RenderViewHostImpl::OnDidZoomURL(double zoom_level,
1935 bool remember, 1955 bool remember,
1936 const GURL& url) { 1956 const GURL& url) {
1937 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( 1957 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>(
1938 HostZoomMap::GetForBrowserContext(GetProcess()->GetBrowserContext())); 1958 HostZoomMap::GetForBrowserContext(GetProcess()->GetBrowserContext()));
1939 if (remember) { 1959 if (remember) {
1940 host_zoom_map->SetZoomLevel(net::GetHostOrSpecFromURL(url), zoom_level); 1960 host_zoom_map->SetZoomLevel(net::GetHostOrSpecFromURL(url), zoom_level);
1941 } else { 1961 } else {
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
2074 GetRoutingID(), snapshot_id, snapshot_size, png)); 2094 GetRoutingID(), snapshot_id, snapshot_size, png));
2075 return; 2095 return;
2076 } 2096 }
2077 } 2097 }
2078 2098
2079 Send(new ViewMsg_WindowSnapshotCompleted( 2099 Send(new ViewMsg_WindowSnapshotCompleted(
2080 GetRoutingID(), snapshot_id, gfx::Size(), png)); 2100 GetRoutingID(), snapshot_id, gfx::Size(), png));
2081 } 2101 }
2082 2102
2083 } // namespace content 2103 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698