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

Side by Side Diff: chrome/renderer/extensions/extension_request_sender.cc

Issue 10703111: Cleanup: make ExtensionRequestSender manage its IPC responses directly, rather (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ScopedObserverify Created 8 years, 5 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 "chrome/renderer/extensions/extension_request_sender.h" 5 #include "chrome/renderer/extensions/extension_request_sender.h"
6 6
7 #include "base/values.h" 7 #include "base/values.h"
8 #include "chrome/common/extensions/extension_messages.h" 8 #include "chrome/common/extensions/extension_messages.h"
9 #include "chrome/renderer/extensions/chrome_v8_context.h" 9 #include "chrome/renderer/extensions/chrome_v8_context.h"
10 #include "chrome/renderer/extensions/chrome_v8_context_set.h" 10 #include "chrome/renderer/extensions/chrome_v8_context_set.h"
11 #include "chrome/renderer/extensions/extension_dispatcher.h" 11 #include "chrome/renderer/extensions/extension_dispatcher.h"
12 #include "content/public/renderer/render_thread.h"
12 #include "content/public/renderer/render_view.h" 13 #include "content/public/renderer/render_view.h"
13 #include "content/public/renderer/v8_value_converter.h" 14 #include "content/public/renderer/v8_value_converter.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
17 18
18 using content::V8ValueConverter; 19 using content::V8ValueConverter;
19 20
20 // Contains info relevant to a pending API request. 21 // Contains info relevant to a pending API request.
21 struct PendingRequest { 22 struct PendingRequest {
22 public : 23 public :
23 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name, 24 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name,
24 const std::string& extension_id) 25 const std::string& extension_id)
25 : context(context), name(name), extension_id(extension_id) { 26 : context(context), name(name), extension_id(extension_id) {
26 } 27 }
27 28
28 ~PendingRequest() { 29 ~PendingRequest() {
29 context.Dispose(); 30 context.Dispose();
30 } 31 }
31 32
32 v8::Persistent<v8::Context> context; 33 v8::Persistent<v8::Context> context;
33 std::string name; 34 std::string name;
34 std::string extension_id; 35 std::string extension_id;
35 }; 36 };
36 37
37 ExtensionRequestSender::ExtensionRequestSender( 38 ExtensionRequestSender::ExtensionRequestSender(
38 ExtensionDispatcher* extension_dispatcher, 39 ExtensionDispatcher* extension_dispatcher,
39 ChromeV8ContextSet* context_set) 40 ChromeV8ContextSet* context_set)
40 : extension_dispatcher_(extension_dispatcher), 41 : extension_dispatcher_(extension_dispatcher),
41 context_set_(context_set) { 42 context_set_(context_set),
43 scoped_observer_(this, content::RenderThread::Get()),
44 next_request_id_(0) {
42 } 45 }
43 46
44 ExtensionRequestSender::~ExtensionRequestSender() { 47 ExtensionRequestSender::~ExtensionRequestSender() {
45 } 48 }
46 49
50 bool ExtensionRequestSender::OnControlMessageReceived(
51 const IPC::Message& message) {
52 bool handled = true;
53 IPC_BEGIN_MESSAGE_MAP(ExtensionRequestSender, message)
54 IPC_MESSAGE_HANDLER(ExtensionMsg_Response, OnExtensionResponse)
55 IPC_MESSAGE_UNHANDLED(handled = false)
56 IPC_END_MESSAGE_MAP()
57 return handled;
58 }
59
47 void ExtensionRequestSender::InsertRequest(int request_id, 60 void ExtensionRequestSender::InsertRequest(int request_id,
48 PendingRequest* pending_request) { 61 PendingRequest* pending_request) {
49 DCHECK_EQ(0u, pending_requests_.count(request_id)); 62 DCHECK_EQ(0u, pending_requests_.count(request_id));
50 pending_requests_[request_id].reset(pending_request); 63 pending_requests_[request_id].reset(pending_request);
51 } 64 }
52 65
53 linked_ptr<PendingRequest> ExtensionRequestSender::RemoveRequest( 66 linked_ptr<PendingRequest> ExtensionRequestSender::RemoveRequest(
54 int request_id) { 67 int request_id) {
55 PendingRequestMap::iterator i = pending_requests_.find(request_id); 68 PendingRequestMap::iterator i = pending_requests_.find(request_id);
56 if (i == pending_requests_.end()) 69 if (i == pending_requests_.end())
57 return linked_ptr<PendingRequest>(); 70 return linked_ptr<PendingRequest>();
58 linked_ptr<PendingRequest> result = i->second; 71 linked_ptr<PendingRequest> result = i->second;
59 pending_requests_.erase(i); 72 pending_requests_.erase(i);
60 return result; 73 return result;
61 } 74 }
62 75
63 void ExtensionRequestSender::StartRequest( 76 int ExtensionRequestSender::StartRequest(const std::string& name,
64 const std::string& name, 77 bool has_callback,
65 int request_id, 78 bool for_io_thread,
66 bool has_callback, 79 base::ListValue* value_args) {
67 bool for_io_thread,
68 base::ListValue* value_args) {
69 ChromeV8Context* current_context = context_set_->GetCurrent(); 80 ChromeV8Context* current_context = context_set_->GetCurrent();
70 if (!current_context) 81 if (!current_context)
71 return; 82 return -1;
72 83
73 // Get the current RenderView so that we can send a routed IPC message from 84 // Get the current RenderView so that we can send a routed IPC message from
74 // the correct source. 85 // the correct source.
75 content::RenderView* renderview = current_context->GetRenderView(); 86 content::RenderView* renderview = current_context->GetRenderView();
76 if (!renderview) 87 if (!renderview)
77 return; 88 return -1;
78 89
79 const std::set<std::string>& function_names = 90 const std::set<std::string>& function_names =
80 extension_dispatcher_->function_names(); 91 extension_dispatcher_->function_names();
81 if (function_names.find(name) == function_names.end()) { 92 if (function_names.find(name) == function_names.end()) {
82 NOTREACHED() << "Unexpected function " << name << 93 NOTREACHED() << "Unexpected function " << name <<
83 ". Did you remember to register it with ExtensionFunctionRegistry?"; 94 ". Did you remember to register it with ExtensionFunctionRegistry?";
84 return; 95 return -1;
85 } 96 }
86 97
87 // TODO(koz): See if we can make this a CHECK. 98 // TODO(koz): See if we can make this a CHECK.
88 if (!extension_dispatcher_->CheckCurrentContextAccessToExtensionAPI(name)) 99 if (!extension_dispatcher_->CheckCurrentContextAccessToExtensionAPI(name))
89 return; 100 return -1;
90 101
91 GURL source_url; 102 GURL source_url;
92 WebKit::WebSecurityOrigin source_origin; 103 WebKit::WebSecurityOrigin source_origin;
93 WebKit::WebFrame* webframe = current_context->web_frame(); 104 WebKit::WebFrame* webframe = current_context->web_frame();
94 if (webframe) { 105 if (webframe) {
95 source_url = webframe->document().url(); 106 source_url = webframe->document().url();
96 source_origin = webframe->document().securityOrigin(); 107 source_origin = webframe->document().securityOrigin();
97 } 108 }
98 109
99 v8::Persistent<v8::Context> v8_context = 110 v8::Persistent<v8::Context> v8_context =
100 v8::Persistent<v8::Context>::New(v8::Context::GetCurrent()); 111 v8::Persistent<v8::Context>::New(v8::Context::GetCurrent());
101 DCHECK(!v8_context.IsEmpty()); 112 DCHECK(!v8_context.IsEmpty());
102 113
114 int request_id = next_request_id_++;
115
103 std::string extension_id = current_context->GetExtensionID(); 116 std::string extension_id = current_context->GetExtensionID();
104 InsertRequest(request_id, new PendingRequest( 117 InsertRequest(request_id, new PendingRequest(v8_context, name, extension_id));
105 v8_context, name, extension_id));
106 118
107 ExtensionHostMsg_Request_Params params; 119 ExtensionHostMsg_Request_Params params;
108 params.name = name; 120 params.name = name;
109 params.arguments.Swap(value_args); 121 params.arguments.Swap(value_args);
110 params.extension_id = extension_id; 122 params.extension_id = extension_id;
111 params.source_url = source_url; 123 params.source_url = source_url;
112 params.source_origin = source_origin.toString(); 124 params.source_origin = source_origin.toString();
113 params.request_id = request_id; 125 params.request_id = request_id;
114 params.has_callback = has_callback; 126 params.has_callback = has_callback;
115 params.user_gesture = 127 params.user_gesture =
116 webframe ? webframe->isProcessingUserGesture() : false; 128 webframe ? webframe->isProcessingUserGesture() : false;
117 if (for_io_thread) { 129 if (for_io_thread) {
118 renderview->Send(new ExtensionHostMsg_RequestForIOThread( 130 renderview->Send(new ExtensionHostMsg_RequestForIOThread(
119 renderview->GetRoutingID(), params)); 131 renderview->GetRoutingID(), params));
120 } else { 132 } else {
121 renderview->Send(new ExtensionHostMsg_Request( 133 renderview->Send(new ExtensionHostMsg_Request(
122 renderview->GetRoutingID(), params)); 134 renderview->GetRoutingID(), params));
123 } 135 }
136
137 return request_id;
124 } 138 }
125 139
126 void ExtensionRequestSender::HandleResponse(int request_id, 140 void ExtensionRequestSender::OnExtensionResponse(
127 bool success, 141 int request_id,
128 const base::ListValue& response, 142 bool success,
129 const std::string& error) { 143 const base::ListValue& response,
144 const std::string& error) {
130 linked_ptr<PendingRequest> request = RemoveRequest(request_id); 145 linked_ptr<PendingRequest> request = RemoveRequest(request_id);
131 146
132 if (!request.get()) { 147 if (!request.get()) {
133 // This should not be able to happen since we only remove requests when 148 // This should not be able to happen since we only remove requests when
134 // they are handled. 149 // they are handled.
135 LOG(ERROR) << "Could not find specified request id: " << request_id; 150 LOG(ERROR) << "Could not find specified request id: " << request_id;
136 return; 151 return;
137 } 152 }
138 153
139 ChromeV8Context* v8_context = context_set_->GetByV8Context(request->context); 154 ChromeV8Context* v8_context = context_set_->GetByV8Context(request->context);
(...skipping 28 matching lines...) Expand all
168 &retval)); 183 &retval));
169 // In debug, the js will validate the callback parameters and return a 184 // In debug, the js will validate the callback parameters and return a
170 // string if a validation error has occured. 185 // string if a validation error has occured.
171 #ifndef NDEBUG 186 #ifndef NDEBUG
172 if (!retval.IsEmpty() && !retval->IsUndefined()) { 187 if (!retval.IsEmpty() && !retval->IsUndefined()) {
173 std::string error = *v8::String::AsciiValue(retval); 188 std::string error = *v8::String::AsciiValue(retval);
174 DCHECK(false) << error; 189 DCHECK(false) << error;
175 } 190 }
176 #endif 191 #endif
177 } 192 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698