Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: jni/AndroidWebRequest.cpp

Issue 5668574207148032: Handle network errors better (Closed)
Left Patch Set: Created Nov. 22, 2013, 4:15 p.m.
Right Patch Set: Use GetString() and don't return a c_str() to a temporary string object Created Nov. 22, 2013, 6 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <AdblockPlus.h> 18 #include <AdblockPlus.h>
19 #include "AndroidWebRequest.h" 19 #include "AndroidWebRequest.h"
20 #include "Utils.h" 20 #include "Utils.h"
21 #include "debug.h" 21 #include "debug.h"
22 22
23 namespace 23 namespace
24 { 24 {
25 std::string ExtractExceptionMessage(JNIEnv* env, jthrowable throwable) 25 std::string ExtractExceptionMessage(JNIEnv* env, jthrowable throwable)
26 { 26 {
27 jclass throwableClass = env->FindClass("java/lang/Throwable"); 27 jclass throwableClass = env->FindClass("java/lang/Throwable");
28 jmethodID throwableToStringMethodId = env->GetMethodID(throwableClass, "toSt ring", "()Ljava/lang/String;"); 28 jmethodID throwableToStringMethodId = env->GetMethodID(throwableClass, "toSt ring", "()Ljava/lang/String;");
29 jstring javaMessage = static_cast<jstring>(env->CallObjectMethod(throwable, throwableToStringMethodId)); 29 jstring javaMessage = static_cast<jstring>(env->CallObjectMethod(throwable, throwableToStringMethodId));
30 const char* nativeMessage = env->GetStringUTFChars(javaMessage, 0); 30 return "Java exception: " + GetString(env, javaMessage);
31 const std::string message = nativeMessage;
32 env->ReleaseStringUTFChars(javaMessage, nativeMessage);
33 return message;
34 } 31 }
35 32
36 class JavaException : public std::exception 33 class JavaException : public std::exception
37 { 34 {
38 public: 35 public:
39 JavaException(JNIEnv* env) 36 JavaException(JNIEnv* env)
40 : env(env), throwable(env->ExceptionOccurred()) 37 : env(env), throwable(env->ExceptionOccurred())
41 { 38 {
42 env->ExceptionClear(); 39 env->ExceptionClear();
43 message = ExtractExceptionMessage(env, throwable); 40 message = ExtractExceptionMessage(env, throwable);
44 } 41 }
45 42
46 virtual ~JavaException() throw() 43 virtual ~JavaException() throw()
47 { 44 {
48 } 45 }
49 46
50 const char* what() const throw() 47 const char* what() const throw()
51 { 48 {
52 return ("Java Exception: " + message).c_str(); 49 return message.c_str();
Wladimir Palant 2013/11/22 17:51:03 You are creating a temporary std::string instance
Felix Dahlke 2013/11/22 18:01:12 Done.
53 } 50 }
54 51
55 bool IsInstanceOf(const std::string& className) const 52 bool IsInstanceOf(const std::string& className) const
56 { 53 {
57 jclass clazz = env->FindClass(className.c_str()); 54 jclass clazz = env->FindClass(className.c_str());
58 if (!clazz) 55 if (!clazz)
59 return false; 56 return false;
60 bool isInstance = env->IsInstanceOf(throwable, clazz); 57 bool isInstance = env->IsInstanceOf(throwable, clazz);
61 env->DeleteLocalRef(clazz); 58 env->DeleteLocalRef(clazz);
62 return isInstance; 59 return isInstance;
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 { 314 {
318 D(D_ERROR, "Unknown exception"); 315 D(D_ERROR, "Unknown exception");
319 result.status = AdblockPlus::DefaultWebRequest::NS_ERROR_FAILURE; 316 result.status = AdblockPlus::DefaultWebRequest::NS_ERROR_FAILURE;
320 } 317 }
321 318
322 if (stat == JNI_EDETACHED) 319 if (stat == JNI_EDETACHED)
323 globalJvm->DetachCurrentThread(); 320 globalJvm->DetachCurrentThread();
324 321
325 return result; 322 return result;
326 } 323 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld